#!/bin/bash

VERSION=$(uname -r)

function compile_for_519
{
	cd /usr/src/promethean/kernel
	patch -f usbpromethean.c < /usr/share/zero-lliurex-promethean/patches/01_remove_doexit.patch
	patch -f usbpromethean.c < /usr/share/zero-lliurex-promethean/patches/02_fix_usbmaxpacket.patch
	cd $OLDPWD	
	compile_for_5x
}
function compile_for_517
{
	cd /usr/src/promethean/kernel
	patch -f usbpromethean.c < /usr/share/zero-lliurex-promethean/patches/01_remove_doexit.patch
	cd $OLDPWD	
	compile_for_5x
}
function compile_for_5x
{
	cd /usr/src/promethean/kernel
	sed -i 's/SUBDIRS/M/g' /usr/src/promethean/kernel/Makefile
	make -C /lib/modules/${VERSION}/build M=$PWD clean
	cp /usr/src/linux-headers-${VERSION}/Module.symvers /usr/src/promethean/kernel
	make -C /lib/modules/${VERSION}/build M=$PWD
	#Control build return
	sed -i 's/\(Error building driver.*\)/\0\n\t\texit 1/g' b
	./b
	modprobe promethean
	if [ $? -ne 0 ] 
	then
	#Something went wrong. Last chance: Reinstall headers and sources
		apt-get remove --purge -y linux-headers-$VERSION
		#[ -d /usr/src/linux-headers-$VERSION/ ] && rm -fr /usr/src/linux-headers-$VERSION 
		HWE_VER=$(echo ${VERSION/-*/} | cut -d "." -f1-2)
		HDR_VER=$(echo ${VERSION} | cut -d "-" -f1-2)
		apt-get install -y --reinstall linux-hwe-$HWE_VER-headers-$HDR_VER linux-headers-$VERSION 
		./b
	fi
	cd $OLDPWD	
	exit 0
}

function fix_activinspire_pkg_llx23
{
	rm activinspire*.deb
	rm -fr wrk
	apt-get download activinspire
	if [ $? -ne 0 ]
	then
		exit 1
	fi
	#Rebuild the package without wrong depends
	mkdir wrk
	dpkg-deb -R activinspire*.deb wrk
	sed -i 's/libre2-5/libre2-5|libre2-9/g' wrk/DEBIAN/control 
	dpkg-deb -b wrk activinspire-lliurex.deb
	dpkg -i $PWD/activinspire-lliurex.deb
}

function fix_activtools_pkg_llx23
{
	#this pkg needs activaid so install it first
	apt-get install -y activdriver
	apt-get install -y activaid
	apt-get download activtools
	if [ $? -ne 0 ]
	then
		exit 1
	fi
	#Rebuild the package without libssl1.0.0 depend
	mkdir wrk
	dpkg-deb -R activtools*.deb wrk
	sed -i 's/libssl1.0.0,/libssl1.1,/g' wrk/DEBIAN/control 
	dpkg-deb -b wrk activtools-lliurex.deb
	dpkg -i $PWD/activtools-lliurex.deb
	mv $PWD/activtools-lliurex.deb /tmp
	mv $PWD/wrk /tmp
}
ACTION="$1"

case $ACTION in

	preInstall)
		dpkg --add-architecture i386
		apt-get update
		#Install needed packages
		#apt-get install -y activinspire-32bitdependsfor64bit-lliurex
		#Get the linux-headers of current kernel
		apt-get install -y linux-headers-$VERSION #linux-source-${VERSION/-*/}
		if [ $(lliurex-version -n | cut -d "." -f1) -gt 21 ]
		then 
			WRKDIR=$(mktemp -d)
			chmod 777 $WRKDIR
			cd $WRKDIR
			#fix_activtools_pkg_llx23
			rm -fr $WRKDIR/*
			fix_activinspire_pkg_llx23
			apt-get install -y -f
#		else
#			apt-get install -y libelf-dev=0.176-1.1ubuntu0.1
		fi
		if [ $? -ne 0 ]
		then
			exit 1
		fi
		;;
	postInstall)
		dpkg --compare-versions 5.19 le $VERSION && compile_for_519
		dpkg --compare-versions 5.17 le $VERSION && compile_for_517
		dpkg --compare-versions 5.0 lt $VERSION && compile_for_5x
		;;	
	remove)
		errpkg=''
		for pkg in promethean-fixboot activinspire activrelay activtools activdriver activaid activinspire-32bitdependsfor64bit-lliurex
		do
			apt-get remove -y $pkg
			TEST=$( dpkg-query -s $pkg 2> /dev/null| grep Status | cut -d " " -f 4 )
			if [ "$TEST" == 'installed' ];then
				errpkg="$errpkg $pkg"
			fi	
		done
		if [ ${#errpkg} -gt 0 ]
		then
			exit 1
		fi
	;;	
esac
exit 0
