#!/bin/sh

set -e

acct=$1
logacct=$2
dir=$3
webroot=$4
myip=$5

if test x$webroot = x; then
  echo "fnord-conf: usage: fnord-conf acct logacct /fnord /webroot [ myip ]"
  exit 1;
fi

# install stuff
mkdir -p "$dir"
mkdir "$dir"/env
cat <<-EOT >"$dir"/run
	#!/bin/sh
	cd $webroot
	exec chpst -m2000000 -U$acct -e$dir/env \\
	  tcpsvd -l0 ${myip:-0} 80 fnord 2>&1
	EOT
chmod 755 "$dir"/run
echo 1 >"$dir"/env/COLF_COMBINED
echo 'http://' >"$dir"/env/PROTOURL

# tell old daemontools that it should spawn a log process, too
chmod a+t "$dir"

# install logging stuff
mkdir "$dir"/log
cat <<-EOT >"$dir"/log/run
	#!/bin/sh
	exec chpst -u$logacct svlogd -tt ./main
	EOT
chmod 755 "$dir"/log/run

mkdir "$dir"/log/main
chown "$logacct" "$dir"/log/main
