#!/bin/sh

# Got args when install:
# -v /tmpData
# $0: /tmpData/upd@te/updater
# ls /tmpData: @autoupdate, upd@te, .noroot
# pwd: /usr/syno/web/webman

# Got args when update:
# -p -l rus		! On this stage current dir (pwd) = /
# -k pre
# -v /
# -k post

logtoconsole () {
	echo "UPDATER: $1" > /dev/console
}

logtoconsole "Updater started..."
logtoconsole "Got params: `for arg in $@; do /bin/echo -en \"$arg \"; done`"

case "$1" in
	"-p")
		logtoconsole "Prepare stage."
	;;
	"-k")
		BACKUP_DIRS="/etc /root/.ssh /usr/local /usr/syno/etc /usr/syno/selfcheck/packages /usr/syno/synoman/webman/3rdparty /var"
		REMOVE_DIRS="/etc/apparmor.d /etc/avahi /etc/cups/cupsd.conf /etc/dbus-1 /etc/dhclient/dhclient-script /etc/dhclient/ipv4/dhclient.conf /etc/dhclient/ipv6/dhclient.conf /etc/ftpusers /etc/fw_security/sysconf /etc/hostapd/mac_filter/mfscript.sh /etc/hostapd/stainfo.sh /etc/httpd/conf /etc/ImageMagick-6 /etc/init /etc/inputrc /etc/iproute2/script /etc/ld.so.preload /etc/logrotate.conf /etc/logrotate.d /etc/lvm/lvm.conf /etc/nginx /etc/pam.d /etc/pam.d/samba /etc/php/conf.d/opcache.ini /etc/php/php-fpm.conf /etc/php/php.ini /etc/postgresql /etc/ppp/ip-down /etc/ppp/ip-function /etc/ppp/ip-up /etc/ppp/ipv6-down /etc/ppp/ipv6-up /etc/ppp/options /etc/profile /etc/rc /etc/rc.network /etc/rc.network_routing /etc/ssl/certs /etc/syslog-ng /etc/tc/default.cmd /etc/vimrc /etc/VERSION etc/synoinfo.conf /etc/wide-dhcpv6 /usr/local/bin/python* /usr/local/cups/backend /usr/local/cups/cupsd.conf /usr/local/cups/filter/bannertops /usr/local/cups/filter/commandtops /usr/local/cups/filter/gziptoany /usr/local/cups/filter/hpgltops /usr/local/cups/filter/imagetops /usr/local/cups/filter/imagetoraster /usr/local/cups/filter/pdftops /usr/local/cups/filter/pstops /usr/local/cups/filter/rastertoepson /usr/local/cups/filter/rastertohp /usr/local/cups/filter/rastertolabel /usr/local/cups/filter/rastertopwg /usr/local/cups/filter/texttops /usr/local/cups/mime /usr/local/cups/mime.convs /usr/local/cups/mime.types /usr/local/cups/testprint /usr/local/etc/rc.d/SynoEnablePersonalServices.sh /usr/syno/etc/.htpasswd /usr/syno/etc/afpd.conf /usr/syno/etc/bluetooth/ /usr/syno/etc/cacert.pem /usr/syno/etc/dbus-1 /usr/syno/etc/iptables_chain_list /usr/syno/etc/iptables_guest_net.sh /usr/syno/etc/iptables_modules_list /usr/syno/etc/iptables_time_ctrl.sh /usr/syno/etc/lpd/lpd.conf /usr/syno/etc/printcap /usr/syno/etc/rc.atalk /usr/syno/etc/rc.d /usr/syno/etc/rc.tun /usr/syno/etc/rfkill.sh /usr/syno/etc/smallupdate_patch /usr/syno/etc/ssdp/dsm_desc.xml /usr/syno/etc/ssl/mkcert.sh /usr/syno/etc/ssl/mkcgikey.sh /usr/syno/etc/synoservice.d /usr/syno/etc/synoservice.d/global.setting /usr/syno/etc/synoservice.d/system.cfg /usr/syno/etc/synovpnclient/scripts /usr/syno/etc/ups /usr/syno/etc/ups.conf /usr/syno/etc/upsd.conf /usr/syno/etc/upsd.users /usr/syno/etc/upsmon.conf /usr/syno/etc/upssched.conf /usr/syno/etc/usbmodem/usb_modeswitch.d/* /usr/syno/etc/usbmodem/wcdma_list.json /usr/syno/etc/vfs /usr/syno/etc/www/app.d /usr/syno/etc/www/SSLProfile.json /var/dynlib /var/lib/apparmor /var/lib/dpkg /var/run /var/spool/postfix /var/state/ups"
		case $2 in
			"pre")
				logtoconsole "Preinstall stage started..."

				# Some shit from initrd/upgrade.sh
				for dir in $BACKUP_DIRS; do
					mkdir -p /.upd@te_sub/$dir
					mv $dir /.upd@te_sub/$dir
				done
				for dir in $REMOVE_DIRS; do
					rm -rf /.upd@te_sub/$dir
				done
			;;
			"post")
				logtoconsole "Postinstall stage started..."
				mv -r /.upd@te_sub/* /
				sync
				reboot -f
			;;
		esac
	;;
	"-v")
		ROOTFS="${2}/upd@te/hda1.tgz"
		KERNEL="${2}/upd@te/uImage"
		INITRD="${2}/upd@te/uRamdisk"
		CONFIG="${2}/upd@te/synoinfo.conf"
		INSDIR="${2}"

		if [ -e $ROOTFS ]; then
			logtoconsole "Found hda1.tgz. Installing..."
			tar xvf $ROOTFS -C $INSDIR
		fi

		if [ -e $KERNEL ]; then
			logtoconsole "Found uImage. Installing..."
			dd if=/dev/zero of=/dev/mtdblock1
			dd if=$KERNEL of=/dev/mtdblock1
		fi

		if [ -e $INITRD ]; then
			logtoconsole "Found uRamdisk. Installing..."
			dd if=/dev/zero of=/dev/mtdblock2
			dd if=$INITRD of=/dev/mtdblock2
		fi

		if [ -e $CONFIG ]; then
			logtoconsole "Found synoinfo. Installing..."
			# TODO: Use sed/awk for replace strings in this file.
			cp $CONFIG $INSDIR/etc.defaults/
		fi

		if [ -f $INSDIR/.noroot ]; then
			rm $INSDIR/.noroot
		fi
	;;
esac

logtoconsole "Done."
