#!/bin/bash

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

	echo "Start upgrade..."

	rm -f /etc/init.d/upgrade_start

	s="#!/bin/bash\n"
	s+="\n"
	s+="if [ \`grep -i \"RELEASE='tactical'\" /etc/environment | wc -l\` == '0' ]; then\n"
	s+="\t        export RELEASE=\"appliance\"\n"
	s+="else\n"
	s+="\t        export RELEASE=\"tactical\"\n"
	s+="fi\n"
	s+="\n"
	s+="file='/opt/td-config/share/NetworkInjectorUpgrade.deb'\n"
	s+="\n"
	s+="if [ -f \$file ]; then\n"
	s+="\t        if [ \`echo \$USER\` != 'root' ]; then\n"
	s+="\t\t                echo \"Can't initialize: Permission denied (you must be root)\"\n"
	s+="\t\t                exit 1\n"
	s+="\t        else\n"
	s+="\t\t                nia='false'\n"
	s+="\t\t                nia_start='false'\n"
	s+="\n"
	s+="\t\t                if [ \`echo \$RELEASE\` == 'appliance' ]; then\n"
	s+="\t\t\t                        if [ \`ls -l /etc/rc?.d/ | grep -i nia | wc -l\` != '0' ]; then\n"
	s+="\t\t\t\t                                nia='true'\n"
	s+="\t\t\t\t                                update-rc.d -f nia remove\n"
	s+="\t\t\t                        fi\n"
	s+="\n"
	s+="\t\t\t                        if [ \`ps ax | grep -i RCSRedirect | grep -v grep | wc -l\` != '0' ]; then\n"
	s+="\t\t\t\t                                if [ \`grep sniffing_iface /opt/td-config/share/rcsredirect.conf | grep -v channel | cut -f 2 -d \\\"\` != 'lo' ]; then\n"
	s+="\t\t\t\t\t                                        nia_start='true'\n"
	s+="\t\t\t\t                                fi\n"
	s+="\t\t\t\t                                pkill -KILL -f infect_app.py 2> /dev/null\n"
	s+="\t\t\t\t                                /etc/init.d/nia stop\n"
	s+="\t\t\t                        fi\n"
	s+="\t\t                fi\n"
	s+="\n"
	s+="\t\t                rm -Rf /tmp/upgrade/\n"
	s+="\t\t                mkdir /tmp/upgrade/\n"
	s+="\t\t                mv /opt/td-config/share/NetworkInjectorUpgrade.deb /tmp/upgrade/\n"
	s+="\t\t                mv /opt/td-config/share/ /tmp/upgrade/\n"
	s+="\t\t                cd\n"
	s+="\n"
	s+="\t\t                rm -f /tmp/upgrade.log 2> /dev/null\n"
	s+="\t\t                date > /tmp/upgrade.log\n"
	s+="\t\t                gdebi -n -q /tmp/upgrade/NetworkInjectorUpgrade.deb >> /tmp/upgrade.log 2>> /tmp/upgrade.log\n"
	s+="\t\t                rc=\$?\n"
	s+="\n"
	s+="\t\t                if [[ \$rc != 0 ]]; then\n"
	s+="\t\t\t                        mv /tmp/upgrade/share/ /opt/td-config/share/\n"
	s+="\t\t\t                        mv /tmp/upgrade/NetworkInjectorUpgrade.deb /opt/td-config/share/\n"
	s+="\t\t\t                        rm -Rf /tmp/upgrade/\n"
	s+="\n"
	s+="\t\t\t                        if [ \`echo \$RELEASE\` == 'tactical' ]; then\n"
	s+="\t\t\t\t                                /opt/td-config/bin/tcc-config false &\n"
	s+="\t\t\t                        else\n"
	s+="\t\t\t\t                                /opt/td-config/bin/acc-config false &\n"
	s+="\t\t\t                        fi\n"
	s+="\n"
	s+="\t\t\t                        exit 1\n"	
	s+="\t\t                fi\n"
	s+="\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 [ \`echo \$RELEASE\` == 'appliance' ]; then\n"
	s+="\t\t\t                        if [ \$nia_start == 'true' ]; then\n"
	s+="\t\t\t\t                                /etc/init.d/nia start\n"
	s+="\t\t\t\t                                /opt/td-config/scripts/infect_app.py &\n"
	s+="\t\t\t                        fi\n"
	s+="\n"
	s+="\t\t\t                        if [ \$nia == 'true' ]; then\n"
	s+="\t\t\t\t                                update-rc.d nia defaults\n"
	s+="\t\t\t                        fi\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                if [ \`echo \$RELEASE\` == 'tactical' ]; then\n"
	s+="\t\t\t                        /opt/td-config/bin/tcc-config true &\n"
	s+="\t\t                else\n"
	s+="\t\t\t                        /opt/td-config/bin/acc-config true &\n"
	s+="\t\t                fi\n"
	s+="\n"
	s+="\t\t                exit 0\n"
	s+="\t        fi\n"
	s+="else\n"
	s+="\t        echo \"Error: Network Injector update is not available.\"\n" 
	s+="\t        exit 1\n"
	s+="fi"

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

	echo "End upgrade."

	exit 0
else
	echo "Error: Network Injector update is not available." 

	exit 1
fi
