#!/bin/sh
# remove old lists
removeoldapt=0
removeoldipk=0
if [[ -f /etc/apt/sources.list.d/disneydream.list ]]; then
	rm /etc/apt/sources.list.d/disneydream.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/amazonprimedream.list ]]; then
	rm /etc/apt/sources.list.d/amazonprimedream.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/boxpiratesreader.list ]]; then
	rm /etc/apt/sources.list.d/boxpiratesreader.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/dazndream.list ]]; then
	rm /etc/apt/sources.list.d/dazndream.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/justwatch.list ]]; then
	rm /etc/apt/sources.list.d/justwatch.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/netflixdream.list ]]; then
	rm /etc/apt/sources.list.d/netflixdream.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/boxpiratespanel.list ]]; then
    rm -f /etc/apt/sources.list.d/boxpiratespanel.list
    removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/boxpiratescammanager.list ]]; then
    rm -f /etc/apt/sources.list.d/boxpiratescammanager.list
    removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/myflow.list ]]; then
	rm -f /etc/apt/sources.list.d/myflow.list
	removeoldapt=1
fi
if [[ -f /etc/apt/sources.list.d/plugins-boxpirates.list ]]; then
	if [[ $(grep "boxpirates.dynvpn.de" /etc/apt/sources.list.d/plugins-boxpirates.list|wc -l) -gt 0 ]]; then
		rm /etc/apt/sources.list.d/plugins-boxpirates.list
		removeoldapt=1
	fi
fi
if [[ -f /etc/opkg/plugins-boxpirates.conf ]]; then
	if [[ $(grep "boxpirates.dynvpn.de" /etc/opkg/plugins-boxpirates.conf|wc -l) -gt 0 ]]; then
		rm /etc/opkg/plugins-boxpirates.conf
		removeoldapt=1
	fi
fi
if [[ -f /etc/opkg/boxpiratespanel-feed.conf ]]; then
    rm -f /etc/opkg/boxpiratespanel-feed.conf
    removeoldipk=1
fi
if [[ -f /etc/opkg/boxpiratescammanager-feed.conf ]]; then
    rm -f /etc/opkg/boxpiratescammanager-feed.conf
    removeoldipk=1
fi
if [[ -f /etc/opkg/boxpirates-feed.conf ]]; then
    rm -f /etc/opkg/boxpirates-feed.conf
    removeoldipk=1
fi

if [[ -d /etc/apt ]]; then
	echo box is deb-type
	if [[ $(uname -m) == "aarch64" ]]; then
		echo box is aarch64
		if [[ -f /etc/apt/sources.list.d/plugins-boxpirates.list ]]; then
			if [[ "$1" == "remove" ]]; then
				rm /etc/apt/sources.list.d/plugins-boxpirates.list
				echo feed removed
			else
				echo feed already installed
				if [[ "$1" == "with-update" ]] && [[ "$removeoldapt" == 1 ]]; then
					apt-get update
					echo apt-get update executed
				fi
			fi
		else
			if [[ "$1" == "remove" ]]; then
				echo feed not installed
			else
    			echo deb [[trusted=yes]] http://plugins.boxpirates.to/apt64/ ./ > /etc/apt/sources.list.d/plugins-boxpirates.list
    			echo feed installed
    			if [[ "$1" == "with-update" ]]; then
					apt-get update
					echo apt-get update executed
				fi
			fi
    	fi
	else
		echo box is not aarch64
		if [[ -f /etc/apt/sources.list.d/plugins-boxpirates.list ]]; then
			if [[ "$1" == "remove" ]]; then
				rm /etc/apt/sources.list.d/plugins-boxpirates.list
				echo feed removed
			else
				echo feed already installed
				if [[ "$1" == "with-update" ]] && [[ "$removeoldapt" == 1 ]]; then
					apt-get update
					echo apt-get update executed
				fi
			fi
		else
			if [[ "$1" == "remove" ]]; then
				echo feed not installed
			else
    			echo deb [[trusted=yes]] http://plugins.boxpirates.to/apt/ ./ > /etc/apt/sources.list.d/plugins-boxpirates.list
    			echo feed installed
    			if [[ "$1" == "with-update" ]]; then
					apt-get update
					echo apt-get update executed
				fi
			fi
    	fi
	fi
elif [[ -d /etc/opkg ]]; then
	echo box is ipk-type
	if [[ -f /etc/opkg/plugins-boxpirates.conf ]]; then
		if [[ $(cat /etc/opkg/plugins-boxpirates.conf) != *"arm"* ]] && [[ $(cat /etc/opkg/plugins-boxpirates.conf) != *"mips"* ]];then
			rm /etc/opkg/plugins-boxpirates.conf
		fi
	fi
	if [[ -f /etc/opkg/plugins-boxpirates.conf ]]; then
		if [[ "$1" == "remove" ]]; then
			rm /etc/opkg/plugins-boxpirates.conf
			echo feed remove
		else
			echo feed already installed
			if [[ "$1" == "with-update" ]] && [[ "$removeoldipk" == 1 ]]; then
				opkg update
				echo opkg update executed
			fi
		fi
	else
		if [[ "$1" == "remove" ]]; then
			echo feed not installed
		else
			if [[ $(uname -m) == *"mips"* ]]; then
				echo box is mips-type
				echo "src/gz boxpiratesplugins_mips http://plugins.boxpirates.to/ipk/mips" > /etc/opkg/plugins-boxpirates.conf
				echo "src/gz boxpiratesplugins_all http://plugins.boxpirates.to/ipk/all" >> /etc/opkg/plugins-boxpirates.conf
			else
				echo box is arm-type
				echo "src/gz boxpiratesplugins_arm http://plugins.boxpirates.to/ipk/arm" > /etc/opkg/plugins-boxpirates.conf
				echo "src/gz boxpiratesplugins_all http://plugins.boxpirates.to/ipk/all" >> /etc/opkg/plugins-boxpirates.conf
			fi
			echo feed installed
			if [[ "$1" == "with-update" ]]; then
				opkg update
				echo opkg update executed
			fi
		fi
	fi
else
	echo nothing done
fi	
exit 0
