  #!/usr/bin/env bash

  # Originally written by MeaTLoTioN of thE qUAntUm wOrmhOlE BBS. Vendored
  # here because it builds exactly the SyncTERM feature set this door
  # needs: pkg-config picks up libjxl (JPEG XL video, see README's "Video
  # rendering") and libsndfile (native SyncTERM 1.10+ audio, see README's
  # "Audio") automatically when their -dev packages are installed, and the
  # script sanity-checks the build log for both afterward.
  #
  # Run this on whatever machine your *players* connect from (their local
  # PC), not the BBS server itself — SyncTERM is the client, not the door.

  echo "This will get syncterm, build it, and install it for you!"

  # Determine if debian or not
  checkOS=$(grep -iE "^id|id_like|REDHAT_BUGZILLA_PRODUCT" /etc/os-release|awk -F\= '{print $2}'|sed 's/"//g')

  if [[ "$checkOS" == *"debian"* ]]; then
    # Debian/Ubuntu: To Install apps/libraries used to compile
    echo "Debian/Ubuntu found: Preparing to install relevant libraries..."
    sudo apt-get install -y wget libncurses-dev libncursesw6 gcc libsdl1.2-dev build-essential libsndfile1-dev pkg-config libjxl-dev
  fi

  if [[ "$checkOS" == *"fedora"* ]]; then
    # Redhat/Fedora: To Install apps/libraries used to compile
    echo "Redhat/Fedora found: Preparing to install relevant libraries..."
    sudo yum -y install wget ncurses-devel gcc sdl12-compat SDL2 libsndfile-devel pkgconf-pkg-config libjxl-devel
    sudo yum -y groupinstall 'Development Tools'
  fi

  if [[ "$checkOS" == "arch" ]]; then
    # Arch Linux: To Install apps/libraries used to compile
    echo "Arch Linux found: Preparing to install relevant libraries..."
    sudo pacman -S --needed --noconfirm wget sdl12-compat sdl2 gcc ncurses base-devel libsndfile pkgconf libjxl
  fi

  # Make a temporary build directory
  CDIR=$(pwd)
  WDIR=/tmp/get-syncterm
  [[ -d $WDIR ]] && rm -rf $WDIR
  mkdir $WDIR && cd $WDIR

  # To Pull source
  echo "About to download the syncterm application source..."
  wget https://gitlab.synchro.net/main/sbbs/-/archive/master/sbbs-master.zip?path=3rdp -O 3rdp.zip
  wget https://gitlab.synchro.net/main/sbbs/-/archive/master/sbbs-master.zip?path=src -O src.zip

  # To extract zip files
  echo "Extracting the source now..."
  for F in *.zip; do unzip -a ${F}; done

  # Sort directory structure
  echo "Sorting directory structure..."
  for D in `ls -1d sbbs*`; do cp -rv ${D}/* .; done
  rm -rf sbbs-master-*

  # Change directory to
  echo "Change into the 'make' folder"
  cd src/syncterm

  # To get full path src
  echo "Set st_path variable for the SRC_ROOT path..."
  st_path=$(pwd | sed 's/\/syncterm$//g')

  # Time to compile!
  echo "Make SRC_ROOT with path: $st_path"
  BUILD_LOG=/tmp/syncterm-build.log
  make SRC_ROOT=$st_path 2>&1 | tee "$BUILD_LOG"

  # Sanity-check that libsndfile was actually picked up by the build. This is
  # what SyncTERM's native audio mixer (LoadBlob/Queue APCs) needs — its
  # absence is why audio silently reports unavailable even when PulseAudio/
  # ALSA/OSS all work fine for plain terminal-bell-style sound, since that's a
  # separate output-backend concern from decoding audio files at all.
  echo
  if grep -qi sndfile "$BUILD_LOG"; then
    echo "sndfile mentioned in build output:"
    grep -i sndfile "$BUILD_LOG"
  else
    echo "WARNING: no mention of sndfile anywhere in the build output ($BUILD_LOG)."
    echo "This usually means the build system didn't detect libsndfile at all —"
    echo "double check the *-dev package installed above matches what this"
    echo "Makefile actually looks for, or check its docs for an explicit flag"
    echo "(e.g. a WITH_SNDFILE= make variable) if auto-detection isn't finding it."
  fi

  # Same check for libjxl: the GNUmakefile only defines WITH_JPEG_XL if
  # `pkg-config libjxl --exists` succeeds at build time (see
  # src/syncterm/GNUmakefile). Without it, JPEG XL graphics APCs
  # (DrawJXLBlob etc.) are compiled out entirely and SyncTERM:Q;JXL always
  # reports unsupported (pR=0) at runtime, even though everything else
  # still works fine.
  if grep -qi jxl "$BUILD_LOG"; then
    echo "jxl mentioned in build output:"
    grep -i jxl "$BUILD_LOG"
  else
    echo "WARNING: no mention of jxl anywhere in the build output ($BUILD_LOG)."
    echo "This usually means pkg-config couldn't find libjxl.pc — check that"
    echo "libjxl-dev (or your distro's equivalent) is actually installed and"
    echo "that 'pkg-config --exists libjxl' succeeds before rebuilding."
  fi

  # Install SyncTerm
  echo "Install SyncTERM..."
  sudo make install

  # Find out where Syncterm was installed
  echo "Find out where SyncTERM installed"
  which syncterm

  # Get back to the original directory
  cd $CDIR
