#! /bin/sh

### BEGIN INIT INFO
# Provides: amiconnect
# Required-Start: $local_fs $remote_fs $syslog $network
# Required-Stop:  $local_fs $remote_fs $syslog $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Amiconnect kernel module and daemon startup
### END INIT INFO

install_deb() {
    if [ ! -d /usr/src/linux-headers-$(uname -r) ]; then
        echo "Packet linux-headers not found. Trying to install..."
        apt update && apt install "linux-headers-$(uname -r)" -y
    else
        echo "Packet linux-headers-$(uname -r) is installed."
    fi
}

install_rpm() {
    # работает для РЕДОС, вряд-ли будет работать для других rpm ( kernel-devel-$(uname -r) скорее всего для остальных )
    if ! rpm -q "kernel-lt-devel-$(uname -r)" &> /dev/null; then
        echo "Packet kernel-devel not found. Trying to install..."
        # Определяем, какой пакетный менеджер использовать: dnf или yum
        if command -v dnf &> /dev/null; then
            dnf install "kernel-lt-devel-$(uname -r)" -y
        else
            yum install "kernel-lt-devel-$(uname -r)" -y
        fi
    else
        echo "Packet kernel-lt-devel-$(uname -r) is installed."
    fi
}

CurDir=`pwd`
# Main Amiconnect directory
mainpath="/usr/lib/Amiconnect"

# Check hashes
wronghash="Program integrity checking failed! Reinstall program!"
cd $mainpath/Hash

# if ! ../linfpsuhash ./linfpsuhash.hsh -s > /dev/null ; then
#     echo $wronghash FPSU
#     exit 0
# fi
# if ! ../linfpsuhash ./amiconnect.hsh -s > /dev/null ; then
#     echo $wronghash amiconnect
#     exit 0
# fi
# if ! ../linfpsuhash ./amiconnectd.hsh -s > /dev/null ; then
#     echo $wronghash amiconnectd
#     exit 0
# fi
# if ! ../linfpsuhash ./AmiNFFltLib.hsh -s > /dev/null ; then
#     echo $wronghash AmiNFFltLib
#     exit 0
# fi

case "$1" in
  start)
    # Compiling Amiconnect filter driver and installing it
    cd $mainpath
    set +e
    timeout -s TERM 5s killall -q -w amiconnectd || killall -q -w -s SIGKILL amiconnectd
    set -e
    cd $mainpath/Drivers


    if command -v apt &> /dev/null; then
      install_deb
    elif command -v dnf &> /dev/null; then
      install_rpm
    fi

    if [ -f AmiNFFilterPKI.ko ]; then
      rm AmiNFFilterPKI.ko
    fi
    make > $mainpath/Drivers/build.log
    make clean > /dev/null
    if [ `lsmod | grep -o ^AmiNFFilterPKI` ]; then
      rmmod AmiNFFilterPKI.ko > /dev/null
    fi
    insmod ./AmiNFFilterPKI.ko #> /dev/null
    cd $mainpath/Hash
    ../linfpsuhash -H "$mainpath/Drivers/AmiNFFilterPKI.ko" "./AmiNFFilterPKI.hsh" "Amiconnect filter driver" -s > /dev/null
    cd $mainpath
	./amiconnectd
    cd $CurDir
  ;;
  restart)
    cd $mainpath
    set +e
    timeout -s TERM 5s killall -q -w amiconnect || killall -q -w -s SIGKILL amiconnect
    timeout -s TERM 5s killall -q -w amiconnectd || killall -q -w -s SIGKILL amiconnectd
    set -e
    ./amiconnectd 
    cd $CurDir
  ;;
  force-reload)
    cd $mainpath
    set +e
    timeout -s TERM 5s killall -q -w amiconnect || killall -q -w -s SIGKILL amiconnect
    timeout -s TERM 5s killall -q -w amiconnectd || killall -q -w -s SIGKILL amiconnectd
    set -e
    if [ `lsmod | grep -o ^AmiNFFilterPKI` ]; then
      rmmod AmiNFFilterPKI.ko > /dev/null
    fi
    insmod ./Drivers/AmiNFFilterPKI.ko > /dev/null
    ./amiconnectd
    cd $CurDir
  ;;
  stop)
    cd $mainpath
    set +e
    # GUI остаётся открытым и сообщает о недоступной службе.
    timeout -s TERM 5s killall -q -w amiconnectd || killall -q -w -s SIGKILL amiconnectd
    set -e
    cd $mainpath/Drivers
    if [ `lsmod | grep -o ^AmiNFFilterPKI` ]; then
      rmmod AmiNFFilterPKI.ko > /dev/null
    fi
    cd $CurDir
  ;;
esac

exit 0
