#!/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_linkjars [option]..."
   echo -e "Reads the file debian/\$package.poms and create links for each"
   echo -e "jar file generated by a POM 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>: name of the Debian package which"
   echo -e "\t  will contain the jar files"
   echo -e "\t-e<version>, --set-version=<version>: set the version for the jars,"
   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-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 should contain the list of POM files associated with the"
   echo -e "list of jars to install in the repository, and each pom file should have either"
   echo -e "the option --usj-name, giving the name of the jar (without the extension)"
   echo -e "to link to and located in /usr/share/java, or the option --artifact, which should"
   echo -e "contain the full name of the source jar to link to."
   echo -e ""
   echo -e "See also: mh_linkjar(1), mh_install(1)"
   exit 1
}

ARGS="p package e set-version r rules v verbose n no-act skip-clean-poms" parseargs "$@"

SETVERSION=$(getarg e set-version)
RULES=$(getarg r rules)
PACKAGE=$(getarg p package)
VERBOSE=$(getarg v verbose)
NOACT=$(getarg n no-act)
SKIP_CLEAN_POMS=$(getarg skip-clean-poms)

function linkpackagejars()
{
  p=$1

  DH_OPTS="${VERBOSE:+-v} ${NOACT:+-n}"
  MH_ARGS="--package=${p} ${VERBOSE:+--verbose} ${SETVERSION:+--set-version=$SETVERSION} ${RULES:+--rules=$RULES} ${SKIP_CLEAN_POMS:+--skip-clean-pom}"

  if [ -z "$NOACT" ]; then
    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=
        C=1
        _opt=$(eval echo '$OPT'$C)
        while [ -n "$_opt" ]; do
          if [ "--usj-name" = "${_opt%%=*}" ]; then
            export ARTIFACT=/usr/share/java/${_opt##--usj-name=}.jar
            # Unset the option where we found the artifact
            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
          elif [ "--artifact" = "${_opt%%=*}" ]; then
            export ARTIFACT=${_opt##--artifact=}
            # Unset the option where we found the artifact
            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 [[ -n "$ARTIFACT" ]]; then
            if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
              echo -e "\tmh_linkjar $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM $ARTIFACT"
            fi
            mh_linkjar $OPT1 $OPT2 $OPT3 $OPT4 $OPT5 $OPT6 $OPT7 $OPT8 $OPT9 $OPT10 $DH_OPTS $MH_ARGS $POM $ARTIFACT
          fi
        fi
      fi
    done
  fi
}

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