#!/bin/sh

PROGNAME=`basename $0`

DEFAULT_USER=imip-agent
DEFAULT_INSTALL_DIR=/var/lib/imip-agent
DEFAULT_WEB_INSTALL_DIR=/var/www/imip-agent

if [ "$1" = "--help" ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME <calendar user> [ <user> [ <stored data directory> [ <published data directory> ] ] ]

Initialise a given calendar user within an existing installation, using the
given system user for ownership and the installation's stored data and published
data directories (or, respectively, $DEFAULT_USER, $DEFAULT_INSTALL_DIR and
$DEFAULT_WEB_INSTALL_DIR if omitted).
EOF
    exit 1
fi

CALENDAR_USER=${1:-$DEFAULT_CALENDAR_USER}
USER=${2:-$DEFAULT_USER}
INSTALL_DIR=${3:-$DEFAULT_INSTALL_DIR}
WEB_INSTALL_DIR=${4:-$DEFAULT_WEB_INSTALL_DIR}

for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static ; do
    mkdir -p "$DIR/$CALENDAR_USER"
    chown "$USER" "$DIR/$CALENDAR_USER"
    chmod g+ws "$DIR/$CALENDAR_USER"
    # Group privileges should already be set.
done
