#!/bin/sh

DIRNAME=`dirname "$0"`

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

PROGNAME=`basename "$0"`

if [ "$1" = "--help" ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME

Fix permissions for the stored and published data directories, operating on...

  * $INSTALL_DIR
  * $WEB_INSTALL_DIR

...respectively.

Set ownership and membership to the user and group respectively given as
$IMIP_AGENT_USER and $IMIP_AGENT_GROUP.
EOF
    exit 1
fi

chown -R "$IMIP_AGENT_USER" "$INSTALL_DIR"
chgrp -R "$IMIP_AGENT_GROUP" "$INSTALL_DIR"

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

if [ "$STORE_TYPE" = "file" ]; then
    for DIR in "$INSTALL_DIR"/store "$INSTALL_DIR"/journal ; do
        chown -R "$IMIP_AGENT_USER" "$DIR"
        chgrp -R "$IMIP_AGENT_GROUP" "$DIR"
        chmod -R g+w "$DIR"
    done
fi
