#!/bin/sh
USER_HOME=$1
if [ "$USER_HOME" = "" ]; then USER_HOME=`realpath ~iceman`; fi
ENV_FILE="$USER_HOME/.bashrc"
if [ -e /etc/os-release ] && cat /etc/os-release | grep -q "ID=iceos"; then
  ENV_FILE="/etc/profile"
fi
source $ENV_FILE

if [ -f $USER_HOME/icebox.tbl ]; then
 touch /tmp/iceos.loopserver
 while true ; do
  trap "echo Trapped Cntrl-C" SIGINT
  echo "Start SERVER on $DISPLAY"
  ice server/iceos 8080 icebox
  trap - SIGINT
  if [ -f /tmp/iceos.loopserver ]; then
    echo "Restarting iceserver in 10 sec"
    sleep 10
  else
    break
  fi
 done
 rm -rf /tmp/iceos.loopserver
else
  echo "No icebox.tbl config found for SERVER on $DISPLAY"
  echo "Use Right Mouse Button to bring up a terminal"
  sleep 60
fi
echo "Done SERVER"
