
OC_PARAMS="VERBOSE BOND WPASUPPLICANT WAITFOR OC_MODULES FIREWALL IFUPTIMEOUT KEYBOARDKEYSLOT REMOTEKEYSLOT KEYBOARDTIMEOUT DEVICELABEL DEVICETYPE MANUALWPA MANUALDHCP"
OC_KEY_PARAMS="REMOTEIP REMOTETIMEOUT REMOTEOPT REMOTEDIR REMOTEFILE LOCALFILE KEYID REMOTEMODE"

cryptlog() {
	if [ "x$CRYPTLOG" != "x" ]; then
		echo `date '+%b %d %H:%M:%S'` "$@" >> "$CRYPTLOG"
		[ "x$VERBOSE" != "x" ] && [ $VERBOSE -gt 1 ] && echo "$@"
	else
		echo "$@"
	fi
}

is_global_param() {
	local param test_param
	param="$1"
	for test_param in $OC_PARAMS ; do
		if [ "x$test_param" = "x$param" ]; then
			return 0
		fi
	done
	return 1
}

is_key_param() {
	local param test_param
	param="$1"
	for test_param in $OC_KEY_PARAMS ; do
		if [ "x$test_param" = "x$param" ]; then
			return 0
		fi
	done
	return 1
}

load_params() {
    local line param value test_param
	for param in $OC_PARAMS ; do
		unset $param
	done
    while read line ; do
		line=$(echo "$line" | sed -r 's/^ +// ; s/ +$// ; s/#.*$// ; s/^\[(SEARCH|KEY) .*\]//')
        if [ "x$line" != "x" ]; then
			param=$(echo "$line" | cut -d'=' -f1)
			value=$(echo "$line" | cut -d'=' -f2 | sed 's/^["]//;s/["]$//' | sed  "s/^[']//;s/[']$//")
			if is_global_param "$param" ; then
				eval "$param='$value'"
				if [ $? != 0 ]; then
					cryptlog "E: error evaluating parameter in '$line'"
					return 1
				fi
			elif ! is_key_param "$param" ; then
				if [ "$param" = "DEVICEDIR" ] ; then
					cryptlog "E: DEVICEDIR is no longer used"
					cryptlog "E: please use REMOTEDIR instead"
				else
					cryptlog "E: unknown parameter in '$line'"
				fi
				return 1
			fi
        fi
    done < $CONFFILE
	VERBOSE=$(echo "$VERBOSE" | sed 's/yes/1/;s/no/0/;s/[^0-9]//g')
	[ "x$VERBOSE" = "x" ] && VERBOSE=0
    if [ "x$BOND" != "xyes" ]; then
        BOND="no"
    fi
    if [ "x$WPASUPPLICANT" != "xyes" ]; then
        WPASUPPLICANT="no"
    fi
	KEYBOARDTIMEOUT=$(echo "$KEYBOARDTIMEOUT" | sed 's/[^0-9]//g')
	if [ "x$KEYBOARDTIMEOUT" = "x" ]; then
	   KEYBOARDTIMEOUT=10
	elif [ $KEYBOARDTIMEOUT -lt 10 ] ; then
	   KEYBOARDTIMEOUT=10
	fi
	IFUPTIMEOUT=$(echo "$IFUPTIMEOUT" | sed 's/[^0-9]//g')
	if [ "x$IFUPTIMEOUT" = "x" ]; then
	   IFUPTIMEOUT=30
	elif [ $IFUPTIMEOUT -lt 10 ] ; then
	   IFUPTIMEOUT=10
	fi
	return 0
}

unset_key_params() {
	local param
	for param in $OC_KEY_PARAMS ; do
		unset $param
	done
	KEYID="$SEARCHID"
	REMOTEMODE="device"
}

get_remote_modes() {
	local num alreadythere
    if [ "x$remote_modes_searched" = "x" ]; then
		for num in $(get_search_ids) ; do
			load_key_params "$num"
			if [ "x$REMOTEMODE" != "x" ]; then
				#alreadythere=$(echo "$remote_modes" | grep " $REMOTEMODE($| )")
				alreadythere=$(echo "$remote_modes" | grep " $REMOTEMODE ")
				if [ "x$alreadythere" = "x" ]; then
					#remote_modes="$remote_modes $REMOTEMODE"
					remote_modes="$remote_modes $REMOTEMODE "
				fi
			fi
		done
        remote_modes_searched="done"
    fi
    echo "$remote_modes"
}

used_remote() {
    local remote used
    remote="$1"
	# remote can be a regex
	used=$(get_remote_modes | grep -E " ($remote) ")
    [ "x$used" != "x" ] && return 0
    return 1
}

add_remote_mode() {
    local remote
    remote="$1"
	if ! used_remote "$remote" ; then
		remote_modes="$remote_modes $remote "
	fi
}

used_device_modes() {
	if used_remote "device" ; then
		return 0
	else
		return 1
	fi
}

used_network_modes() {
	if used_remote "ssh|wget|wget2|curl|http|arp" ; then
		return 0
	else
		return 1
	fi
}

get_search_ids() {
	local param num alreadythere
    if [ "x$search_ids_searched" = "x" ]; then
		while read param ; do
			param=$(echo "$param" | sed 's/#.*$//')
			if [ "x$param" != "x" ]; then
				num=$(echo "$param" | grep -E "^ *\[(SEARCH|KEY) .*\]" | sed -r 's/^ *\[(SEARCH|KEY) +// ; s/\].*$// ; s/\W/_/g')
				if [ "x$num" != "x" ]; then
					alreadythere=$(echo "$search_ids" | grep -E "(^| )$num($| )")
					if [ "x$alreadythere" = "x" ]; then
						search_ids="$search_ids $num"
					fi
				fi
			fi
		done < $CONFFILE
    	search_ids_searched="done"
	fi
	echo "$search_ids"
}

get_key_ids() {
	local param num alreadythere
    if [ "x$key_ids_searched" = "x" ]; then
		for num in $(get_search_ids) ; do
			load_key_params "$num"
			alreadythere=$(echo "$key_ids" | grep -E "(^| )$KEYID($| )")
			if [ "x$KEYID" != "x" -a "x$alreadythere" = "x" ]; then
				key_ids="$key_ids $KEYID"
			fi
		done
    	key_ids_searched="done"
	fi
	echo "$key_ids"
}

load_key_params() {
	local line param value found num test_param
	SEARCHID="$1"
	unset_key_params
	found="no"
	while read line ; do
		line=$(echo "$line" | sed 's/#.*$//')
		if [ "x$line" != "x" ]; then
			num=$(echo "$line" | grep -E "^ *\[(SEARCH|KEY) .*\]" | sed -r 's/^ *\[(SEARCH|KEY) // ; s/\].*$// ; s/\W/_/g')
			if [ "x$num" != "x" ]; then
            	if [ "$found" = "yes" ]; then
					# we use only the first block with the searched id
					normalize_parameters
					return 0
				fi
				if [ "$num" = "$SEARCHID" ]; then
					unset_key_params
					found="yes"
				else
					found="no"
				fi
            elif [ "$found" = "yes" ]; then
				param=$(echo "$line" | cut -d'=' -f1)
				value=$(echo "$line" | cut -d'=' -f2 | sed 's/^["]//;s/["]$//' | sed  "s/^[']//;s/[']$//")
				if is_key_param "$param" ; then
					eval "$param='$value'"
					if [ $? != 0 ]; then
						cryptlog "E: error evaluating parameter in '$line'"
						return 1
					fi
				fi
			fi
		fi
	done < $CONFFILE
	normalize_parameters
}

get_localfile() {
	local id suffix
	id="$1"
	id=$(echo "$id" | sed 's/\W/_/g')
	suffix="all"
	[ "x$MAP" != "x" ] && suffix="$MAP"
	echo "${LOCALPREFIX}.${id}.${MAP}"
}

normalize_parameters() {
	local check
	if [ "x$KEYID" = "x" ]; then
		KEYID="$SEARCHID"
	fi
	REMOTETIMEOUT=$(echo "$REMOTETIMEOUT" | sed 's/[^0-9]//g')
	if [ "x$REMOTETIMEOUT" = "x" ]; then
	   REMOTETIMEOUT=10
	elif [ $REMOTETIMEOUT -lt 10 ] ; then
	   REMOTETIMEOUT=10
	fi
	if [ "x$REMOTEMODE" = "x" ]; then
		REMOTEMODE="device"
	fi
	if [ "$REMOTEMODE" = "device" -a "x$REMOTEDIR" != "x" ]; then
		REMOTEDIR=$(echo "$REMOTEDIR" | sed 's/\/*$/\// ; s/^\/*//')
	fi
	if [ "x$REMOTEFILE" != "x" ]; then
        KEYFILE="$REMOTEFILE"
    else
		check=$(echo "$CRYPT" | grep -E '(UUID|LABEL)=')
		if [ "x$check" != "x" ]; then
			KEYFILE="$(echo "$CRYPT" | cut -d'=' -f2).key"
		else
			KEYFILE="$(echo "$CRYPT" | sed 's/.*\///').key"
		fi
    fi
	LOCALFILE=$(get_localfile "$KEYID")
}

is_remotemode_valid() {
	local mode=$1
    case "$mode" in
        device|ssh|wget|wget2|curl|http|arp)
            return 0
            ;;
    esac
	return 1
}

dhcp_used() {
	local interfaces interface i check
	interface="$1"
	if grep -qsE "^iface.+$interface.+dhcp" /etc/network/interfaces /etc/network/interfaces.d/* ; then
		return 0
	fi
	interfaces=$(ifquery --list --all --exclude lo)
	for i in $interfaces ; do
		if [ "x$interface" = "x" -o "$i" = "$interface" ]; then
			check=$(ifquery "$i" | grep -E "dhclient|udhcpc")
			if [ "x$check" != "x" ]; then
				return 0
			fi
		fi
	done
	return 1
}

