#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

_pkgname='webinterface-onboot'
pkgnames=("$_pkgname")
pkgdesc="Start the web interface without the cable, on boot."
url="https://github.com/rM-self-serve/$_pkgname"
pkgver=1.2.3-1
timestamp=2023-12-31T11:43:00Z
section="utils"
maintainer="rM-self-serve <122753594+rM-self-serve@users.noreply.github.com>"
license=MIT
conflicts=(ddvk-hacks signature-rm)
installdepends=(libbfd)

source=(
    "$url"/archive/e184b6a37ccba0ebeacd34faf63c8f4cdfa5c448.zip
    "$_pkgname-toltec.service"
)
sha256sums=(
    2e3666b1875f874ef09da2bbd163295b89e9e241f1e59e77349b0e2db716b8ff
    SKIP
)

package() {
    install -D -m 755 -t "$pkgdir/opt/bin" "$srcdir/$_pkgname"
    install -D -m 644 "$srcdir/$_pkgname-toltec.service" \
        "$pkgdir/lib/systemd/system/$_pkgname.service"

    touch "$srcdir"/emptyfile
    install -D -m 666 -t "$pkgdir"/usr/share/toltec/reenable.d/"$_pkgname" "$srcdir"/emptyfile
}

configure() {
    systemctl daemon-reload

    echo
    echo "Applying usb0 ip persistence"
    webinterface-onboot apply-prstip -y > /dev/null
    echo "Success"
    if webinterface-onboot is-hack-version > /dev/null; then
        echo
        echo "Applying binary modification"
        webinterface-onboot apply-hack -y > /dev/null
        echo "Success"
    fi

    echo
    echo "Run the following command to use $pkgname"
    how-to-enable "$pkgname.service"
    echo
    echo "Then restart xochitl or the device"
}

_restore() {
    if webinterface-onboot is-prstip-applied > /dev/null; then
        echo
        echo "Reverting usb0 ip persistence"
        webinterface-onboot revert-prstip -y > /dev/null
        echo "Success"
    fi

    if webinterface-onboot is-hack-applied > /dev/null; then
        echo
        echo "Reverting binary modification"
        if webinterface-onboot has-backup > /dev/null; then
            webinterface-onboot revert-hack --backup -y > /dev/null
        else
            webinterface-onboot revert-hack --reverse -y > /dev/null
        fi
        echo "Success"
    fi
}

preremove() {
    disable-unit "$pkgname.service"
    _restore
}

preupgrade() {
    _restore
}

postremove() {
    systemctl daemon-reload
}
