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

Fix permissions for the stored and published data directories, using the given
user and group (or, respectively, $DEFAULT_USER and $DEFAULT_GROUP if omitted),
and operating on the given stored data and published data directories (or,
respectively, $DEFAULT_INSTALL_DIR and $DEFAULT_WEB_INSTALL_DIR if omitted).
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}

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

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