#!/bin/bash

# Copyright: 2014 Rick Salevsky <rsalevsky@suse.com>
#
# This software may be used and distributed according to the terms of
# the GNU General Public License (GPL), version 3, or at your option
# any later version.

script_usage()
{
echo "
xml-group-manager (xgm)
-------------------------------------------------------------------

Usage:
xml-group-manager [option] [file]


Options:

  -a GROUP, --add GROUP        add the XML group with the content
                               from the given file
  -d GROUP, --delete GROUP     delete the XML group
  -u GROUP, --update GROUP     update the XML group with the content
                               from the given file
  -i GROUP, --init GROUP       creates the XML group and
                               set delegatePublic and
                               delegateSystem to the given file

  -p STRING, --public STRING   defines the publicIdStartString for
                               the init group command
  -s STRING, --system STRING   defines the systemIdStartString for
                               the init group command

  -c FILE, --catalog FILE      set the output catalog file if
                               not specified /etc/xml/catalog
                               is used

  -h, --help                   print this help
"
}

init()
{
    rootdir=$(dirname "$(readlink -e "$0")")
    if [[ -e $rootdir/xslt/add-group.xsl ]];then
        add_stylesheet="$rootdir/xslt/add-group.xsl"
    else
        if [[ -e /usr/share/xml-group-manager/xslt/add-group.xsl ]];then
            add_stylesheet="/usr/share/xml-group-manager/xslt/add-group.xsl"
        else
            echo "Error: Cannot find the xml-group-manager stylesheets!"
            exit 1
        fi
    fi
    if [[ -e $rootdir/xslt/delete-group.xsl ]];then
        delete_stylesheet="$rootdir/xslt/delete-group.xsl"
    else
        if [[ -e /usr/share/xml-group-manager/xslt/delete-group.xsl ]];then
            delete_stylesheet="/usr/share/xml-group-manager/xslt/delete-group.xsl"
        else
            echo "Error: Cannot find the xml-group-manager stylesheets!"
            exit 1
        fi
    fi
    if [[ -e $rootdir/xslt/update-group.xsl ]];then
        update_stylesheet="$rootdir/xslt/update-group.xsl"
    else
        if [[ -e /usr/share/xml-group-manager/xslt/update-group.xsl ]];then
            update_stylesheet="/usr/share/xml-group-manager/xslt/update-group.xsl"
        else
            echo "Error: Cannot find the xml-group-manager stylesheets!"
            exit 1
        fi
    fi
    if [[ -e $rootdir/xslt/init-group.xsl ]];then
        init_stylesheet="$rootdir/xslt/init-group.xsl"
    else
        if [[ -e /usr/share/xml-group-manager/xslt/init-group.xsl ]];then
            init_stylesheet="/usr/share/xml-group-manager/xslt/init-group.xsl"
        else
            echo "Error: Cannot find the xml-group-manager stylesheets!"
            exit 1
        fi
    fi
    if [[ -z $catalog ]];then
        if [[ -e /etc/xml/catalog ]];then
            catalog=/etc/xml/catalog
        else
            echo "Error: Cannot find the xml catalog!"
            exit 1
        fi
    fi
}

prepair_files()
{
    tmp_catalog=$(mktemp --tmpdir xmg-catalog-XXXXXXXX.xml)
    tmp_input_file=$(mktemp --tmpdir xmg-input-XXXXXXXX.xml)
    cp $file $tmp_input_file
    sed -i '1 i\<group id="'$group'" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">' $tmp_input_file
    sed -i '$a </group>' $tmp_input_file
}

prepair_file()
{
    tmp_catalog=$(mktemp --tmpdir xmg-catalog-XXXXXXXX.xml)
}

add_group()
{
    if [[ -n $group && -e $file ]];then
        prepair_files
        xsltproc --nonet \
                 --stringparam xmlfile $tmp_input_file \
                 --stringparam group-id "$group" \
                 $add_stylesheet $catalog > $tmp_catalog
        if [[ $? -ne 0 ]]; then
            rm -- $tmp_catalog $tmp_input_file
            exit 1
        else
            xmllint --format $tmp_catalog -o $catalog
            rm -- $tmp_catalog $tmp_input_file
            exit 0
        fi
    else
        echo "Error: Group or input file is missing!"
        exit 1
    fi
}

delete_group()
{
    if [[ -n $group ]];then
        prepair_file
        xsltproc --nonet \
                 --stringparam group-id "$group" \
                 $delete_stylesheet $catalog > $tmp_catalog
        if [[ $? -ne 0 ]]; then
            rm -- $tmp_catalog
            exit 1
        else
            xmllint --format $tmp_catalog -o $catalog
            rm -- $tmp_catalog
            exit 0
        fi
    else
        echo "Error: Group is missing!"
        exit 1
    fi
}

update_group()
{
    if [[ -n $group && -e $file ]];then
        prepair_files
        xsltproc --nonet \
                 --stringparam xmlfile $tmp_input_file \
                 --stringparam group-id "$group" \
                 $update_stylesheet $catalog > $tmp_catalog
        if [[ $? -ne 0 ]]; then
            rm -- $tmp_catalog $tmp_input_file
            exit 1
        else
            xmllint --format $tmp_catalog -o $catalog
            rm -- $tmp_catalog $tmp_input_file
            exit 0
        fi
    else
        echo "Error: Group or input file is missing!"
        exit 1
    fi
}

init_group()
{
    if [[ -n $group && -e $file ]];then
        if [[ -n $public && -n $system ]];then
            prepair_file
            xsltproc --nonet \
                     --stringparam targetfile "$file" \
                     --stringparam group-id "$group" \
                     --stringparam publicIdStartString "$public" \
                     --stringparam systemIdStartString "$system" \
                     $init_stylesheet $catalog > $tmp_catalog
            if [[ $? -ne 0 ]]; then
                rm -- $tmp_catalog
            else
                xmllint --format $tmp_catalog -o $catalog
                rm -- $tmp_catalog
                exit 0
            fi
        else
            echo "Error: publicIdStartString or systemIdStartString is missing!"
            exit 1
        fi
    else
        echo "Error: Group or file is missing!"
        exit 1
    fi
}

ARGS=$(getopt -o a:d:u:i:p:s:c:h -l "add:,delete:,update:,init:,public:,system:,catalog:,help" -n "$0" -- "$@");
while true; do
    case "$1" in
        -h|--help)
            shift;
            script_usage
            exit 0
            ;;
        -a|--add)
            shift;
            if [[ -n "$1" ]]; then
                group=$1;
                action=add;
                shift;
            fi
            ;;
        -d|--delete)
            shift;
            if [[ -n "$1" ]]; then
                group=$1;
                action=delete;
                shift;
            fi
            ;;
        -u|--update)
            shift;
            if [[ -n "$1" ]]; then
                group=$1;
                action=update;
                shift;
            fi
            ;;
        -i|--init)
            shift;
            if [[ -n "$1" ]]; then
                group=$1;
                action=init;
                shift;
            fi
            ;;
        -p|--public)
            shift;
            if [[ -n "$1" ]]; then
                public=$1;
                shift;
            fi
            ;;
        -s|--system)
            shift;
            if [[ -n "$1" ]]; then
                system=$1;
                shift;
            fi
            ;;
        -c|--catalog)
            shift;
            if [[ -n "$1" ]]; then
                catalog=$(readlink -e "$1");
                shift;
            fi
            ;;
        --)
            shift;
            break;
            ;;
        *)
            break;
            ;;
    esac
done
file=$(readlink -e "$@" 2>/dev/null);

case "$action" in
    add)
        init
        add_group
        ;;
    delete)
        init
        delete_group
        ;;
    update)
        init
        update_group
        ;;
    init)
        init
        init_group
        ;;
    *)
        echo "Error: No valid action given!"
        script_usage
        exit 1
        ;;
esac
