#!/bin/bash
#
# Program: install-tlbweb-fresh (Copyright) Jul 25, 2026
# Authors: Paul Aidukas - KN2R, Logan Crook - K8LRC
#
# Get/install latest Official LinkBox web package
# (public monitor exp.php + tlb-admin) from the
# package host — same idea as supermonASL_fresh_install.
#
# 25-Jul-2026  Paul-KN2R / Logan-K8LRC  Initial release (Paul-style wrapper).
# 26-Jul-2026  Logan-K8LRC              Match Paul ASL fresh_install layout.
# 05-Aug-2026  Logan-K8LRC              tmp extract; do not leave tlb down.
#
###############################################################

OSR=`cat /etc/os-release | egrep -i '^id=' |  awk -F '=' '{print $2}' | tr -d '"'`
if [ "$OSR" != "debian" ] && [ "$OSR" != "raspbian" ]; then
    echo -e "\nThis is NOT a DEBIAN / Raspbian system!"
    echo "You must get and run the correct installer for your system image."
    echo -e "Installation aborted.\n\a"
    exit 1
fi

if [ "`id -u`" -ne 0 ]; then
    echo -e "\nRun as root:\n  sudo $0\n\a"
    exit 1
fi

echo -e "\nStarting installation...\n"

# Safe PATH — package ships restart-tlb* (must not run by accident).
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

apt -y update
apt -y install apache2
apt -y install php
apt -y install libapache2-mod-php
apt -y install wget
ln -sf ../mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load 2>/dev/null
systemctl enable apache2
systemctl restart apache2
apt -y install bc
apt -y autoremove

# Package host (IP first — some networks block crabdance.com DDNS)
# Override:  PKG_URL='http://linkbox.k8lrc.crabdance.com' sudo ./install-tlbweb-fresh
PKG_URL="${PKG_URL:-http://45.32.193.131}"
PACKAGE_URL="${PACKAGE_URL:-${PKG_URL%/}/tlb-2+}"
export PACKAGE_URL PKG_URL

tlb_is_up() {
    pidof tlb >/dev/null 2>&1 && return 0
    pgrep -x tlb >/dev/null 2>&1 && return 0
    return 1
}

TLB_WAS_UP=0
tlb_is_up && TLB_WAS_UP=1

WORKDIR="/tmp/tlb-2+-install.$$"
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"
cd "$WORKDIR" || exit 1

wget "${PKG_URL%/}/tlb-2+-web.tgz" -O tlb-2+-web.tgz
sync
tar -xzf tlb-2+-web.tgz
sync
rm -f tlb-2+-web.tgz

if [ ! -d "$WORKDIR/tlb-2+" ]; then
    echo -e "\nERROR: tgz did not contain tlb-2+/ directory\n\a"
    rm -rf "$WORKDIR"
    exit 1
fi

cd "$WORKDIR/tlb-2+" || exit 1
chmod +x install-tlbweb.sh update-tlbweb.sh
chmod a-x restart-tlb restart-tlb.sample \
    restart-tlb-nopolicy restart-tlb-nopolicy.sample \
    restart-tlb-conference restart-tlb-conference.sample 2>/dev/null || true

# Public monitor is permanently exp.php; admin is tlb-admin/index.php
PUBLIC_MONITOR_SCRIPT=exp.php
export PUBLIC_MONITOR_SCRIPT
./install-tlbweb.sh "$@"
INSTALL_RC=$?

# NOTE: package no longer ships index.html (was overwriting operators' web root).

# Remove legacy web-root LinkBox public monitor if present (never touch tlb-admin/)
if [ -f /var/www/html/index.php ]; then
    if grep -qE 'exp_render_live_panel|tlb-common\.inc|LinkBox Monitor|tlbcmdport' /var/www/html/index.php 2>/dev/null; then
        stamp=`date +%Y%m%d%H%M%S`
        cp -a /var/www/html/index.php /var/www/html/index.php.bak.$stamp
        rm -f /var/www/html/index.php
        echo "Removed legacy public monitor /var/www/html/index.php (backup: index.php.bak.$stamp)"
    fi
fi

if systemctl reload apache2 >/dev/null 2>&1; then
    echo "install-tlbweb-fresh: apache2 reloaded"
else
    service apache2 force-reload >/dev/null 2>&1 || systemctl restart apache2 >/dev/null 2>&1 || true
fi

# Fresh install should not leave a previously-running tlb down.
if [ "$TLB_WAS_UP" -eq 1 ] && ! tlb_is_up; then
    echo "install-tlbweb-fresh: tlb was running before install — starting it again"
    systemctl start tlb >/dev/null 2>&1 || true
    sleep 1
    if ! tlb_is_up; then
        for bin in /usr/local/libexec/tlb /usr/local/sbin/tlb /usr/sbin/tlb; do
            if [ -x "$bin" ] && [ -f /home/thelinkbox/tlb.conf ]; then
                ( cd /home/thelinkbox && "$bin" -d -f /home/thelinkbox/tlb.conf >/dev/null 2>&1 & ) || true
                sleep 1
                break
            fi
        done
    fi
fi

rm -rf "$WORKDIR"
sync
echo -e "\nAll tasks completed.\n"

exit $INSTALL_RC

# The end ;)
###############################################################
