#!/bin/bash --

# Copyright 2011 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_patchpoms [option]..."
   echo -e "Reads the file debian/\$package.poms and tranform each POM file"
   echo -e "listed in the .poms file into a POM file using the Debian versions"
   echo -e "of the libraries. Also keeps a backup of each POM file which can"
   echo -e "be restored with mh_unpatchpoms"
   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-k --keep-pom-version: keep the original version of the POMs but, "
   echo -e "\t  convert all other versions in dependencies and plugins"
   echo -e "\t  If there is a parent POM, keeps its version except when the parent is already"
   echo -e "\t  registered in the Maven repository"
   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>: path to the file containing the"
   echo -e "\t  rules to apply when cleaning the POM."
   echo -e "\t  Optional, the default location is debian/maven.rules"
   echo -e "\t-R<rule>, --extra-rule=<rule>: extra rule to apply when cleaning the POM"
   echo -e "\t  May occur multiple times, instead of or in addition to -r"
   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"
   echo -e "\t  cleaned POM."
   echo -e "\t  Optional, the default location is debian/maven.publishedRules"
   echo -e "\t-U<rule>, --extra-published-rule=<rule>: extra rule to publish"
   echo -e "\t  May occur multiple times, instead of or in addition to -u"
   echo -e "\t-i<rules> --ignore-rules=<rules>: path to the file containing the"
   echo -e "\t  rules used to remove certain dependencies from the cleaned POM"
   echo -e "\t  Optional, the default location is debian/maven.ignoreRules"
   echo -e "\t-I<rule>, --extra-ignore-rule=<rule>: extra rule used to remove dependencies"
   echo -e "\t  from the transformed POM"
   echo -e "\t  May occur multiple times, instead of or in addition to -i"
   echo -e "\t-c<rules> --clean-ignore-rules=<rules>: path to the file containing the"
   echo -e "\t  rules use to remove certain dependencies from the cleaned POM,"
   echo -e "\t  in addition to the ignore rules specified previously. This is"
   echo -e "\t  useful in situations such as when the Maven clean target requires more"
   echo -e "\t  dependencies or plugins to ignore than the build target."
   echo -e "\t  All rules defined in clean-ignore-rules will be added to the existing"
   echo -e "\t  rules in ignore-rules."
   echo -e "\t  Optional, the default location is debian/maven.cleanIgnoreRules"
   echo -e "\t-s --no-rules: don't apply any rules for converting versions,"
   echo -e "\t  do not even convert versions to the default 'debian' version"
   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-d --debian-build: transform during a Debian build, which means that"
   echo -e "\t  some POM elements will be removed"
   echo -e "\t-b --build-no-docs: if the build doesn't need to build documentation, use this"
   echo -e "\t  option to remove some POM elements (in particular plugins) which are useless here"
   echo -e "\t  and may require extra dependencies and make the packaging harder."
   echo -e "\t-m<repo root>--maven-repo=<repo root>: location of the Maven repository,"
   echo -e "\t  used to force the versions of the Maven plugins used in the current"
   echo -e "\t  POM file with the versions found in the repository"
   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_installpoms(1), mh_patchpom(1)"
   exit 1
}

ARGS="p package keep-pom-version e set-version r rules R extra-rule u published-rules U extra-published-rule i ignore-rules I extra-ignore-rule c clean-ignore-rules s no-rules no-publish-used-rule v verbose n no-act d debian-build b build-no-docs m maven-repo" parseargs "$@"

KEEP_POM_VERSION=$(getarg k keep-pom-version)
SETVERSION=$(getarg e set-version)
RULES=$(getarg r rules)
eval EXTRA_RULES=$(getargs R extra-rule)
PUBLISHED_RULES=$(getarg u published-rules)
eval EXTRA_PUBLISHED_RULES=$(getargs U extra-published-rule)
IGNORE_RULES=$(getarg i ignore-rules)
eval EXTRA_IGNORE_RULES=$(getargs I extra-ignore-rule)
CLEAN_IGNORE_RULES=$(getarg c clean-ignore-rules)
NORULES=$(getarg s no-rules)
NO_PUBLISH_USED_RULE=$(getarg no-publish-used-rule)
MAVEN_REPO=$(getarg m maven-repo)
PACKAGE=$(getarg p package)
PACKAGE=${PACKAGE:?"Package parameter (-p) is mandatory"}
VERBOSE=$(getarg v verbose)
NOACT=$(getarg n no-act)
DEBIAN_BUILD=$(getarg d debian-build)
BUILD_NO_DOCS=$(getarg b build-no-docs)

if [ -z "$PUBLISHED_RULES" ]; then
    if [ -f debian/maven.publishedRules ]; then
        PUBLISHED_RULES="debian/maven.publishedRules"
    fi
fi
if [ -z "$IGNORE_RULES" ]; then
    if [ -f debian/maven.ignoreRules ]; then
        IGNORE_RULES="debian/maven.ignoreRules"
    fi
fi
if [ -z "$RULES" ]; then
    if [ -f debian/maven.rules ]; then
        RULES="debian/maven.rules"
    fi
fi
if [ -z "$MAVEN_REPO" ]; then
    if [ -f /usr/share/maven-repo ]; then
        MAVEN_REPO="/usr/share/maven-repo"
    fi
fi

DH_OPTS="${VERBOSE:+-v} ${NOACT:+-n}"
MH_ARGS=(--package=${PACKAGE} ${NORULES:+--no-rules} ${KEEP_POM_VERSION:+--keep-pom-version} ${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} ${CLEAN_IGNORE_RULES:+--clean-ignore-rules=$CLEAN_IGNORE_RULES} ${DEBIAN_BUILD:+--debian-build} ${BUILD_NO_DOCS:+--build-no-docs} ${MAVEN_REPO:+--maven-repo=$MAVEN_REPO} "${EXTRA_RULES[@]/#/--extra-rule=}" "${EXTRA_PUBLISHED_RULES[@]/#/--extra-published-rule=}" "${EXTRA_IGNORE_RULES[@]/#/--extra-ignore-rule=}")

if [ -z "$NOACT" ]; then
    cat debian/$PACKAGE.poms | while read POM OPT1 OPT2; do
        # Remove comments
        POM=${POM##\#*}
        if [ ! -z "$POM" ]; then
            cp $POM $POM.save
        fi
    done
    java -cp $CLASSPATH $JAVA_OPTIONS org.debian.maven.repo.POMTransformer $DH_OPTS "${MH_ARGS[@]}"
fi

