#!/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_linkjar [option]... [pom] [dest_jar] [link]..."
   echo -e "Create symlinks for a jar installed by other means. The symlinks created"
   echo -e "include links to the jar in /usr/share/maven-repo, at the correct"
   echo -e "location for Maven."
   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[dest_jar] is the path of the installed jar, usually located in the"
   echo -e "\t  usr/share/java 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-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"
   exit 1
}

ARGS="p package e set-version r rules l java-lib n usj-name j usj-version s no-usj-versionless 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 j usj-version)
NO_USJ_VERSIONLESS=$(getarg s no-usj-versionless)
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]}"

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

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}

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
fi

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

	if [[ ("$srcDir" == "$destDir") && ("$srcJar" == "$destJar") ]]; then
		return
	fi

	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}
}

TARGET_DIR=$(dirname $JAR)
TARGET_JAR=$(basename $JAR)

# Install the link to the jar into the Maven repository
link_jar "$TARGET_DIR" "$TARGET_JAR" "$MVN_VERSIONED_DIR" "$VERSIONED_JAR_NAME"

if [[ "${version}" != "${debianVersion}" ]]; then
	link_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]}"
	link_jar "$TARGET_DIR" "$TARGET_JAR" "$(dirname $LINK_JAR)" "$(basename $LINK_JAR)"
done

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

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

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