#!/bin/bash
####################################################################
# Prey Linux Specific Functions - by Tomas Pollak
# (c) 2010 Fork Ltd. (usefork.com) - Licensed under the GPLv3.
####################################################################

. "$platform_path/services"

run_as_current_user(){
	if [ "$(whoami)" != "$logged_user" ]; then
		DISPLAY=:0 sudo su $logged_user -c "$1"
	else
		eval $1
	fi
}

####################################################################
# network stuff
####################################################################

get_mac_address(){
	ifconfig $1 2> /dev/null | grep 'HWaddr' | awk '{print $5}'
}

get_ip_address(){
	ifconfig $1 2> /dev/null | grep "inet " | awk '{print $2}' | sed "s/addr://"
}

get_netmask(){
	ifconfig $1 2> /dev/null | grep "Mask:" | awk '{print $4}' | sed "s/Mask://"
}

get_gateway_ip(){
	ip r | grep "$1" | grep default | cut -d ' ' -f 3
}

get_wifi_device(){
	[ -z "$wifi_device" ] && wifi_device=$(iwconfig 2>&1 | grep -v "no wireless" | cut -f1 -d" " | grep -v "^$" | tail -1)
}

get_wifi_info(){
	[ -z "$wifi_info" ] && wifi_info=$(iwconfig 2>&1 | grep -v "no wireless")
}

####################################################################
# get proxy functions
####################################################################

get_system_proxy(){
	[ -n "$http_proxy" ] && echo "$http_proxy" && return
	[ -n "$https_proxy" ] && echo "$https_proxy" && return
	[ -n "$(which gsettings)" ] && get_proxy_from_gsettings && return
}

get_proxy_from_gsettings(){
	local proxy_host=$(gsettings get org.gnome.system.proxy.http host | sed "s/'//g")
	if [ -n "$proxy_host" ]; then
		local proxy_port=$(gsettings get org.gnome.system.proxy.http port | sed "s/'//g")
		echo "${proxy_host}:${proxy_port}"
	fi
}

####################################################################
# auto connect
####################################################################

get_open_ssid(){
	local iwlist=$(which iwlist)

	# access_points=`iwlist $wifi_device | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr`

	$iwlist $wifi_device scan | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr | grep "off" | head -1 | cut -d ' ' -f1 | sed 's/"//g'
}

# attempts to connect to the first open public wifi network
# if we dont have NetworkManager available, we use plain iwconfig
try_to_connect() {

	if [ -n "$(is_process_running $network_manager_process)" ]; then

		log " -- Restarting $network_manager_process and giving it some time to connect."
		service $network_manager_process restart &> /dev/null
		sleep 5

	else

		get_wifi_device

		if [ -z "$wifi_device" ]; then
			log ' !! No wifi device found!'
			return 1
		fi

		local open_ssid=$(get_open_ssid)

		if [ -z "$open_ssid" ]; then
			log ' -- No open SSIDs found.'
			return 1
		fi

		log " -- SSID found! Attempting to connect to ${open_ssid}..."
		$(which iwconfig) $wifi_device essid $open_ssid
		$(which dhclient3) $wifi_device

	fi

}

reverse_tunnel_command(){
	setsid "$base_path/lib/tunnel.sh" ${remote_tunnel_host} ${local_tunnel_port} ${remote_tunnel_port} ${remote_tunnel_user} ${remote_tunnel_pass} &> /dev/null
}

############################################
# trigger enabling functions
############################################

# echoes 1 if prey trigger is set
is_trigger_loaded(){
	if [ -d "/etc/rc3.d" ]; then
		find /etc/rc3.d/ | grep 'prey-trigger' > /dev/null && echo 1
	elif [ -d "/etc/init.d" ]; then
		find /etc/init.d/ | grep 'prey-trigger' > /dev/null && echo 1
	elif [ -d "/etc/rc.d" ]; then
		find /etc/rc.d/ | grep 'prey-trigger' > /dev/null && echo 1
	fi
}

reload_trigger(){
	log " -- Setting up network detection trigger init script..."
	linux_load_service 'prey-trigger'
}

# unloads trigger from launchd and removes plist file
unload_trigger(){
	log " -- Removing network detection trigger init script..."
	linux_unload_service 'prey-trigger'
}

############################################
# updater-specific functions
############################################

# here we'll eventually put whatever we need to do in linux before
# performing a full update
pre_update_hook(){
	return 0
}

# post update hooks go in here
post_update_hook(){
	log ' -- Reloading Prey...'
	"$base_path/prey.sh" & # lets restart prey now
}

############################################
# device creation stuff
############################################

get_pc_info(){
	pc_name=$(hostname)

	$(which laptop-detect)
	[ $? == 1 ] && pc_type="Desktop" || pc_type="Laptop"

	get_distro_name
	if [ "$distro_name" == "ubuntu" ]; then
		pc_os_version=$(lsb_release -r -s)
	elif [ "$distro_name" == "debian" ]; then
		pc_os_version=$(cat /etc/debian_version)
	fi

}

get_distro_name(){

	[ -n "$distro_name" ] && return

	local proc_version=$(cat /proc/version 2>&1)

	# todo: make this in a more efficient way
	if [[ "$proc_version" == *Ubuntu* ]]; then
		distro_name=ubuntu
	elif [[ "$proc_version" == *Debian* ]]; then
		distro_name=debian
	elif [[ "$proc_version" == *Redhat* ]]; then
		distro_name=redhat
	elif [[ "$proc_version" == *Fedora* ]]; then
		distro_name=fedora
	elif [[ "$proc_version" == *CentOS* ]]; then
		distro_name=redhat
	elif [[ "$proc_version" == *SuSE* ]]; then
		distro_name=suse
	elif [[ "$proc_version" == *ArchLinux* ]]; then
		distro_name=arch
	elif [[ "$proc_version" == *Gentoo* ]]; then
		distro_name=gentoo
	else
		distro_name=linux
	fi

}
