#!/bin/bash
# Copyright (c) 2013-2017 Red Hat, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of Red Hat nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors: Mikolaj Izdebski <mizdebsk@redhat.com>

set -e

if ! test -f config.status; then
    echo config.status does not exist. Run configure first. >&2
    exit 1
fi
. ./config.status

DEST=${DEST:-${RPM_BUILD_ROOT}}
DEST=${DEST:-${PWD}/root}
rm -rf "${DEST}"

dir()
{
    install -d -m 755 "${DEST}/${1}"
    echo "%dir ${1}"
}

link()
{
    install -d -m 755 "${DEST}/"$(dirname "${2}")
    ln -sf "${1}" "${DEST}/${2}"
    echo "${2}"
}

inst()
{
    install -d -m 755 "${DEST}/${3}"
    install -p -m 644 "${2}" "${DEST}/${3}/${4}"
    echo "${1} ${3}/${4:-$(basename "${2}")}"
}

inst_data() { inst "%attr(0644,root,root)" "${@}"; }
inst_exec() { inst "%attr(0755,root,root)" "${@}"; }
inst_config() { inst "%config(noreplace)" "${@}"; }

inst_doc() {
    echo "%doc ${1}"
}

exec >files-common

dir "${javaconfdir}"
dir "${javaconfdir}/security"
dir "${javaconfdir}/security/security.d"
dir "${jvmdir}"
dir "${jvmprivdir}"
dir "${jvmlibdir}"
dir "${jvmdatadir}"
dir "${jvmsysconfdir}"
dir "${jvmcommonlibdir}"
dir "${jvmcommondatadir}"
dir "${jvmcommonsysconfdir}"
dir "${javadir}"
dir "${javadir}-utils"
dir "${jnidir}"
dir "${javadocdir}"
dir "${mavenpomdir}"
dir "${ivyxmldir}"
dir "${datadir}/maven-metadata"
dir "${m2home}"
dir "${prefix}/lib/eclipse"
dir "${prefix}/lib/eclipse/features"
dir "${prefix}/lib/eclipse/plugins"
dir "${prefix}/lib/eclipse/dropins"
dir "${prefix}/lib/eclipse/droplets"
dir "${datadir}/eclipse"
dir "${datadir}/eclipse/dropins"
dir "${datadir}/eclipse/droplets"
dir "${rpmconfigdir}"
dir "${rpmconfigdir}"
if [[ -n "${scl}" ]]; then
    dir "${javadir}-utils/scl-template"
fi

inst_exec target/build-classpath "${bindir}"
inst_exec target/build-classpath-directory "${bindir}"
inst_exec target/build-jar-repository "${bindir}"
inst_exec target/check-binary-files "${bindir}"
inst_exec target/clean-binary-files "${bindir}"
inst_exec target/create-jar-links "${bindir}"
inst_exec target/diff-jars "${bindir}"
inst_exec target/find-jar "${bindir}"
inst_exec target/rebuild-jar-repository "${bindir}"
inst_exec target/shade-jar "${bindir}"

inst_config etc/font.properties "${javaconfdir}"
inst_config target/java.conf "${javaconfdir}"
inst_config target/eclipse.conf "${javaconfdir}"

inst_data target/java-functions "${javadir}-utils"
inst_exec java-utils/java-wrapper "${javadir}-utils"
inst_exec java-utils/scl-enable "${javadir}-utils"

if [[ -z "${scl}" ]]; then
    # in scl, macros.jpackage is installed in javapackages-local
    inst_data target/macros.jpackage "${rpmmacrodir}"
fi

inst_data target/build-classpath.1 "${mandir}/man1"
inst_data target/build-jar-repository.1 "${mandir}/man1"
inst_data man/diff-jars.1 "${mandir}/man1"
inst_data target/rebuild-jar-repository.1 "${mandir}/man1"
inst_data target/shade-jar.1 "${mandir}/man1"
inst_data target/find-jar.1 "${mandir}/man1"

inst_data target/javapackages-metadata.xml "${datadir}/maven-metadata"
inst_data target/configuration.xml "${m2home}"


if [[ -n "${scl}" ]]; then
    inst_data target/configure-base.sh "${javadir}-utils/scl-template"
    inst_data expand.sh "${javadir}-utils/scl-template"
    inst_data configs/configuration-SCL.xml "${javadir}-utils/scl-template"
    inst_data etc/javapackages-config-SCL.json "${javadir}-utils/scl-template"
    inst_data etc/java.conf "${javadir}-utils/scl-template"
    inst_data etc/eclipse.conf "${javadir}-utils/scl-template"
fi

if [ -z "$pyinterpreter" ]; then
    exit 0
fi


exec >files-local

dir "${rpmconfigdir}/fileattrs"

inst_exec target/abs2rel "${bindir}"
inst_exec target/xmvn-builddep "${bindir}"

link "${bindir}/abs2rel" "${javadir}-utils/abs2rel.sh"

inst_data java-utils/maven_depmap.py "${javadir}-utils"
inst_data java-utils/pom_editor.py "${javadir}-utils"
inst_data java-utils/request-artifact.py "${javadir}-utils"
inst_data java-utils/builddep.py "${javadir}-utils"
inst_data java-utils/mvn_alias.py "${javadir}-utils"
inst_data java-utils/mvn_artifact.py "${javadir}-utils"
inst_data java-utils/mvn_build.py "${javadir}-utils"
inst_data java-utils/mvn_compat_version.py "${javadir}-utils"
inst_data java-utils/mvn_config.py "${javadir}-utils"
inst_data java-utils/mvn_file.py "${javadir}-utils"
inst_data java-utils/mvn_package.py "${javadir}-utils"

if [[ -n "${scl}" ]]; then
    # Base RHEL contains macros in /etc/ that would override ours.
    # We need to place ours there alphabetically after to prevent that.
    # Additionally we move macros.jpackage to javapackages-local, so they don't
    # interfere with base RHEL if only javapackages-tools is installed.
    inst_data target/macros.fjava "${rpmmacrodir}" "macros.x-${scl}.fjava"
    inst_data target/macros.jpackage "${rpmmacrodir}" "macros.x-${scl}.jpackage"
    inst_data target/macros.scl-java-template "${rpmmacrodir}" "macros.${scl}.scl-java-template"
else
    inst_data target/macros.fjava "${rpmmacrodir}"
fi

inst_data target/abs2rel.1 "${mandir}/man1"
inst_data target/mvn_alias.7 "${mandir}/man7"
inst_data target/mvn_artifact.7 "${mandir}/man7"
inst_data target/mvn_build.7 "${mandir}/man7"
inst_data target/mvn_compat_version.7 "${mandir}/man7"
inst_data target/mvn_config.7 "${mandir}/man7"
inst_data target/mvn_file.7 "${mandir}/man7"
inst_data target/mvn_install.7 "${mandir}/man7"
inst_data target/mvn_package.7 "${mandir}/man7"
inst_data target/pom_add_dep.7 "${mandir}/man7"
inst_data target/pom_add_parent.7 "${mandir}/man7"
inst_data target/pom_add_plugin.7 "${mandir}/man7"
inst_data target/pom_disable_module.7 "${mandir}/man7"
inst_data target/pom_xpath_disable.7 "${mandir}/man7"
inst_data target/pom_remove_dep.7 "${mandir}/man7"
inst_data target/pom_remove_parent.7 "${mandir}/man7"
inst_data target/pom_remove_plugin.7 "${mandir}/man7"
inst_data target/pom_change_dep.7 "${mandir}/man7"
inst_data target/pom_set_parent.7 "${mandir}/man7"
inst_data target/pom_xpath_inject.7 "${mandir}/man7"
inst_data target/pom_xpath_remove.7 "${mandir}/man7"
inst_data target/pom_xpath_replace.7 "${mandir}/man7"
inst_data target/pom_xpath_set.7 "${mandir}/man7"

inst_exec target/maven.prov "${rpmconfigdir}" "maven${scl_suffix}.prov"
inst_exec target/maven.req "${rpmconfigdir}" "maven${scl_suffix}.req"
inst_exec target/osgi.prov "${rpmconfigdir}" "osgi${scl_suffix}.prov"
inst_exec target/osgi.req "${rpmconfigdir}" "osgi${scl_suffix}.req"
inst_exec target/javadoc.req "${rpmconfigdir}" "javadoc${scl_suffix}.req"

inst_data target/maven.attr "${rpmconfigdir}/fileattrs" "maven${scl_suffix}.attr"
inst_data target/osgi.attr "${rpmconfigdir}/fileattrs" "osgi${scl_suffix}.attr"
inst_data target/javadoc.attr "${rpmconfigdir}/fileattrs" "javadoc${scl_suffix}.attr"

inst_config target/javapackages-config.json "${javaconfdir}"


exec >files-maven

# Nothing


exec >files-ivy

dir "${sysconfdir}/ivy"

inst_data configs/ivy-local-classpath "${sysconfdir}/ant.d"
inst_data configs/ivysettings.xml "${sysconfdir}/ivy"


exec >files-gradle

inst_exec target/gradle-local "${bindir}"
inst_data gradle/init.gradle "${datadir}/gradle-local"
inst_data target/gradle_build.7 "${mandir}/man7"

exec >files-python
(cd ./python && "${pyinterpreter}" setup.py install -O1 --skip-build --prefix "${prefix}" --root "${DEST}") >&2
echo "${prefix}/lib/python?.?/site-packages/javapackages"
echo "${prefix}/lib/python?.?/site-packages/javapackages-*.egg-info"
