#!/bin/sh

set -euo pipefail

GIT_REPO=${GIT_REPO:-"https://github.com/sm0svx/svxlink.git"}
GIT_REF=${GIT_REF:-"master"}
CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-"RelWithDebInfo"}

if [[ ! -d svxlink ]]; then
  git clone --branch "${GIT_REF}" "${GIT_REPO}"
  cd svxlink/src
  mkdir build
  cd build
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_SYSCONFDIR=/etc \
    -DCMAKE_INSTALL_LOCALSTATEDIR=/var \
    -DUSE_QT=OFF \
    -DLIB_INSTALL_DIR=/lib \
    -DDO_INSTALL_CHOWN=YES \
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
    ..
  cd
fi

if [[ ! -d .svxlink ]]; then
  mkdir .svxlink
  cd .svxlink
  mkdir svxlink.d events.d modules.d

  cd events.d
  cp -s ../../svxlink/src/svxlink/svxlink/*.tcl .
  cp -s ../../svxlink/src/svxlink/modules/*/*.tcl .
  find ../../svxlink/src/build -name "*.tcl" -exec ln -s {} . \;
  rm events.tcl event_test.tcl Template.tcl
  cd ..
  mv events.d/Module[A-Z]*.tcl modules.d/
  ln -s ../svxlink/src/svxlink/svxlink/events.tcl
  ln -s ../../../usr/share/svxlink/sounds

  cd svxlink.d
  find ../../svxlink/src -name contrib -prune -o -name "*.conf" -exec cp {} . \;
  sed -i 's/^LOGIC_CORE_PATH=.*$/LOGIC_CORE_PATH=/' svxlink.conf
  sed -i 's|^EVENT_HANDLER=.*$|EVENT_HANDLER=/home/svxlink/.svxlink/events.tcl|' svxlink.conf
  mv svxlink.conf remotetrx.conf svxreflector.conf TclVoiceMail.conf ../
  rm gpio.conf ModuleTemplate.conf

  cd
fi

mkdir -p /var/lib/svxlink/pki
cd /var/spool/svxlink/
mkdir -p qso_recorder voice_mail
cd

exec "${*:-/bin/sh}"
