#!/bin/sh

set -eu

command=$1
shift

urlbase='http://activsoftware.co.uk/linux/repos/driver/ubuntu/pool'

promethean_links='
  /etc/promethean
  /etc/udev/rules.d/10-board.rules
  /etc/xdg/autostart/activmgr.desktop
  /lib/udev/rules.d/10-activ-tpd.rules
  /lib/udev/rules.d/66-xorg-promethean.rules
  /opt/bitrock/activaid-2.0.1-0
  /usr/bin/promethean.sh
  /usr/lib/X11/xorg.conf.d/10-promethean.conf
  /usr/local/bin/ac3register
  /usr/local/bin/ac3register.sh
  /usr/local/bin/activaid
  /usr/local/bin/activaid.sh
  /usr/local/bin/activcalibrate
  /usr/local/bin/activcalibrate.sh
  /usr/local/bin/activcheckspecs
  /usr/local/bin/activ_enumerator
  /usr/local/bin/activlc
  /usr/local/bin/activmgr
  /usr/local/bin/activmgr.sh
  /usr/local/bin/activmonitor
  /usr/local/bin/activmonitor.sh
  /usr/local/bin/dataview
  /usr/local/bin/dataview.sh
  /usr/local/bin/detectuserswitch
  /usr/local/bin/devreg
  /usr/local/bin/devreg.sh
  /usr/local/bin/icudtl.dat
  /usr/local/bin/imageformats
  /usr/local/bin/modifyxorg.py
  /usr/local/bin/platforms
  /usr/local/bin/qtwebengine_locales
  /usr/local/bin/QtWebEngineProcess
  /usr/local/bin/qtwebengine_resources_100p.pak
  /usr/local/bin/qtwebengine_resources_200p.pak
  /usr/local/bin/qtwebengine_resources.pak
  /usr/local/bin/timelinkdiagnostics
  /usr/local/bin/timelinkdiagnostics.sh
  /usr/local/bin/visual_view
  /usr/local/bin/visual_view.sh
  /usr/local/bin/xcbglintegrations
  /usr/local/lib32/libactivboardex.so.1.0
  /usr/local/lib/libactivboardex.so.1.0
  /usr/local/lib/libactivsystem.so
  /usr/local/lib/libactivsystem.so.2
  /usr/local/lib/libactivsystem.so.2.4
  /usr/local/lib/libactivsystem.so.2.4.0
  /usr/local/lib/libclassflowclient.so
  /usr/local/lib/libclassflowclient.so.1
  /usr/local/lib/libclassflowclient.so.1.0
  /usr/local/lib/libclassflowclient.so.1.0.0
  /usr/share/applications/activaid.desktop
  /usr/share/applications/ActivCalibrate.desktop
  /usr/share/applications/activmonitor.desktop
  /usr/share/hal/fdi/policy/20thirdparty/12-promethean.fdi
  /usr/share/icons/hicolor/48x48/apps/activaid.png
  /usr/share/icons/hicolor/48x48/apps/activcalibrate.png
  /usr/share/icons/hicolor/48x48/apps/activmonitor.png
  /usr/share/promethean/activaid
  /usr/share/promethean/languages
  /usr/share/promethean/vasaricontent
  /usr/src/promethean
  /usr/tmp-promethean/driver/activdevices
'

build_and_install_promethean_ko() {
  tmp_module_build=$(mktemp -d /tmp/promethean.XXXXXX)
  cp -a -t "${tmp_module_build}/" /usr/src/promethean/*
  cat <<'EOF' > "${tmp_module_build}/kernel/Makefile.puavo"
obj-m += promethean.o
promethean-objs += Mathrout.o usbpromethean.o

all:
	make -C /lib/modules/$(KERNEL_VERSION)/build KBUILD_EXTMOD="$(PWD)" modules

clean:
	make -C /lib/modules/$(KERNEL_VERSION)/build KBUILD_EXTMOD="$(PWD)" clean
EOF

  # compile promethean.ko for the default kernel only
  # (does not compile on "fresh")
  vmlinuz_version=$(readlink /boot/vmlinuz) || true
  kernel_version=${vmlinuz_version#vmlinuz-}

  if [ -z "$kernel_version" ]; then
    echo 'Could not determine the default kernel version' >&2
    exit 1
  fi

  (
    cd "${tmp_module_build}/kernel"
    module_file="promethean-${kernel_version}.ko"
    if [ ! -e "modules/${module_file}" ]; then
      mkdir -p modules
      make "KERNEL_VERSION=${kernel_version}" clean all
      mv promethean.ko "modules/${module_file}"
    fi
    cp -p "${tmp_module_build}/kernel/modules/${module_file}" \
          "/lib/modules/${kernel_version}/kernel/drivers/input/tablet/promethean.ko"
    if [ "$(uname -r)" = "$kernel_version" ]; then
      depmod -a
      modprobe promethean
    fi
    rm -rf "$tmp_module_build"
  )
}

case "$command" in
  configure)
    upstream_dir=$1

    for f in $promethean_links; do
      mkdir -p "$(dirname "$f")"
      ln -fsT "${upstream_dir}/${f}" "$f"
    done
    ln -fsT libactivboardex.so.1.0 \
            /usr/local/lib/libactivboardex.so.1

    ln -fsT /usr/lib/x86_64-linux-gnu /usr/share/promethean/lib

    install -o root -g root -m 755 /usr/tmp-promethean/driver/activdevices \
            /etc/init.d/activdevices
    update-rc.d activdevices defaults

    mkdir -p /usr/share/X11/xorg.conf.d
    cat <<'EOF' > /usr/share/X11/xorg.conf.d/75-promethean.conf
Section "InputClass"
	Identifier   "calibration"
	MatchProduct "Promethean Limited ACTIVboard"
	Driver       "evdev"
EndSection
EOF

    cat <<'EOF' > /usr/src/promethean/kernel/Makefile
obj-m += promethean.o
promethean-objs += Mathrout.o usbpromethean.o

debug: all
all:
	make -C /lib/modules/$(shell uname -r)/build KBUILD_EXTMOD="$(PWD)" modules

clean:
	make -C /lib/modules/$(shell uname -r)/build KBUILD_EXTMOD="$(PWD)" clean

install:
	./b install
EOF

    build_and_install_promethean_ko
    ;;

  unconfigure)
    upstream_dir=$1

    update-rc.d activdevices remove
    rm -f /etc/init.d/activdevices \
          /lib/modules/*/kernel/drivers/input/tablet/promethean.ko \
          ${promethean_links} \
          /usr/local/lib/libactivboardex.so.1 \
          /usr/share/promethean/lib \
          /usr/share/X11/xorg.conf.d/75-promethean.conf
    rmmod promethean 2>/dev/null || true
    ;;

  unpack)
    upstream_pack=$1
    upstream_dir=$2
    tar -x -f "$upstream_pack" -C "$upstream_dir"
    ;;

  download)
    upstream_pack=$(readlink -f "$1")
    tmpdir=
    trap '[ -n "$tmpdir" ] && rm -rf "$tmpdir"' 0 INT TERM
    tmpdir=$(mktemp -d)

    (
      cd "$tmpdir"
      while read sha384 debpath; do
        deb=$(basename "$debpath")
        wget \
            --no-use-server-timestamps \
            --no-cookies \
            --output-document "$deb" \
            --progress=dot:mega \
            "${urlbase}/${debpath}" || {
            [ $? -eq 4 ] && exit 2 ## Network failure.
            exit 1
        }
        echo "${sha384} ${deb}" | sha384sum --check >/dev/null || exit 1
        dpkg-deb -x "$deb" promethean || exit 1
      done <<EOF
83effb26da80162679fc67d71693731e896041382fc77ced089e60d4e21cdad2b5023036d675e535853c77ff6f9417dc oss/a/activdriver/activdriver_5.18.19-0~Ubuntu~1804_amd64.deb
f84e72d2267c9131c8b8cb1180b12aff170882d45048dcf52bb6fdebf6f0f7d25df86178143e5f067ae4483641ff1fda non-oss/a/activaid/activaid_2.0.1~Ubuntu~1804_amd64.deb
1d79a843a9ce5d25f6a0fd743b664335a520e5ba9af5f2b1e4a099433e43a0141533d0939c2f809ce03794a65629bdc8 non-oss/a/activtools/activtools_5.18.19-0~Ubuntu~1804_amd64.deb
EOF
      # these take space and do not work on Debian Stretch
      rm -rf promethean/usr/share/promethean/lib

      # Set LC_COLLATE=C so that files always sort in the same
      # way (so we get the same tar-archive independent of locales).
      env LC_COLLATE=C \
        tar -C promethean --mtime='2000-01-01 00:00:00 +00:00' --sort=name -c \
          -f "$upstream_pack" .
    )
    ;;

    *)
        ;;
esac
