#!/bin/bash
# Install script version
VERSION_DATE=november-6-2026
#
# Downloads the newest LinFBB release from SourceForge, compiles it,
# installs it and restarts the BBS.
# The version is no longer written in this script: it is asked to
# SourceForge each time (latest release of the linfbb project).
#
# Usage (as root):  ./updfbb            full update
#                   ./updfbb --essai    test only: download and compile,
#                                       the BBS is neither stopped nor installed
#
# Script provided by Charles S. Schuman, modified by F6BVP.

# Directories
SRCDIR=/usr/local/src/ax25
FBBCONF=/usr/local/etc/ax25/fbb
FBBVAR=/usr/local/var/ax25/fbb
LOG=$SRCDIR/bbserror.txt

ESSAI=0
[ "$1" = "--essai" ] && ESSAI=1

# Color Codes
Reset='\e[0m'
Red='\e[31m'
Green='\e[30;42m'  # Black/Green
Yellow='\e[33m'
BluW='\e[37;44m'

echo -e "${BluW}\t\n\t Script provided by Charles S. Schuman, modified by F6BVP for updating LinFBB BBS package\t\n\t\t\t ${Red} $VERSION_DATE \n\t\t${Yellow}       k4gbb1@gmail.com f6bvp@free.fr \n${Reset}"

if [ "$(id -u)" -ne 0 ]; then
  echo -e "${Red}\t This script must be run as root (sudo ./updfbb) ${Reset}"
  exit 1
fi

# Tools and libraries needed to compile
echo -e "${Red}\t Checking compiler and ncurses development files ${Reset}"
apt-get -y -q install wget bzip2 gcc make >> /dev/null 2>&1
# libncurses-dev on recent systems, libncurses5-dev/libncursesw5-dev on older ones
apt-get -y -q install libncurses-dev >> /dev/null 2>&1 || \
  apt-get -y -q install libncurses5-dev libncursesw5-dev >> /dev/null 2>&1

# Directories
mkdir -p $SRCDIR $FBBVAR/docs
cd $SRCDIR || exit 1
: > $LOG

# Newest release published on SourceForge
echo -e "${Green}\t Looking for the newest LinFBB release on SourceForge ${Reset}"
BZ2=$(wget -q -O - https://sourceforge.net/projects/linfbb/best_release.json \
      | grep -o '"filename": *"/fbb-[0-9.]*\.tar\.\(bz2\|gz\)"' | head -1 \
      | sed 's/.*"\/\(fbb-[0-9.]*\.tar\.[a-z0-9]*\)"/\1/')
if [ -z "$BZ2" ]; then
  echo -e "${Red}\t Cannot find the newest release on SourceForge (network ?) ${Reset}"
  exit 1
fi
FBB=${BZ2%.tar.*}
echo -e "\t  * Newest release : ${Yellow}$FBB${Reset}"
if [ -x /usr/local/sbin/xfbbd ]; then
  echo -e "\t  * Installed      : $(strings /usr/local/sbin/xfbbd | grep -m1 -xE '7\.[0-9]+\.[0-9]+' || echo unknown)"
fi

# Removing old source files
rm -rf fbb-7.* fbb-7.*.tar.*

echo -e "${Green}\t Downloading LinFBB BBS source package $BZ2 ${Reset}"
if ! wget -q -O $BZ2 "https://sourceforge.net/projects/linfbb/files/$BZ2/download" >> $LOG 2>&1 \
   || ! tar xf $BZ2 >> $LOG 2>&1; then
  echo -e "${Red}\t Download error - See $LOG ${Reset}"
  exit 1
fi
cd $FBB || exit 1

# Configure
echo -e "${Green}\t Configuring compile LinFBB BBS ${Reset}"
if ! ./configure >> $LOG 2>&1; then
  echo -e "${Red}\t Configure compile error - See $LOG ${Reset}"
  exit 1
fi

# Making
echo -e "${Green}\t Compiling LinFBB BBS ${Reset}"
echo "  * Be very patient. This is going to be quite long..."
if ! make -j4 >> $LOG 2>&1; then
  echo -e "${Red}\t BBS Compile error - See $LOG ${Reset}"
  exit 1
fi
echo -e "\t \e[030;42m FBB BBS successfully built\t ${Reset}"

if [ $ESSAI -eq 1 ]; then
  echo -e "${Yellow}\t Test only (--essai): BBS not stopped, nothing installed.${Reset}"
  echo -e "\t Compiled in $SRCDIR/$FBB"
  exit 0
fi

# Stop BBS
echo -e "${Red}\t Shutting down FBB BBS if running ${Reset}"
chmod +x doc/fbb.sh
doc/fbb.sh stop >> /dev/null 2>&1

# Install
echo -e "${Green}\t Installing LinFBB BBS ${Reset}"
if ! make install >> $LOG 2>&1; then
  echo -e "${Red} BBS Install error - See $LOG${Reset}"
  exit 1
fi
echo -e "${Green} BBS Installed${Reset}"
rm -f $LOG

# First install: create default configuration
if [ ! -d $FBBCONF ] ; then
  echo -e "${Red} FBB base directory '$FBBCONF' does not exist. This is apparently a first install."
  echo -e "${Green} Now creating default BBS configuration directories and files.${Reset}"
  make installconf
fi

# Rename wrong log file numbers (old week 53 bug)
[ -f $FBBVAR/log/fbblog20.53 ] && mv $FBBVAR/log/fbblog20.53 $FBBVAR/log/fbblog20.00

# Restarting LinFBB
echo -e "${Red} Starting BBS${Reset}"
/usr/local/sbin/fbb
# (End of Script)
