#!/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_installjar [option]... [pom] [jar] [link]..."
   echo -e "Installs the jar file in /usr/share/maven-repo, at the correct location for"
   echo -e "Maven. The jar is copied into the build directory."
   echo -e "It can also create additional links to the jar, usually located in"
   echo -e "/usr/share/java."
   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 "\t[jar] is the path to the jar to install, usually located in the build"
   echo -e "\t  folder."
   echo -e "\t[link] is an additional link to the jar to install, usually there should"
   echo -e "\t  be a link to usr/share/java/\$jar.jar and"
   echo -e "\t  usr/share/java/\$jar-\$version.jar to comply with the Java packaging"
   echo -e "\t  guidelines. Note that there is no need to specify those particular"
   echo -e "\t  links if the --java-lib option is used."
   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 this jar file"
   echo -e "\t-e<version>, --set-version=<version>: set the version for the jar,"
   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  Maven rules are used here to extract the groupId, artifactId"
   echo -e "\t  and version from the POM file."
   echo -e "\t-l --java-lib: Optional, if given it will install the jar into"
   echo -e "\t  /usr/share/java to comply with the Debian Java specification."
   echo -e "\t  The jar will be installed as /usr/share/java/\$name-\$version.jar and"
   echo -e "\t  a versionless link /usr/share/java/\$name.jar will point to it, as"
   echo -e "\t  well as the links installed in /usr/share/maven-repo"
   echo -e "\t-n<name> --usj-name=<name>: Optional, the name to use when installing the"
   echo -e "\t  library in /usr/share/java when --java-lib is used."
   echo -e "\t  Defaults to the artifact id found in the POM."
   echo -e "\t-j<version> --usj-version=<version>: Optional, the version to use when"
   echo -e "\t  installing the library in /usr/share/java when --java-lib is used."
   echo -e "\t  Defaults to the version found in the POM."
   echo -e "\t-s --no-usj-versionless: Optional, don't install the versionless link"
   echo -e "\t  in /usr/share/java."
   echo -e "\t  This flag is used only when the -l or --java-lib option is given."
   echo -e "\t-d<path> --dest-jar=<path>: Optional, the destination for the real jar."
   echo -e "\t  The other places where the jar appears, in the repository or in the"
   echo -e "\t  list of links, will be symlinks to this jar."
   echo -e "\t  Defaults to /usr/share/java/\$name-\$version.jar if --java-lib is used,"
   echo -e "\t  otherwise the jar is installed in the versioned path in the Maven repository."
   echo -e "\t-c<classifier> --classifier=<classifier>: Optional, the classifier for"
   echo -e "\t  the jar. Empty by default."
   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--skip-clean-pom: don't clean the pom, assume that a previous action ran"
   echo -e "\t  mh_cleanpom with the correct options. mh_cleanpom is run only to extract"
   echo -e "\t  the groupId, artifactId and version of the jar"
   echo -e ""
   echo -e "See also: mh_installpom(1), mh_installsite(1)"
   exit 1
}

# The following elements are options which just need to be ignored: no-parent has-package-version keep-elements ignore-pom
ARGS="p package e set-version r rules l java-lib n usj-name i j usj-version s no-usj-versionless d dest-jar c classifier v verbose n no-act skip-clean-pom no-parent has-package-version keep-elements ignore-pom relocate" parseargs "$@"

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

SETVERSION=$(getarg e set-version)
RULES=$(getarg r rules)
PACKAGE=$(getarg p package)
PACKAGE=${PACKAGE:?"Package parameter (-p) is mandatory"}
JAVALIB=$(getarg l java-lib)
USJ_JAR_NAME=$(getarg n usj-name)
USJ_JAR_VERSION=$(getarg i j usj-version)
NO_USJ_VERSIONLESS=$(getarg s no-usj-versionless)
TARGET_JAR_PATH=$(getarg d dest-jar)
CLASSIFIER=$(getarg c classifier)
VERBOSE=$(getarg v verbose)
NOACT=$(getarg n no-act)
SKIP_CLEAN_POM=$(getarg skip-clean-pom)
POM="${ARGV[0]}"
JAR="${ARGV[1]}"

if [ -n "$(getarg i)" ]; then
  echo "WARNING: mh_installjar -i option is deprecated, prefer -j or --usj-version instead"
fi

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

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

if [ -z "$SKIP_CLEAN_POM" ]; then
    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 $POM debian/.mh/pom.xml debian/.mh/pom.properties
fi

source debian/.mh/pom.properties

groupPath=$(echo $groupId | tr . / )
if [ -z "$CLASSIFIER" ]; then
    # Use the classifier from the POM
    CLASSIFIER=$classifier
fi

if [ ! -e $JAR ]; then
    echo "Cannot find the jar to install: $JAR"
    exit 2
fi

VERSIONED_JAR_NAME="${artifactId}-${version}.jar"
if [ ! -z "$CLASSIFIER" ]; then
    VERSIONED_JAR_NAME="${artifactId}-${version}-${CLASSIFIER}.jar"
fi

DEBIAN_JAR_NAME="${artifactId}-${debianVersion}.jar"
if [ ! -z "$CLASSIFIER" ]; then
	DEBIAN_JAR_NAME="${artifactId}-${debianVersion}-${CLASSIFIER}.jar"
fi

MVN_VERSIONED_DIR=usr/share/maven-repo/${groupPath}/${artifactId}/${version}
MVN_DEBIAN_DIR=usr/share/maven-repo/${groupPath}/${artifactId}/${debianVersion}

TARGET_DIR=${MVN_VERSIONED_DIR}
TARGET_JAR=${VERSIONED_JAR_NAME}

if [ -n "$JAVALIB" ]; then
    USJ_JAR_NAME=$(getarg n usj-name)
    USJ_JAR_NAME=${USJ_JAR_NAME:-$artifactId}
    USJ_JAR_VERSION=${USJ_JAR_VERSION:-$version}
	USJ_VERSIONED_JAR_NAME=${USJ_JAR_NAME}-${USJ_JAR_VERSION}.jar
    USJ_JAR_NAME=${USJ_JAR_NAME}.jar
	TARGET_DIR=usr/share/java
	TARGET_JAR=${USJ_VERSIONED_JAR_NAME}
fi

if [[ -n "$TARGET_JAR_PATH" ]]; then
	TARGET_DIR=$(dirname $TARGET_JAR_PATH)
	TARGET_JAR=$(basename $TARGET_JAR_PATH)
fi

JAR_INSTALLED=

install_jar ()
{
	local srcDir=$1
	local srcJar=$2
	local destDir=$3
	local destJar=$4

	if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
		echo "mh_installjar: Install $srcDir $srcJar to $destDir $destJar"
	fi

	if [[ ("$destDir" == "$TARGET_DIR") && ("$destJar" == "$TARGET_JAR") ]]; then
		# avoid duplication as install_jar is called twice
		if [[ -n "$JAR_INSTALLED" ]]; then 
			return 
		fi
		cp ${srcJar} debian/.mh/${destJar}
		if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
			echo -e "\tinstall -m 644 -D debian/.mh/${destJar} debian/${PACKAGE}/${destDir}/${destJar}"
		fi
		install -m 644 -D debian/.mh/${destJar} debian/${PACKAGE}/${destDir}/${destJar} 

		JAR_INSTALLED=done
	else
		if [[ ! -z "$VERBOSE" || "$DH_VERBOSE" = "1" ]]; then
			echo -e "\tdh_link $DH_OPTS -p${PACKAGE} ${srcDir}${srcJar} ${destDir}/${destJar}"
		fi
	    dh_link $DH_OPTS -p${PACKAGE} ${srcDir}/${srcJar} ${destDir}/${destJar}
	fi
}

# Install the jar in its target directory
install_jar "" "$JAR" "$TARGET_DIR" "$TARGET_JAR"

# Install the jar in the Maven repository
install_jar "$TARGET_DIR" "$TARGET_JAR" "$MVN_VERSIONED_DIR" "$VERSIONED_JAR_NAME"

if [[ "${version}" != "${debianVersion}" ]]; then
	install_jar "$TARGET_DIR" "$TARGET_JAR" "$MVN_DEBIAN_DIR" "$DEBIAN_JAR_NAME"
fi

# Create the additional links supplied on the argument list
for (( i=2; i < $ARGC; i++ )); do
    LINK_JAR="${ARGV[i]}"
	install_jar "$TARGET_DIR" "$TARGET_JAR" "$(dirname $LINK_JAR)" "$(basename $LINK_JAR)"
done

# Install the jar in /usr/share/java
if [ -n "$JAVALIB" ]; then

	install_jar "$TARGET_DIR" "$TARGET_JAR" "usr/share/java" "$USJ_VERSIONED_JAR_NAME"

	if [[ -z "$NO_USJ_VERSIONLESS" ]]; then
		install_jar "$TARGET_DIR" "$TARGET_JAR" "usr/share/java" "$USJ_JAR_NAME"
	fi
fi
