#!/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" ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME [ <stored data directory> [ <published data directory> [ <user> [ <group> ] ] ] ]

Initialise stored and published data directories either at any specified
locations or, respectively, at $INSTALL_DIR and $WEB_INSTALL_DIR.

Set permissions to the given user and group or, respectively, to $IMIP_AGENT_USER
and $IMIP_AGENT_GROUP.

Within the stored data directory (using $INSTALL_DIR as an example), the
following directories are created:

  * $INSTALL_DIR/journal
  * $INSTALL_DIR/preferences
  * $INSTALL_DIR/store

Within the published data directory (using $WEB_INSTALL_DIR as an example), the
following directory is created:

  * $WEB_INSTALL_DIR/static
EOF
    exit 1
fi

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

for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/preferences "$WEB_INSTALL_DIR"/static \
           "$INSTALL_DIR"/journal ; do
    mkdir -p "$DIR"
    chown "$USER" "$DIR"
    chgrp "$GROUP" "$DIR"
    chmod g+ws "$DIR"
done
