#!/bin/sh -e
#
# postinst script for tts2-6400
#
# see dh_install(1)
case "$1" in
    configure)
	#
	# Build Debian/Ubuntu modules for TechnoTrend Premium DVB S2-6400
	# The build directory needs at least 1 GByte space!
	#
	URL="https://github.com/s-moch/linux-saa716x/compare"
	BASEDIR=/usr/share/tts2-6400
	BUILDDIR="${BASEDIR}/linux-saa716x"
	PATCHDIR="${BASEDIR}/patches"
	KERNELVERSION="$(uname -r)"
	KERNELCONFIG="/boot/config-$KERNELVERSION"
	MODULESYMVERS="/usr/src/linux-headers-${KERNELVERSION}/Module.symvers"
	KMAJOR="$(echo $KERNELVERSION | cut -d'.' -f1)"
	KMINOR="$(echo $KERNELVERSION | cut -d'.' -f2)"
	PATCH="saa716x-${KMAJOR}.${KMINOR}.diff"
	LOGFILE="${BASEDIR}/linux-saa716x-${KERNELVERSION}.log"

	print() { echo "## $1" | tee -a $LOGFILE; [ x$2 = x ] || exit $2; }

	finish() {
	    if [ x$? = x0 ]; then
		print "Success"
		print "Delete build directory $BUILDDIR"
		rm -rf $BUILDDIR
		print "Logfile: $LOGFILE"
		cat /usr/share/doc/tts2-6400/README
	    else
		print "FAILED"
		print "Build directory: $BUILDDIR"
		print "Logfile: $LOGFILE"
	    fi
	}

	if [ ! -e /etc/os-release ]; then echo "## /etc/os-release not found"; exit 1; fi
	. /etc/os-release
	if [ "x$NAME" != "xDebian GNU/Linux" ] && [ "x$NAME" != "xUbuntu" ]; then echo "## Only Debian GNU/Linux and Ubuntu are supported"; exit 1; fi
	if [ ! -e $KERNELCONFIG ]; then echo "## Can't find $KERNELCONFIG"; exit 1; fi
	if [ ! -e $MODULESYMVERS ]; then echo "## Can't find $MODULESYMVERS"; exit 1; fi

	rm -rf $BUILDDIR
	if ! mkdir -p $BUILDDIR; then echo "## Can't create $BUILDDIR"; exit 1; fi
	[ -e "$PATCHDIR" ] || mkdir -p $PATCHDIR
	>$LOGFILE

	trap finish exit

	if [ $KMAJOR -lt 4 ]; then
	    print "Only kernel version 4.12 or higher is supported" 1
	elif [ $KMAJOR -lt 5 ]; then
	    [ "$KMINOR" -ge 12 ] || print "Only kernel version 4.12 or higher is supported" 1
	    INPUT="y\nm\nm\nm\nm\n"
	elif [ $KMAJOR -lt 6 ]; then
	    INPUT="y\ny\ny\nm\nm\nm\n\n\n"
	elif [ $KMAJOR -lt 7 ]; then
	    INPUT="y\ny\ny\nm\nm\nm\n\n"
	fi

	cd $BUILDDIR
	if [ -e "$PATCHDIR/$PATCH" ]; then
	    print "Using patch $PATCHDIR/$PATCH"
	else
	    print "Download patch"
	    wget -q -O $PATCHDIR/$PATCH $URL/$PATCH || print "Patch download failed: $URL/$PATCH" 1
	fi

	SECTION="$(dirname $(grep source $PATCHDIR/$PATCH | grep saa716x/Kconfig | cut -d'"' -f2))"
	[ x$SECTION != x ] || print "SECTION not detected" 1

	print "Uncompress linux source"
	SOURCE=$(find /usr/src -type f -name linux-source-$KMAJOR.$KMINOR*.tar.*)
	[ x$SOURCE != x ] || print "Can't find linux source archive" 1
	[ $(echo $SOURCE | wc -w) = 1 ] || print "Found multiple linux source archives" 1
	tar xf $SOURCE || print "Uncompressing linux source failed" 1
	SOURCEDIR=$(find . -type d -name linux-source-$KMAJOR.$KMINOR*)
	[ -e "$SOURCEDIR" ] || print "Linux source directory not found" 1
	cd $SOURCEDIR

	print "Prepare linux source"
	cp $KERNELCONFIG .config
	cp $MODULESYMVERS Module.symvers
	patch -p1 < $PATCHDIR/$PATCH >>$LOGFILE 2>&1 || print "Linux patch failed" 1
	printf "$INPUT" | make oldconfig >>$LOGFILE 2>&1 || print "make oldconfig failed" 1
	make KERNELVERSION=$KERNELVERSION -j6 modules_prepare >>$LOGFILE 2>&1 || print "make modules_prepare failed" 1

	print "Build modules"
	make KERNELVERSION=$KERNELVERSION -j6 M=$SECTION modules >>$LOGFILE 2>&1 || print "make modules failed" 1

	print "Install modules"
	[ -e /lib/modules/$KERNELVERSION/kernel/$SECTION ] || mkdir -p /lib/modules/$KERNELVERSION/kernel/$SECTION || print "mkdir -p /lib/modules/$KERNELVERSION/kernel/$SECTION failed" 1
	print "cp -af $SECTION/*.ko /lib/modules/$KERNELVERSION/kernel/$SECTION/" >>$LOGFILE 2>&1
	cp -af $SECTION/*.ko /lib/modules/$KERNELVERSION/kernel/$SECTION/ || print "copy modules failed" 1

	print "Update module dependencies"
	depmod -a -F /boot/System.map-$KERNELVERSION $KERNELVERSION || print "depmod failed" 1

	print "Unload/Load modules"
	! grep -sq saa716x_ff /proc/modules || modprobe -r saa716x-ff >>$LOGFILE 2>&1 || print "WARNING: modprobe -r saa716x-ff failed - in use?"
	grep -sq saa716x_ff /proc/modules || modprobe saa716x-ff >>$LOGFILE 2>&1 || print "WARNING: modprobe saa716x-ff failed"
    ;;

    *)
	echo "postinst called with unknown argument \`$1'" >&2
	exit 1
    ;;
esac

#DEBHELPER#

exit 0
