#!/bin/sh

set -e

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

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

# install stuff
mkdir -p "$dir"
mkdir "$dir"/env
mkdir "$dir"/sslio
cat <<-EOT >"$dir"/run
	#!/bin/sh
	cd $webroot
	exec chpst -m3000000 -U$acct -e$dir/env \\
	  tcpsvd -l0 ${myip:-0} 443 \\
	    sslio -u$sslacct -/$dir/sslio -C/cert.pem fnord 2>&1
	EOT
chmod 755 "$dir"/run
echo 1 >"$dir"/env/COLF
echo 1 >"$dir"/env/COLF_COMBINED
echo 'https://' >"$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
