#!/bin/sh

PROGNAME=`basename $0`

DEFAULT_USER=imip-agent
DEFAULT_GROUP=lmtp
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 [ <user> [ <group> [ <stored data directory> [ <published data directory> ] ] ] ]

Initialise stored and published data directories with permissions set to the
given user and group (or, respectively, $DEFAULT_USER and $DEFAULT_GROUP if omitted),
and with the given stored data and published data directories (or, respectively,
$DEFAULT_INSTALL_DIR and $DEFAULT_WEB_INSTALL_DIR if omitted).

Within the stored data directory, the following directories are created:

  * $DEFAULT_INSTALL_DIR/store
  * $DEFAULT_INSTALL_DIR/preferences

Within the published data directory, the following directory is created:

  * $DEFAULT_WEB_INSTALL_DIR/static
EOF
    exit 1
fi

USER=${1:-$DEFAULT_USER}
GROUP=${2:-$DEFAULT_GROUP}
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"
    chown "$USER" "$DIR"
    chgrp "$GROUP" "$DIR"
    chmod g+ws "$DIR"
done
