#!/bin/bash --

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-o --no-parent: don't inherit from a parent POM"
   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_installpoms(1), mh_cleanpom(1)"
   exit 1
}

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

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

NOPARENT=$(getarg o no-parent)
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)
POM="${ARGV[0]}"

DH_OPTS="${VERBOSE:+-v} ${NOACT:+-n}"
CLEAN_ARGS="--package=${PACKAGE} ${VERBOSE:+--verbose} ${SETVERSION:+--set-version=$SETVERSION} ${RULES:+--rules=$RULES} ${PUBLISHED_RULES:+--published-rules=$PUBLISHED_RULES} ${IGNORE_RULES:+--ignore-rules=$IGNORE_RULES}"

mkdir -p debian/tmp 2> /dev/null

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

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

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

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

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

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

install -m 644 -D debian/tmp/${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/tmp/${artifactId}-${debianVersion}.pom debian/${PACKAGE}/usr/share/maven-repo/${groupPath}/${artifactId}/${debianVersion}/${artifactId}-${debianVersion}.pom"
    fi

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