#!/bin/sh -e
#
# postinst script for tts2-6400
#
# see dh_install(1)
#
# Build Debian modules for TechnoTrend Premium DVB S2-6400
# The build directory needs at least 1 GByte space!
#
RELEASES="stretch buster bullseye bookworm sid"
BUILDDIR=/tmp/linux-saa716x
KERNELVERSION=$(uname -r)
KMAJOR=$(echo $KERNELVERSION | cut -d'.' -f1)
KMINOR=$(echo $KERNELVERSION | cut -d'.' -f2)
URL=https://github.com/s-moch/linux-saa716x/compare
PATCH=saa716x-$KMAJOR.$KMINOR.diff

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

[ 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" ] || print "Only Debian GNU/Linux is supported" 1

for x in $RELEASES; do { [ x$x != x$VERSION_CODENAME ] || RELEASE=$x; }; done
[ x$RELEASE != x ] || print "Release not supported" 1

[ -e /usr/src/linux-headers-$KERNELVERSION/.config ] || print "Can't find /usr/src/linux-headers-$KERNELVERSION/.config" 1
[ /usr/src/linux-headers-$KERNELVERSION/Module.symvers ] || print "Can't find /usr/src/linux-headers-$KERNELVERSION/Module.symvers" 1

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

if [ -e /usr/share/tts2-6400/patches/$PATCH ]; then
    print "Copy patch"
    cp /usr/share/tts2-6400/patches/$PATCH .
else
    print "Download patch"
    wget -q $URL/$PATCH || print "Patch download failed" 1
    [ -e /usr/share/tts2-6400/patches ] || mkdir -p /usr/share/tts2-6400/patches
    cp -a $PATCH /usr/share/tts2-6400/patches/
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*)
[ x$SOURCEDIR != x ] || print "Linux source directory not found" 1
cd $SOURCEDIR

print "Prepare kernel source"
cp /usr/src/linux-headers-$KERNELVERSION/.config .
cp /usr/src/linux-headers-$KERNELVERSION/Module.symvers .
patch -p1 < ../$PATCH >/dev/null 2>&1 || print "Linux patch failed" 1
printf "y\nm\nm\nm\nm\n" | make oldconfig >/dev/null 2>&1 || print "make oldconfig failed" 1

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

print "Install modules"
make KERNELVERSION=$KERNELVERSION M=$SECTION modules_install >/dev/null 2>/dev/null || print "make modules_install failed" 1
depmod $KERNELVERSION

print "Check/Load modules"
for module in $(grep -s saa716x_ /proc/modules | cut -d' ' -f1); do { rmmod $modules >/dev/null 2>&1 || true; }; done
grep -sq saa716x_ /proc/modules || modprobe saa716x_ff || print "modprobe failed" 1

#DEBHELPER#

exit 0
