#!/bin/sh
[ -e /tmp/loader ] && . /tmp/loader

_log do check_settings daemon info "checking for relevant entrys ($( grep ^MAC= "$0" | wc -l ) overall)"

WIFIMAC="$( _sanitizer do "$( ip -o link show dev $LANDEV  )" mac hex )"
LANMAC="$(  _sanitizer do "$( ip -o link show dev $WIFIDEV )" mac hex )"

LINE="$( grep ^"MAC=$WIFIMAC" "$0" )" && eval "$LINE"	# MAC,NODE,PROFILE,ESSID,HOSTNAME
LINE="$( grep ^"MAC=$LANMAC"  "$0" )" && eval "$LINE"	# only one grep should work

if [ -z "$MAC" ]; then
	_log do mac_notinlist daemon alert "don't found $WIFIMAC or $LANMAC in list"
else
	_log do found_mac daemon info "found $MAC in '$( grep ^"MAC=$MAC" "$0" )'"

	[ "$( _system hostname )" != "$HOSTNAME" -a "$HOSTNAME" != "-" ] && {
		_log do hostname_set daemon info "just set to '$HOSTNAME', no further action needed"
		_profile set_system "HOSTNAME=$HOSTNAME"
		_nvram set commit "set new hostname '$HOSTNAME'"
		/etc/init.d/S53olsrd restart
		sleep 60			# get new routes
	}

	eval "$( _wifi params )"
	[ "$WIFI_ESSID" != "$ESSID" -a "$ESSID" != "-" ] && {
		_log do essid_set daemon info "setting new essid '$ESSID' and reinit wifi-dev"
		_profile set_wifi_phy "ESSID=$ESSID"
		_nvram set commit "set new ESSID '$ESSID'"
		_wifi phy_restart $WIFIDEV
	}

	[ "$( nvram get fff_profile )" != "$PROFILE" -a "$PROFILE" != "-" ] && {
		_log do profile_set daemon info "setting to '$PROFILE'"
		_nvram set fff_profile "$PROFILE"
		/etc/init.d/S02check_interface_correctness_fff+ enforce_new_profile
		_wifi phy_restart $WIFIDEV
		/etc/init.d/S53olsrd restart
		sleep 60
	}
fi

eval "$( grep ^"PASSWORD_SSH=" "$0" )"		# + PASSWORD_HTTP
[ -n "$PASSWORD_SSH" ] && {
	fgrep -q "root:${PASSWORD_SSH}:" /etc/passwd || {
		_log do ssh_password_set daemon info "setting new pass"

		sed -i "s#^\(root\):[^:]*:\(.*\)#\1:${PASSWORD_SSH}:\2#" /etc/passwd			

		_nvram set ff_pw "$PASSWORD_SSH"
		_nvram set commit "set new ssh-password"

		fgrep "$( nvram get ff_pw )" /var/etc/httpd.conf || {
			_log do httpd_restart daemon info "restarting httpd for using new pass"
			killall httpd
			/etc/init.d/S*httpd start
		}
	}
}

eval "$( grep ^"OPTIMIZE_NLQ=" "$0" )"		# + OPTIMIZE_NEIGH
[ -n "$OPTIMIZE_NLQ" ] && {
	[ "$( nvram get fff_optimize_neigh )" != "off" ] && {
		_sanitizer do "$( nvram get fff_optimize_neigh )" ip4 check || {	# do nothing on manual settings
			_nvram set fff_optimize_nlq   "$OPTIMIZE_NLQ"
			_nvram set fff_optimize_neigh "$OPTIMIZE_NEIGH"
			_nvram set commit "txpower-regulation to nlq/neigh: $OPTIMIZE_NLQ/$OPTIMIZE_NEIGH"	# fixme! no commit, if no change
		}
	}
}

eval "$( grep ^"CUSTOM=" "$0" )"
[ -n "$CUSTOM" ] && {
	_log do custom_command daemon info "executing '$CUSTOM'"
	sh -c ". /tmp/loader; $CUSTOM"
}

eval "$( grep ^"PMU=" "$0" )"					# + VERSION_MIN
[ -n "$PMU" ] && {						# later use: _firmware update_pmu $PMU
	[ "$FFF_PLUS_VERSION" -lt "$VERSION_MIN" ] && {

		nvram get wan_hostname | grep -q kasse && {
			ipkg remove libopenssl
			ipkg remove vtun
			ipkg remove zlib
			ipkg remove liblzo
		}

		if type _firmware >/dev/null && _firmware include && type _firmware_update_pmu >/dev/null; then
			_firmware update_pmu $PMU
		else
			_software make_free_space 350
			wget -O /tmp/tar.tgz "http://intercity-vpn.de/firmware/broadcom/images/$PMU/tarball.tgz"
			cd /
			tar xzf /tmp/tar.tgz
			rm /tmp/tar.tgz
			cd /tmp
			/etc/kalua_init
			[ -e "/www/placeholder_for_showing_that_software_is_fully_installed_fff+" ] && touch /www/SOFTWARE_FULLY_INSTALLED
			/etc/init.d/S51crond_fff+ stop
			/etc/init.d/S51crond_fff+ start
			echo "1" >/www/REBOOT_COUNTER
		fi
	}
}

cron.monitoring send_alive_message

exit 0

PASSWORD_SSH='$1$$KMMiw5xKCpOyNK7uW6imz.';PASSWORD_HTTP='$1$$KMMiw5xKCpOyNK7uW6imz.';
OPTIMIZE_NLQ='';OPTIMIZE_NEIGH='';
PMU='';VERSION_MIN='';
CUSTOM='';
