#!/bin/bash

# Config file
CONF_FILE="/etc/bosixnet/bosixnet-webui.conf"

# If this script is run as root, switch to the specific user
USERID=bosixnet
GROUPID=bosixnet

# TCP port for binding on localhost
FCGIPORT=33169

# ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI="/usr/bin/spawn-fcgi"

# ABSOLUTE path to the program binary
FCGIPROGRAM="/usr/bin/bosixnet_webui"

# Read settings from config file if it exists
[ -e "${CONF_FILE}" ] && . "${CONF_FILE}"

# Check user existence
if ! id -u ${USERID} &> /dev/null; then
    echo ;
    echo "User ${USERID} does not exist! Check what you use the correct USERID."
    exit 1
fi

# Check group existence
if ! id -g ${GROUPID} &> /dev/null; then
    echo ;
    echo "Group ${GROUPID} does not exist! Check what you use the correct GROUPID."
    exit 1
fi

# spawn-fcgi command-line options
EX="${SPAWNFCGI} -p ${FCGIPORT} -f ${FCGIPROGRAM} -u ${USERID} -g ${GROUPID}"

# Clean the environment, set up a new one and launch the program
env - ${E} ${EX} &> /dev/null

