#!/bin/bash

while true; do
	if [ -f '/opt/td-config/share/NetworkInjectorUpgrade.deb' ]; then
		if [ `ps aux | grep -i autoupgrade_start | grep -v grep | wc -l` != '0' ]; then
			echo "Upgrade already running..."

			sleep 5
			continue
		fi

		if [ `ps ax | grep -i tcc-config.py | grep -v grep | wc -l` != '0' ]; then
			echo "Manual upgrade already running..."

                        sleep 5
                        continue
		fi

		echo "Start upgrade..."

	       	rm -f /etc/init.d/autoupgrade_start

		s="#!/bin/bash\n"
		s+="\n"
		s+="export RELEASE=\"appliance\"\n"
		s+="\n"
		s+="file='/opt/td-config/share/NetworkInjectorUpgrade.deb'\n"
		s+="\n"
		s+="while true; do\n"
		s+="\t        if [ \`ps ax | grep -i tcc-config.py | grep -v grep | wc -l\` != '0' ]; then\n"
		s+="\t\t                sleep 5\n"
		s+="\t\t                continue\n"
		s+="\t        fi\n"
		s+="\n"
		s+="\t        if [ -f \$file ]; then\n"
		s+="\t\t                nia='false'\n"
		s+="\t\t                nia_start='false'\n"
		s+="\n"
		s+="\t\t                if [ \`ls -l /etc/rc?.d/ | grep -i nia | wc -l\` != '0' ]; then\n"
		s+="\t\t\t                        nia='true'\n"
		s+="\t\t\t                        update-rc.d -f nia remove\n"
		s+="\t\t                fi\n"
		s+="\n"
		s+="\t\t                if [ \`ps ax | grep -i RCSRedirect | grep -v grep | wc -l\` != '0' ]; then\n"
		s+="\t\t\t                        nia_start='true'\n"
		s+="\t\t\t                        pkill -KILL -f infect_app.py 2> /dev/null\n"
		s+="\t\t\t                        /etc/init.d/nia stop\n"
		s+="\t\t                fi\n"
		s+="\n"
		s+="\t\t                while true; do\n"
		s+="\t\t\t                        if [ \`ps ax | grep -i tcc-config.py | grep -v grep | wc -l\` != '0' ]; then\n"
		s+="\t\t\t\t                                sleep 5\n"
		s+="\t\t\t\t                                continue\n"
		s+="\t\t\t                        fi\n"
		s+="\n"
		s+="\t\t\t                        rm -Rf /tmp/upgrade/\n"
		s+="\t\t\t                        mkdir /tmp/upgrade/\n"
		s+="\t\t\t                        mv /opt/td-config/share/NetworkInjectorUpgrade.deb /tmp/upgrade/\n"
		s+="\t\t\t                        mv /opt/td-config/share/ /tmp/upgrade/\n"
		s+="\t\t\t                        mv /opt/td-config/run/ /tmp/upgrade/\n"
		s+="\n"
		s+="\t\t\t                        cd\n"
		s+="\n"
		s+="\t\t\t                        rm -f /tmp/upgrade.log 2> /dev/null\n"
		s+="\t\t\t                        date > /tmp/upgrade.log\n"
		s+="\t\t\t                        gdebi -n -q /tmp/upgrade/NetworkInjectorUpgrade.deb >> /tmp/upgrade.log 2>> /tmp/upgrade.log\n"
		s+="\t\t\t                        rc=\$?\n"
		s+="\n"
		s+="\t\t\t                        if [[ \$rc != 0 ]]; then\n"
		s+="\t\t\t\t                                mv /tmp/upgrade/run/ /opt/td-config/run/\n"
		s+="\t\t\t\t                                mv /tmp/upgrade/share/ /opt/td-config/share/\n"
		s+="\t\t\t\t                                mv /tmp/upgrade/NetworkInjectorUpgrade.deb /opt/td-config/share/\n"
		s+="\t\t\t\t                                rm -Rf /tmp/upgrade/\n"
		s+="\n"
		s+="\t\t\t\t                                sleep 5\n"
		s+="\t\t\t\t                                continue\n"
		s+="\t\t\t                        fi\n"
		s+="\n"
		s+="\t\t\t                        break\n"
		s+="\t\t                done\n"
		s+="\n"
		s+="\t\t                rm -Rf /opt/td-config/run/\n"
		s+="\t\t                mv /tmp/upgrade/run/ /opt/td-config/\n"
		s+="\t\t                rm -Rf /opt/td-config/share/\n"
		s+="\t\t                mv /tmp/upgrade/share/ /opt/td-config/\n"
		s+="\n"
		s+="\t\t                if [ \$nia_start == 'true' ]; then\n"
		s+="\t\t\t                        /etc/init.d/nia start\n"
		s+="\t\t\t                        /opt/td-config/scripts/infect_app.py &\n"
		s+="\t\t                fi\n"
		s+="\n"
		s+="\t\t                if [ \$nia == 'true' ]; then\n"
		s+="\t\t\t                        update-rc.d nia defaults\n"
		s+="\t\t                fi\n"
		s+="\n"
	        s+="\t\t                while true; do\n"
	        s+="\t\t\t                        /etc/init.d/update_block\n"
	        s+="\t\t\t                        rc=\$?\n"
	        s+="\n"
	        s+="\t\t\t                        if [[ \$rc != 0 ]]; then\n"
	        s+="\t\t\t\t                                continue\n"
	        s+="\t\t\t                        else\n"
	        s+="\t\t\t\t                                break\n"
	        s+="\t\t\t                        fi\n"
	        s+="\t\t                done\n"
	        s+="\n"
		s+="\t\t                /etc/init.d/startupgrade start\n"
		s+="\t\t                exit 0\n"
		s+="\t        else\n"
		s+="\t\t                sleep 5\n"
		s+="\t        fi\n"
		s+="done"

		echo -e $s > /etc/init.d/autoupgrade_start
	       	chmod +x /etc/init.d/autoupgrade_start
	       	/etc/init.d/autoupgrade_start &

		echo "End upgrade."

		exit 0
	else
		sleep 5
	fi
done
