#!/bin/sh

if [ -e /usr/bin/ciplushelper ]; then
	rm /usr/bin/ciplushelper
fi
if [ -e /usr/bin/cihelper ]; then
	rm /usr/bin/cihelper
fi

if grep -qs 'getMachineBuild=hd2400\|getMachineBuild=hd1500\|getMachineBuild=triplex\|getMachineBuild=h6\|getMachineBuild=et8000\|getMachineBuild=et10000' cat /tmp/boxbranding.cfg ; then
	echo "Mutant/Triplex/H6/Xtrend found"
	if grep -qs 'getImageVersion=5.3\|getImageVersion=5.5' cat /tmp/boxbranding.cfg  ; then
		echo "Imageversion 5.x found"
		cp /tmp/mipsel32/5.x/usr/bin/ciplushelper /usr/bin/ciplushelper
		cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
	elif grep -qs 'getImageVersion=6\|getImageVersion=6.0\|getImageVersion=6.1\|getImageVersion=6.2\|getImageVersion=6.3\|getImageVersion=6.4\|getImageVersion=6.5\|getImageVersion=6.6\|getImageVersion=7.0\|getImageVersion=7.1\|getImageVersion=7.2\|getImageVersion=7.3' cat /tmp/boxbranding.cfg  ; then
		echo "Imageversion 6.x found"
		kernel_version=`uname -r`
		echo "kernel = $kernel_version"
		if [ ${kernel_version:0:3} == 4.1 ]  ; then
			echo "new ci helper for mutant/Triblex/H6/Xtrend"
			cp /tmp/mipsel32/6.x/usr/bin/ciplushelper_4.10.6 /usr/bin/ciplushelper
			cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
		else
			echo "old ci helper for mutant/Triplex"
			cp /tmp/mipsel32/6.x/usr/bin/ciplushelper /usr/bin/ciplushelper
			cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
		fi
	fi
	if true && type update-rc.d >/dev/null 2>/dev/null; then
		if [ -n "$D" ]; then
			OPT="-r $D"
		else
			OPT="-s"
		fi
		update-rc.d $OPT ciplushelper defaults 50
	fi
elif grep -qs 'getMachineBuild=hd51\|getMachineBuild=vs1500\|getMachineBuild=h7\|getMachineBuild=h9combo\|getMachineBuild=h9twin\|getMachineBuild=8100s\|getMachineBuild=h10\|getMachineBuild=hd61\|getMachineBuild=h9combose\|getMachineBuild=pulse4k\|getMachineBuild=pulse4kmini' cat /tmp/boxbranding.cfg ; then
	echo "hd51/vs1500/h7/h9combo/h9twin/8100s found"
	if grep -qs 'getImageVersion=5.3\|getImageVersion=5.5' cat /tmp/boxbranding.cfg  ; then
		echo "Imageversion 5.x found"
		cp /tmp/hd51/5.x/usr/bin/ciplushelper /usr/bin/ciplushelper
		cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
	elif grep -qs 'getImageVersion=6\|getImageVersion=6.0\|getImageVersion=6.1\|getImageVersion=6.2\|getImageVersion=6.3\|getImageVersion=6.4\|getImageVersion=6.5\|getImageVersion=6.6\|getImageVersion=7.0\|getImageVersion=7.1\|getImageVersion=7.2\|getImageVersion=7.3' cat /tmp/boxbranding.cfg  ; then
		echo "Imageversion 6.x found"
		kernel_version=`uname -r`
		echo "kernel = $kernel_version"
		if [ ${kernel_version:0:3} == 4.1 ] || [ ${kernel_version:0:3} == 4.4 ]; then
			echo "new ci helper for hd51/vs1500/h7/8100s/h9combo/h9twin/h10/hd61/pulse4k/pulse4kmini"
			cp /tmp/hd51/6.x/usr/bin/ciplushelper_4.10.6 /usr/bin/ciplushelper
			cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
		else
			echo "old ci helper for hd51/vs1500/h7/8100s"
			cp /tmp/hd51/6.x/usr/bin/ciplushelper /usr/bin/ciplushelper
			cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
		fi
	fi
	if true && type update-rc.d >/dev/null 2>/dev/null; then
		if [ -n "$D" ]; then
			OPT="-r $D"
		else
			OPT="-s"
		fi
		update-rc.d $OPT ciplushelper defaults 50
	fi
elif grep -qs 'getMachineBuild=cube' cat /tmp/boxbranding.cfg ; then
	echo "cube found"
	if grep -qs 'getImageVersion=6.1\|getImageVersion=6.2\|getImageVersion=6.3\|getImageVersion=6.4\|getImageVersion=6.5\|getImageVersion=6.6\|getImageVersion=7.0\|getImageVersion=7.1\|getImageVersion=7.2\|getImageVersion=7.3' cat /tmp/boxbranding.cfg  ; then
		echo "Imageversion 6.1 or newer found"
		cp /tmp/cube/6.1/usr/bin/ciplushelper /usr/bin/ciplushelper
		cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
	fi
	if true && type update-rc.d >/dev/null 2>/dev/null; then
		if [ -n "$D" ]; then
			OPT="-r $D"
		else
			OPT="-s"
		fi
		update-rc.d $OPT ciplushelper defaults 50
	fi
elif grep -qs 'getMachineBuild=formuler1\|getMachineBuild=formuler3\|getMachineBuild=formuler4' cat /tmp/boxbranding.cfg ; then
	if grep -qs 'getMachineBuild=formuler4turbo\|getMachineBuild=formuler1tc\|getMachineBuild=formuler3ip\|getMachineBuild=formuler4ip' cat /tmp/boxbranding.cfg ; then
		echo "found formuler4turbo/formuler1tc/formuler3ip/formuler4ip"
		else
		echo "Formuler1/3/4 found"
		if grep -qs 'getImageVersion=5.3\|getImageVersion=5.5' cat /tmp/boxbranding.cfg  ; then
			echo "Imageversion 5.x found"
		elif grep -qs 'getImageVersion=6\|getImageVersion=6.0\|getImageVersion=6.1\|getImageVersion=6.2\|getImageVersion=6.3\|getImageVersion=6.4\|getImageVersion=6.5\|getImageVersion=6.6\|getImageVersion=7.0\|getImageVersion=7.1\|getImageVersion=7.2\|getImageVersion=7.3' cat /tmp/boxbranding.cfg  ; then
			echo "Imageversion 6.x found"
			kernel_version=`uname -r`
			echo "kernel = $kernel_version"
			if [ ${kernel_version:0:3} == 4.1 ]  ; then
				echo "new ci helper for formuler 1/3/4"
				cp /tmp/mipsel32/6.x/usr/bin/ciplushelper_4.10.6 /usr/bin/ciplushelper
				cp /tmp/etc/init.d/ciplushelper /etc/init.d/ciplushelper
			else
				echo "old ci helper for formuler 1/3/4"
			fi
		fi
	fi
	if true && type update-rc.d >/dev/null 2>/dev/null; then
		if [ -n "$D" ]; then
			OPT="-r $D"
		else
			OPT="-s"
		fi
		update-rc.d $OPT ciplushelper defaults 50
	fi
fi

rm -rf /tmp/etc
rm -rf /tmp/gigablue
rm -rf /tmp/hd51
rm -rf /tmp/mipsel32
rm -rf /tmp/boxbranding.cfg

exit 0

