#!/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="/tmp/$(basename $BUILDDIR.log)"
PATCHDIR="/usr/share/tts2-6400/patches"
KERNELVERSION="$(uname -r)"
#KERNELCONFIG="/usr/src/linux-headers-$KERNELVERSION/.config"
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"

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"; [ ! -e $LOGFILE ] || print "Logfile: $LOGFILE"; fi; }

[ x$1 = xconfigure ] || exit 1
rm -rf $BUILDDIR $LOGFILE
touch $LOGFILE
mkdir -p $BUILDDIR || print "Can't create $BUILDDIR" 1
cd $BUILDDIR
trap finish exit
[ -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

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
[ $(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 < ../$PATCH >>$LOGFILE 2>&1 || print "Linux patch failed" 1
if [ $KMAJOR -lt 5 ] || [ $KMINOR -lt 15 ]; then
    INPUT="y\nm\nm\nm\nm\n"
else
    INPUT="y\ny\ny\nm\nm\nm\n\n\n"
fi
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"
#make KERNELVERSION=$KERNELVERSION M=$SECTION modules_install >>$LOGFILE 2>/dev/null || print "make modules_install failed" 1
[ -e /lib/modules/$KERNELVERSION/kernel/$SECTION ] || mkdir -p /lib/modules/$KERNELVERSION/kernel/$SECTION || print "mkdir -p /lib/modules/$KERNELVERSION/kernel/$SECTION failed" 1
cp -af $SECTION/*.ko /lib/modules/$KERNELVERSION/kernel/$SECTION/ || print "copy modules failed" 1
depmod -a -F /boot/System.map-$KERNELVERSION $KERNELVERSION || true

print "Check/Load modules"
grep -sq saa716x_ff /proc/modules || modprobe saa716x-ff || print "WARNING: modprobe saa716x-ff failed"

exit 0
