#!/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_install [option]..."
   echo -e "Reads the file debian/\$package.poms and installs each POM file"
   echo -e "listed in the .poms file, as well as the associated jars - assuming"
   echo -e "that at least the --artifact option is given in the .poms file for"
   echo -e "all jars to install."
   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>: name of the Debian package which"
   echo -e "\t will contain all the files to install"
   echo -e "\t-e<version>, --set-version=<version>: set the version for all artifacts,"
   echo -e "\t  do not use the version declared in the POM files."
   echo -e "\t-r<rules> --rules=<rules>: path to the file containing the"
   echo -e "\t  rules to apply when cleaning the POM files."
   echo -e "\t  Optional, the default location is 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"
   echo -e "\t  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  rules used to remove certain dependencies from the cleaned POM"
   echo -e "\t  Optional, the default location is debian/maven.ignoreRules"
   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-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 "The \$package.poms file must contain enough information to locate"
   echo -e "the jar files to install, and to associate them with their POM file."
   echo -e "For each POM file associated with a jar, we need to supply at least"
   echo -e "the --artifact parameter."
   echo -e ""
   echo -e "See also: mh_installpoms(1), mh_installjar(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)
NO_PUBLISH_USED_RULE=$(getarg no-publish-used-rule)
VERBOSE=$(getarg v verbose)
NOACT=$(getarg n no-act)

function installpackage()
{
  p=$1
  DH_OPTS="${VERBOSE:+-v} ${NOACT:+-n}"
  MH_ARGS="--package=${p} ${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}"

  cat debian/$p.poms | while read POM OPT1 OPT2 OPT3 OPT4 OPT5 OPT6 OPT7 OPT8 OPT9 OPT10; do
    # Remove comments
    POM=${POM##\#*}
    if [[ -n "$POM" ]]; then
      POM_DIR=$(dirname $POM)
      ARTIFACT=
      SITE_XML=
      C=1
      _opt=$(eval echo '$OPT'$C)
      while [ -n "$_opt" ]; do
        if [ "--artifact" = "${_opt%%=*}" ]; then
          export ARTIFACT=${_opt##--artifact=}
        fi
        if [ "--site-xml" = "${_opt%%=*}" ]; then
          export SITE_XML=${_opt##--site-xml=}
        fi
        if [ "--artifact" = "${_opt%%=*}" ] || [ "--site-xml" = "${_opt%%=*}" ]; then
           # Unset the option where we found the matching option
           eval OPT$C=
           # shift all following options
           while [ -n "$_opt" ]; do
            C1=$(( $C + 1 ))
            eval OPT$C='$OPT'$C1
            C=$C1
            _opt=$(eval echo '$OPT'$C)
           done
         fi
         C=$(( $C + 1 ))
         _opt=$(eval echo '$OPT'$C)
       done
       if [[ ! "--ignore" == "$OPT1" ]]; 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
        mh_installpom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM
        if [[ -n "$ARTIFACT" ]]; then
          if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
            echo -e "\tmh_installjar --skip-clean-pom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM $ARTIFACT"
          fi
          mh_installjar --skip-clean-pom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM $ARTIFACT
        fi
        if [[ -n "$SITE_XML" ]]; then
          if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
            echo mh_installsite --skip-clean-pom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM ${SITE_XML}
          fi
          mh_installsite --skip-clean-pom $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM ${SITE_XML}
        fi
      fi
    fi
  done
}

if [ -n "$PACKAGE" ]; then
  installpackage $PACKAGE
else
  for p in `findpackages`; do
    if [ -f debian/$p.poms ]; then
      installpackage $p
    fi
  done
fi
