#!/bin/sh
#
# Manage /etc/svc-runner/htpasswd.d/svc.htpasswd
#

# include prerequisites
. /etc/default/svc-runner

if test -z "$SVCRUNNER_ETC" -o -z "$SVCRUNNER_USER" -o -z "$SVCRUNNER_ADMIN" -o -z "$SVCRUNNER_GROUP" -o -z "$JAVA_HOME"
then
    echo "ERROR: SVCRUNNER_ETC, SVCRUNNER_USER, SVCRUNNER_ADMIN, SVCRUNNER_GROUP and JAVA_HOME not specified in /etc/default/svc-runner"
    exit 64
fi

if test $# -lt 1
then
    echo "No command given, first argument mus be [adduser] or [deluser]."
    exit 64
fi

cmd=$1

shift 1

HTPASSWD="$SVCRUNNER_ETC/htpasswd.d/svc.htpasswd"

if test ! -d "$SVCRUNNER_ETC/htpasswd.d"
then
    sudo -u $SVCRUNNER_ADMIN -g $SVCRUNNER_GROUP mkdir -p -m 750 "$SVCRUNNER_ETC/htpasswd.d"
fi
    
sudo -u $SVCRUNNER_ADMIN -g $SVCRUNNER_GROUP "${JAVA_HOME}/bin/java" -Dsvc.runner.etcPath="$SVCRUNNER_ETC" -jar /usr/share/java-modules/clazzes/htpasswd-jaas-*.jar "$cmd" "$HTPASSWD" "$@"
chmod 640 "$HTPASSWD"
