#!/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> ] ] ] ]

Fix permissions for the stored and published data directories, operating on the
given stored data and published data directories (or, respectively,
$INSTALL_DIR and $WEB_INSTALL_DIR if omitted).

Set ownership and membership using the given user and group (or, respectively,
$IMIP_AGENT_USER and $IMIP_AGENT_GROUP if omitted).
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}

chown -R "$USER" "$INSTALL_DIR"
chgrp -R "$GROUP" "$INSTALL_DIR"

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