#!/bin/sh

DIRNAME=`dirname "$0"`

if [ -e "$DIRNAME/config.sh" ]; then
    . "$DIRNAME/config.sh"
else
    . /etc/imip-agent/config.sh
fi

PROGNAME=`basename "$0"`

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

Initialise a given calendar user within an existing installation, creating
resources within the given stored data and published data directories or,
respectively, within $INSTALL_DIR and $WEB_INSTALL_DIR.

The resources will be defined as having the given system user as owner or,
if the user is omitted, the $IMIP_AGENT_USER as owner.
EOF
    exit 1
fi

CALENDAR_USER=$1
INSTALL_DIR=${2:-$INSTALL_DIR}
WEB_INSTALL_DIR=${3:-$WEB_INSTALL_DIR}
USER=${4:-$IMIP_AGENT_USER}

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