#!/bin/bash
set -e

printf "Host name: " >&2
if [ "$1" ]; then
    host="$1"; shift
    echo "$host"
else
    read host
fi

if ! grep -Fq "." <<< "$host"; then host=$host.mit.edu; fi

printf "User: " >&2
if [ "$1" ]; then
    user="$1"; shift
    echo "$user"
else
    read user
fi

pw=$(getent passwd "$user")
if [ $? -ne 0 ]; then
    echo "User not found." >&2
    exit $?
fi
IFS=: read user x uid gid x home x <<< "$pw"

user_dn=$(ldapsearch -LLL -x -b ou=People,dc=scripts,dc=mit,dc=edu '(uid=andersk)' dn | perl -0pe 's/\n //g; s/^dn: //')

printf "Docroot: $home/web_scripts" >&2
read subdir

tmpfile=$(mktemp -t vhostadd.XXXXXX) || exit $?
trap 'rm -f "$tmpfile"' EXIT

cat <<EOF > "$tmpfile"
dn: apacheServerName=$host,ou=VirtualHosts,dc=scripts,dc=mit,dc=edu
objectClass: apacheConfig
objectClass: top
apacheServerName: $host
EOF

if [ "${host%mit.edu}" != "$host" ]; then
    cat <<EOF >> "$tmpfile"
apacheServerAlias: ${host%.mit.edu}
EOF
fi

cat <<EOF >> "$tmpfile"
apacheDocumentRoot: $home/web_scripts$subdir
apacheSuexecUid: $uid
apacheSuexecGid: $gid

dn: scriptsVhostName=$host,ou=VirtualHosts,dc=scripts,dc=mit,dc=edu
objectClass: scriptsVhost
objectClass: top
scriptsVhostName: $host
EOF

if [ "${host%mit.edu}" != "$host" ]; then
    cat <<EOF >> "$tmpfile"
scriptsVhostAlias: ${host%.mit.edu}
EOF
fi

cat <<EOF >> "$tmpfile"
scriptsVhostAccount: $user_dn
scriptsVhostDirectory: ${subdir#/}
EOF

exec ldapvi --add --in "$tmpfile"
