#!/bin/sh

# set time
[ ! -e /etc/crontabs ] && mkdir /etc/crontabs
crontab=/etc/crontabs/root
[ ! -e $crontab ] && touch $crontab
[ ! -e /var/spool/cron/crontabs ] && mkdir -p /var/spool/cron/crontabs

#check and update bzImage
checkAndUpdateImage() {
	local newImage=/boot/bzImage
	#current bzImage version
	local tosloader=/boot/.tosloader
	local usbRoot=/mnt/bootdev
	if [ ! -e $tosloader ]; then
		return
	fi
	#usb bzImage version
	local usbloader=$usbRoot/boot/.tosloader
	if [ ! -e $usbloader ]; then
		updateImage $usbRoot $newImage
		return
	fi
	#V4, V5, V6, ...
	local newSlice1=$(awk -F'.' '{print $1}' $tosloader)
	local slice1=$(awk -F'.' '{print $1}' $usbloader)
	[ "$newSlice1" \< "$slice1" ] && return 1
	if [ "$newSlice1" \> "$slice1" ]; then
		updateImage $usbRoot $newImage
		return 0
	fi
	# number compare
	local newSlice2=$(awk -F'.' '{print $2}' $tosloader)
	local slice2=$(awk -F'.' '{print $2}' $usbloader)
	[ "$newSlice2" -lt "$slice2" ] && return 1
	if [ "$newSlice2" -gt "$slice2" ]; then
		updateImage $usbRoot $newImage
		return 0
	fi
	local newSlice3=$(awk -F'.' '{print $3}' $tosloader)
	local slice3=$(awk -F'.' '{print $3}' $usbloader)
	if [ "$newSlice3" -lt "$slice3" ]; then
		return 1
	fi
	# install package is newer
	updateImage $usbRoot $newImage
	return 0
}

updateImage() {
	local updateRoot=$1
	local newImage=$2
	local md5_old=$(md5sum $updateRoot/boot/bzImage | awk {'print $1'})
	local md5_new=$(md5sum $newImage | awk {'print $1'})
	if [ "${md5_old}" != "${md5_new}" ]; then
		chattr -i $updateRoot/boot/bzImage $updateRoot/boot/.tosloader
		cp -a $newImage $updateRoot/boot/bzImage
		cp -a /boot/.tosloader $updateRoot/boot/.tosloader
		if [ -d $updateRoot/boot/grub2 ]; then
			cp /boot/grub/grub.cfg $updateRoot/boot/grub2/ -a
		fi
		if [ -d $updateRoot/boot/grub ]; then
			cp /boot/grub/grub.cfg $updateRoot/boot/grub/ -a
		fi
		sync
		chattr +i $updateRoot/boot/bzImage $updateRoot/boot/.tosloader
	fi
}

# update disk u:
if [ -e /.eraseAcl ]; then
	rm -fr /.eraseAcl
	#check usb free space
	/usr/sbin/checkusb /boot/bzImage >/var/log/checkusb.log
	if [ $? -eq 0 ]; then
		checkAndUpdateImage
	fi
	/etc/init.d/mountusb stop >>/var/log/checkusb.log
fi
# set .default.sys
DEFAULTSYS="/etc/.default.sys"
TIMESERVER="pool.ntp.org"
if [ -f ${DEFAULTSYS} ]; then
	TIMESERVER=$(iniparse -c -f ${DEFAULTSYS} -s system -k timeserver)
	# Fan ctrl
	Autofan=$(iniparse -c -f ${DEFAULTSYS} -s system -k Autofan)
	Fanspeed=$(iniparse -c -f ${DEFAULTSYS} -s system -k Fanspeed)
	if [ "X${Autofan}" = "X0" ]; then
		if [ "X$(dmidecode | grep retsamarret)" != "X" ]; then
			#1900
			if [ "X${Fanspeed}" = "X0" ]; then
				ec_stat -w 105
			elif [ "X${Fanspeed}" = "X4" ]; then
				ec_stat -w 180
			elif [ "X${Fanspeed}" = "X8" ]; then
				ec_stat -w 254
			fi
		else
			#2538
			if [ "X${Fanspeed}" = "X0" ]; then
				ec_status -w 105
			elif [ "X${Fanspeed}" = "X4" ]; then
				ec_status -w 180
			elif [ "X${Fanspeed}" = "X8" ]; then
				ec_status -w 254
			fi
		fi
	fi
fi
ntpdate -b ${TIMESERVER}
hwclock -w

# powerlast ...
if [ -f /etc/tos/scripts/powerlast ]; then
	/etc/tos/scripts/powerlast
fi
# powertimer ...
if [ -f /etc/tos/scripts/powertimer ]; then
	/etc/tos/scripts/powertimer
fi
# encrypt /etc/email to /etc/email.ecrypt
if [ -f /etc/email ]; then
	/usr/sbin/email_helper --act='enc' --in='/etc/email' --out='/etc/email.encrypt'
	rm -f /etc/email
fi
# spindown ...
if [ -f /etc/tos/scripts/spindown ]; then
	/etc/tos/scripts/spindown >/dev/null &
fi

# remotefolder ...
if [ -f /etc/tos/scripts/remotefolder ]; then
	/etc/tos/scripts/remotefolder mountall >/dev/null &
fi

# clean the update dir
rm -fr /etc/base/update >/dev/null &
