#! /bin/sh

JABBER_XSL_DIR=/usr/lib/jabber/xsl

XSLTPROC=/usr/bin/xsltproc
PWGEN=/usr/bin/pwgen

PROGNAME=jabber-conftool

Usage() {
    cat <<EOF
Usage: $PROGNAME --gen-secret FILE...
       $PROGNAME --add-service PATHNAME FILE
       $PROGNAME --remove-service PATHNAME FILE
       $PROGNAME --add-browse PATHNAME FILE
       $PROGNAME --remove-browse PATHNAME FILE

Actions:
--gen-secret        Generate a secret phrase and put it
                        into the <secret/> elements of all specified files
--add-service       Add a <jabberd:include/> element containing PATHNAME
                        to the service configuration part in FILE
--remove-service    Remove the <jabberd:include/> element containing PATHNAME
                        from the service configuration part in FILE
--add-browse        Add a <jabberd:include/> element containing PATHNAME
                        to the browse configuration part in FILE
--remove-browse     Remove the <jabberd:include/> element containing PATHNAME
                        from the browse configuration part in FILE
EOF
}

ARGS=$(getopt --name $PROGNAME -o h \
     --long help,gen-secret,add-service:,remove-service:,add-browse:,remove-browse: \
     -- "$@")
[ $? = 0 ] || exit $?

eval set -- "$ARGS"

while :; do
    case "$1" in
        -h|--help)
            Usage
            exit 0
            ;;
        --gen-secret)
            action=gen-secret
            ;;
        --add-service|--remove-service|--add-browse|--remove-browse)
            action=${1#--}
            include=$2
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "$PROGNAME: invalid option \`$1'" >&2
            USAGE=$(Usage)
            echo "$USAGE" >&2
            exit 1
            ;;
    esac
    shift
done

GenSecret() {
    local password timestamp

    password=$("$PWGEN" -s -N 1)
    timestamp=$(LC_TIME=C /bin/date)

    for f in "$@"; do
        "$XSLTPROC" --output "$f" - "$f" <<EOF
<?xml version="1.0"?>
<xsl:transform version="1.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="file://$JABBER_XSL_DIR/jabber-gen-secret.xsl"/>
  <xsl:param name="secret">$password</xsl:param>
  <xsl:param name="timestamp">$timestamp</xsl:param>
</xsl:transform>
EOF
    done
}

AddService() {
    local include config
    include=$1
    config=$2
    "$XSLTPROC" --stringparam pathname "$include" --output "$config" \
        $JABBER_XSL_DIR/jabber-add-service-include.xsl "$config"
}

RemoveService() {
    local include config
    include=$1
    config=$2
    "$XSLTPROC" --stringparam pathname "$include" --output "$config" \
        $JABBER_XSL_DIR/jabber-remove-service-include.xsl "$config"
}

AddBrowse() {
    local include config
    include=$1
    config=$2
    "$XSLTPROC" --stringparam pathname "$include" --output "$config" \
        $JABBER_XSL_DIR/jabber-add-browse-include.xsl "$config"
}

RemoveBrowse() {
    local include config
    include=$1
    config=$2
    "$XSLTPROC" --stringparam pathname "$include" --output "$config" \
        $JABBER_XSL_DIR/jabber-remove-browse-include.xsl "$config"
}


if [ $action = gen-secret ]; then
    GenSecret "$@"
else
    if [ $# -eq 0 ]; then
        echo "$PROGNAME: file not given" >&2
        USAGE=$(Usage)
        echo "$USAGE" >&2
        exit 1
    fi
    if [ $# -gt 1 ]; then
        echo "$PROGNAME: too many parameters for --$action" >&2
        USAGE=$(Usage)
        echo "$USAGE" >&2
        exit 1
    fi
    config=$1
    # FIXME: sanity check on $include and $config
    case $action in
        add-service)    AddService "$include" "$config" ;;
        remove-service) RemoveService "$include" "$config" ;;
        add-browse)     AddBrowse "$include" "$config" ;;
        remove-browse)  RemoveBrowse "$include" "$config" ;;
    esac
fi
