#!/bin/sh -e
#
# postinst script for tts2-6400
#
# see dh_install(1)
#
# 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"
BUILDDIR="/tmp/linux-saa716x"
LOGFILE="$BUILDDIR.log"
PATCHDIR="/usr/share/tts2-6400/patches"
KERNELVERSION="$(uname -r)"
KERNELCONFIG="/boot/config-$KERNELVERSION"
#KERNELCONFIG="/usr/src/linux-headers-$KERNELVERSION/.config
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"

print() { echo "## $1" | tee -a $LOGFILE; [ x$2 = x ] || exit $2; }
finish() { if [ x$? = x0 ]; then print "Delete $BUILDDIR"; rm -rf $BUILDDIR; print "Success"; else print "FAILED"; fi; print "Logfile: $LOGFILE"; }

[ x$1 = xconfigure ] || [ x$1 = xreconfigure ] || exit 1

[ -e /etc/os-release ] || print "/etc/os-release not found" 1
. /etc/os-release
[ "x$NAME" = "xDebian GNU/Linux" ] || [ "x$NAME" = "xUbuntu" ] || print "Only Debian GNU/Linux and Ubuntu are supported" 1

[ -e $KERNELCONFIG ] || print "Can't find $KERNELCONFIG" 1
[ -e $MODULESYMVERS ] || print "Can't find $MODULESYMVERS" 1

rm -rf $BUILDDIR $LOGFILE
mkdir -p $BUILDDIR || print "Can't create $BUILDDIR" 1
trap finish exit
cd $BUILDDIR

if [ -e $PATCHDIR/$PATCH ]; then
    print "Copy patch"
    cp $PATCHDIR/$PATCH .
else
    print "Download patch"
    wget -q $URL/$PATCH || print "Patch download failed: $URL/$PATCH" 1
    [ -e $PATCHDIR ] || mkdir -p $PATCHDIR || print "Can't create $PATCHDIR" 1
    cp -af $PATCH $PATCHDIR/
fi
SECTION="$(dirname $(grep source $PATCH | grep saa716x/Kconfig | cut -d'"' -f2))"

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
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 < ../$PATCH >>$LOGFILE 2>&1 || print "Linux patch failed" 1
printf "y\n\nm\nm\nm\n" | make oldconfig >>$LOGFILE 2>&1 || print "make oldconfig failed" 1

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

print "Install modules"
#make KERNELVERSION=$KERNELVERSION M=$SECTION modules_install >>$LOGFILE 2>/dev/null || print "make modules_install failed" 1
[ -e /lib/modules/$KERNELVERSION/kernel/$SECTION ] || mkdir /lib/modules/$KERNELVERSION/kernel/$SECTION || print "mkdir /lib/modules/$KERNELVERSION/kernel/$SECTION failed" 1
cp -af $SECTION/*.ko /lib/modules/$KERNELVERSION/kernel/$SECTION/ || print "copy modules failed" 1
depmod -a $KERNELVERSION

print "Check/Load modules"
grep -sq saa716x_ff /proc/modules || modprobe saa716x_ff || print "modprobe failed" 1

#DEBHELPER#

exit 0
