#!/bin/sh
# created by Lizard - Installer for OE1.6 / OE2.0
if [ -f /etc/opkg/boxpiratespanel-feed.conf ]; then
    rm -f /etc/opkg/boxpiratespanel-feed.conf
fi
if [ -f /etc/opkg/boxpiratescammanager-feed.conf ]; then
    rm -f /etc/opkg/boxpiratescammanager-feed.conf
fi
if [ -f /etc/opkg/boxpirates-feed.conf ]; then
    rm -f /etc/opkg/boxpirates-feed.conf
fi
if [ $(opkg list-installed enigma2-plugin-extensions-boxpirates-panel|grep -c "enigma2-plugin-extensions-boxpirates-panel") == 1 ]; then
    opkg remove enigma2-plugin-extensions-boxpirates-panel
    if [ -d /usr/lib/enigma2/python/Plugins/Extensions/BoxPirates_Panel ]; then
        rm -rf /usr/lib/enigma2/python/Plugins/Extensions/BoxPirates_Panel
    fi
fi
opkg update
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
opkg update
if [ -f /etc/image-version ]; then
	echo "... file image-version exists"
	reseller=$(cat /etc/image-version | grep creator | cut -d"=" -f2)
	reseller=${reseller,,}
	imageversion=$(cat /etc/image-version | grep imageversion | cut -d"=" -f2)
	if [[ $reseller == *"openatv"* ]] && [[ $imageversion == "7.0" ]]; then
		echo "... Image is OpenATV 7.0"
		if [ $(opkg list-installed libssl0.9.8|grep -c "libssl0.9.8") == 0 ]; then
			echo "... install optional libssl"
			opkg install libssl0.9.8
		else
			echo "... optional libssl already installed"
		fi
		if [ ! -L /usr/lib/libcrypto.so.1.0.0 ] && [ -f /lib/libcrypto.so.0.9.8 ]; then
			ln -s /lib/libcrypto.so.0.9.8 /usr/lib/libcrypto.so.1.0.0
			echo "... libcrypto symlink (option 1) created"
		elif [ ! -L /usr/lib/libcrypto.so.1.0.0 ] && [ -f /usr/lib/libcrypto.so.0.9.8 ]; then
			ln -s /usr/lib/libcrypto.so.0.9.8 /usr/lib/libcrypto.so.1.0.0
			echo "... libcrypto symlink (option 2) created"
		else
			echo "... no libcrypto symlink created, list current symlinks:"
			echo "... /usr/lib directory"
			ls -l /usr/lib/libcrypto* 2> /dev/null
			echo "... /lib directory"
			ls -l /lib/libcrypto* 2> /dev/null
		fi
		if [ ! -L /usr/lib/libssl.so.1.0.0 ] && [ -f /lib/libssl.so.0.9.8 ]; then
			ln -s /lib/libssl.so.0.9.8 /usr/lib/libssl.so.1.0.0
			echo "... libssl symlink (option 1) created"
		elif [ ! -L /usr/lib/libssl.so.1.0.0 ] && [ -f /usr/lib/libssl.so.0.9.8 ]; then
			ln -s /usr/lib/libssl.so.0.9.8 /usr/lib/libssl.so.1.0.0
			echo "... libssl symlink (option 2) created"
		else
			echo "... no libssl symlink created, list current symlinks:"
			echo "... /usr/lib directory"
			ls -l /usr/lib/libssl* 2> /dev/null
			echo "... /lib directory"
			ls -l /lib/libssl* 2> /dev/null
		fi
	else
		echo "reseller: "$reseller
		echo "imageversion: "$imageversion
	fi
else
	echo "... file image-version not exists"
fi
opkg install enigma2-plugin-extensions-boxpirates-cammanager --force-depends
sleep 3
echo "... please restart gui"

