#!/bin/bash
####################################################################
# Prey Core Setup Routine - by Tomas Pollak (bootlog.org)
# URL: http://preyproject.com
# License: GPLv3
####################################################################

trap "" INT TERM
# trap cleanup EXIT

cleanup(){
	log " -- Cleaning up!\n"
	delete_tmpdir
}

error_exit(){
	log " !! $1"
	cleanup
	exit 1
}

show_usage(){
	echo -e "Usage: `basename $0` [options]\n"
	echo -e "Options:"
	echo -e "  -c | --check\t\tCheck mode. Checks if configuration is correctly set up."
	echo -e "  -l | --log\t\tLog output to ${base_path}/prey.log (default in Windows)."
	echo -e "  -s | --silent\t\tDon't log any output."
	echo -e "  -t | --test\t\tTest mode. Runs Prey without sending any data."
	echo -e "  -v | --version\tDisplay version."
	echo -e "\nFor more information and customization options, please check http://preyproject.com.\n"
}

show_version(){
	echo "Prey ${version}"
}

# step throught the params and check
until [ -z "$1" ]; do
	case "$1" in
		-t | --test )
			echo -e "\n -- TEST MODE ON."
			trap - INT # set off trap
			test_mode=1
			# the following lets us include stuff for specific tests
			# e.g. ./prey.sh --test reports
			if [ -z $(find_in $2 '-') ]; then
				. $base_path/test/include "$2" 2> /dev/null
				shift
			fi
		;;
		-c | --check )
			echo -e "\n -- CHECK MODE ON."
			check_mode=1
		;;
			-l | --log )
			logfile="$base_path/prey.log"
			echo -n "" > "$logfile" # empty the logfile first
			log_output=">> \"$logfile\""
		;;
			-s | --silent )
			log_output="&> /dev/null"
		;;
			-v | --version )
			show_version && exit
		;;
			-h | --help | * )
			show_usage && exit
	esac
	shift
done

get_os(){
	os=$(lowercase $(uname))

	if [ "$os" == "windowsnt" ]; then
		os=windows
	else # linux/mac stuff
		[ "$os" == "darwin" ] && os=mac
		readonly root_path='/'
		readonly home_path=$(eval echo ~)
		[ -t 1 ] && set_colors # only set color if running from terminal (not Cron)
	fi

	readonly os
	readonly platform_path="$base_path/platform/$os"
	PATH=$PATH:$platform_path/bin

}

# here we put the vars that are shared by two os, but a third one has
# different values
set_vars(){
	tmpbase="/tmp"
	line_breaker="\n"
	user_agent="-A Prey/$version ($os)"
	processes='ps ax'
}

set_aliases(){
	shopt -s expand_aliases
	alias getter="curl $curl_options -s \"$user_agent\""
	alias mailsender="sendEmail"
}

set_constants(){
	readonly lang
	readonly start_time=$(date +"%F %T")

	readonly config_file="$base_path/config"
	readonly tmpdir="$tmpbase/p${RANDOM}"
	readonly last_response="$tmpbase/prey-last-response.xml"
	readonly on_demand_pipefile="$tmpbase/prey-on-demand.pipe"
	readonly logged_user

	readonly control_panel_url="https://control.preyproject.com" 2> /dev/null
	modules_url="https://prey-bash-client-modules.s3.amazonaws.com" 2> /dev/null
	updates_url="https://s3.amasonaws.com/prey-releases/bash-client/patches" 2> /dev/null
}

set_colors(){
	cyan='\E[36m'
	green='\E[32m'
	red='\E[31m'
	color_end='\E[0m'
	bold='\033[1m'
	bold_end='\033[0m'
}

check_on_demand_status(){

	# we check if the pipe has been used to send pings in the last ten minutes
	# if not, then we assume the connection has been cut off
	# if the file doesn't exist, simply return

	if [ ! -f "$on_demand_pipefile" ]; then

		return 1

	elif test $(find "$on_demand_pipefile" -mmin +10); then

		echo " -- On Demand connection seems to have ended. Cleaning up and resetting..."
		. "$base_path/core/on_demand"

		[ -n "`is_process_running 'openssl'`" ] && kill_process 'openssl'
		on_demand_cleanup
		sleep 3 # just to make sure changes are reflected as they should

	fi

}

get_os
set_vars
set_aliases
. "$platform_path/settings"
set_constants
check_on_demand_status
