#!/bin/bash --

# Copyright 2009 Ludovic Claude.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

. /usr/share/maven-repo-helper/mh_lib.sh

syntax()
{
   echo -e "Usage: mh_installpoms [option]..."
   echo -e "Reads the file debian/\$package.poms and installs each POM file"
   echo -e "listed in the .poms file."
   echo -e ""
   echo -e "Options:"
   echo -e "\t-h --help: show this text"
   echo -e "\t-V --version: show the version"
   echo -e "\t-p<package> --package=<package>: package to act on "
   echo -e "\t--no-publish-used-rule: don't publish the rule used to transform"
   echo -e "\t  a POM's own attributes in debian.mavenRules"
   echo -e "\t-e<version>, --set-version=<version>: set the version for the POM,"
   echo -e "\t  do not use the version declared in the POM file."
   echo -e "\t-r<rules> --rules=<rules>: gives the location of the rules file for"
   echo -e "\t  special properties. Optional, the default location is"
   echo -e "\t  debian/maven.rules"
   echo -e "\t-u<rules> --published-rules=<rules>: path to the file containing the"
   echo -e "\t  extra rules to publish in the property debian.mavenRules in the cleaned POM"
   echo -e "\t  Optional, the default location is debian/maven.publishedRules"
   echo -e "\t-i<rules> --ignore-rules=<rules>: path to the file containing the"
   echo -e "\t  extra rules use to remove certain dependencies from the cleaned POM"
   echo -e "\t  Optional, the default location is debian/maven.ignoreRules"
   echo -e "\t-v --verbose: show more information while running"
   echo -e "\t-n --no-act: don't actually do anything, just print the results"
   echo -e ""
   echo -e "See also: mh_installpom(1), mh_cleanpom(1)"
   exit 1
}

ARGS="p package no-publish-used-rule r rules u published-rules i ignore-rules e set-version v verbose n no-act" parseargs "$@"

RULES=$(getarg r rules)
PUBLISHED_RULES=$(getarg u published-rules)
IGNORE_RULES=$(getarg i ignore-rules)
SETVERSION=$(getarg e set-version)
PACKAGE=$(getarg p package)
PACKAGE=${PACKAGE:?"Package parameter (-p) is mandatory"}
NO_PUBLISH_USED_RULE=$(getarg no-publish-used-rule)
VERBOSE=$(getarg v verbose)
NOACT=$(getarg n no-act)

DH_OPTS="${VERBOSE:+-v} ${NOACT:+-n}"
MH_ARGS="--package=${PACKAGE} ${VERBOSE:+--verbose} ${NO_PUBLISH_USED_RULE:+--no-publish-used-rule} ${SETVERSION:+--set-version=$SETVERSION} ${RULES:+--rules=$RULES} ${PUBLISHED_RULES:+--published-rules=$PUBLISHED_RULES} ${IGNORE_RULES:+--ignore-rules=$IGNORE_RULES}"

if [ -z "$NOACT" ]; then
    cat debian/$PACKAGE.poms | while read POM OPT1 OPT2 OPT3 OPT4 OPT5 OPT6 OPT7 OPT8 OPT9 OPT10; do
        # Remove comments
        POM=${POM##\#*}
        if [[ ! -z "$POM" ]]; then
            if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
	        echo -e "\tmh_installpom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM"
            fi
            if [[ ! "--ignore" == "$OPT1" ]]; then
                mh_installpom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM
            fi
        fi
    done
fi
