#!/bin/bash
#################
#  HTTP SERVER  #
#################
ver="1.2"

if [ "$1" != "start" ]; then
  echo -e "\e[31;1m\n Can only be started from within 'inxi-report'!\e[0m\n"
  exit
fi

# Absolute path to this script
scrpath=$(readlink -f $0)
workdir=$(dirname $scrpath)
cd $workdir

# Load settings
source "$workdir/inxi-report.conf"


# Start http server
$(which webfsd) -i "0.0.0.0" -p "$port" -L "$workdir/server.log" -r "$docroot" -f "index.html" &

echo "*** Server started ***" >> "$workdir/server.log"

# Start kill loop. If inxi-report.sh script terminates,
# then server must be terminated too.
while :
do
     scrpid=$(ps aux | grep 'inxi-report.sh' | grep -v 'grep' | awk '{print $2}')
      if [ "$scrpid" = "" ]; then
        killall webfsd
        wait
        echo -e "*** Server terminated ***\n" >> "$workdir/server.log"
        killall server
        exit 0
      fi
     sleep 5
done
