#!/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_installpom [option]... [pom]"
   echo -e "Installs the POM file in /usr/share/maven-repo, at the correct location for"
   echo -e "Maven."
   echo -e "Before installing the POM, it prepares it with mh_cleanpom."
   echo -e ""
   echo -e "debian/maven.rules is used to alter the version properties for the library and"
   echo -e "its dependencies."
   echo -e ""
   echo -e "Prefer to use mh_installpoms as it reuses the information in"
   echo -e "debian/\$package.poms and avoids repetition."
   echo -e ""
   echo -e "Where"
   echo -e "\t[pom] is the location of the POM associated with the jar to install."
   echo -e "\t  GroupId, artifactId and version will be extracted from this file."
   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-h --has-package-version: flag that indicates that this POM has the"
   echo -e "\t  same version as the package, this helps packagers of depending packages"
   echo -e "\t-o --no-parent: don't inherit from a parent POM"
   echo -e "\t--keep-elements=<elem1,elem2>: keep the elements listed here"
   echo -e "\t  even if they are normally removed by the clean operation."
   echo -e "\t  Such elements are build,reports,reporting,prerequisites,profiles."
   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 "\t-ignore-pom: read the POM but don't install it"
   echo -e ""
   echo -e "See also: mh_installpoms(1), mh_cleanpom(1)"
   exit 1
}

# The following elements are options which just need to be ignored: artifact java-lib usj-name usj-version no-usj-versionless dest-jar classifier
ARGS="p package o no-parent no-publish-used-rule r rules u published-rules i ignore-rules e set-version v verbose n no-act h has-package-version keep-elements artifact java-lib usj-name usj-version no-usj-versionless dest-jar ignore-pom classifier" parseargs "$@"

if [ "$ARGC" -lt "1" ]; then
   syntax
fi

NOPARENT=$(getarg o no-parent)
NO_PUBLISH_USED_RULE=$(getarg no-publish-used-rule)
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"}
VERBOSE=$(getarg v verbose)
NOACT=$(getarg n no-act)
HAS_PACKAGE_VERSION=$(getarg h has-package-version)
KEEP_ELEMENTS=$(getarg keep-elements)
IGNORE_POM=$(getarg ignore-pom)
POM="${ARGV[0]}"

DH_OPTS="${VERBOSE:+-v} ${NOACT:+-n}"
CLEAN_ARGS="--package=${PACKAGE} ${VERBOSE:+--verbose} ${NOPARENT:+--no-parent} ${HAS_PACKAGE_VERSION:+--has-package-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} ${KEEP_ELEMENTS:+--keep-elements=$KEEP_ELEMENTS}"

mkdir -p debian/.mh 2> /dev/null

if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
    echo -e "\tmh_cleanpom $DH_OPTS $CLEAN_ARGS $POM debian/.mh/pom.xml debian/.mh/pom.properties"
fi

mh_cleanpom $DH_OPTS $CLEAN_ARGS --keep-pom-version $POM debian/.mh/pom.xml.keep debian/.mh/pom.properties.keep
mh_cleanpom $DH_OPTS $CLEAN_ARGS $POM debian/.mh/pom.xml debian/.mh/pom.properties
source debian/.mh/pom.properties

groupPath=$(echo $groupId | tr . / )

if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
    echo -e "\tmv debian/.mh/pom.xml debian/.mh/${artifactId}-${debianVersion}.pom"
fi

if [ -n "${IGNORE_POM}" ]; then
    exit
fi

mv debian/.mh/pom.xml.keep debian/.mh/${artifactId}-${version}.pom
mv debian/.mh/pom.xml debian/.mh/${artifactId}-${debianVersion}.pom

if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
    echo -e "\tinstall -m 644 -D debian/.mh/${artifactId}-${version}.pom debian/${PACKAGE}/usr/share/maven-repo/${groupPath}/${artifactId}/${version}/${artifactId}-${version}.pom"
fi

install -m 644 -D debian/.mh/${artifactId}-${version}.pom debian/${PACKAGE}/usr/share/maven-repo/${groupPath}/${artifactId}/${version}/${artifactId}-${version}.pom

if [[ "${version}" != "${debianVersion}" ]]; then
    if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
        echo -e "\tinstall -m 644 -D debian/.mh/${artifactId}-${debianVersion}.pom debian/${PACKAGE}/usr/share/maven-repo/${groupPath}/${artifactId}/${debianVersion}/${artifactId}-${debianVersion}.pom"
    fi

    install -m 644 -D debian/.mh/${artifactId}-${debianVersion}.pom debian/${PACKAGE}/usr/share/maven-repo/${groupPath}/${artifactId}/${debianVersion}/${artifactId}-${debianVersion}.pom
fi
