#!/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-filesystem

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 "${jnidir}"
dir "${javadocdir}"
dir "${mavenpomdir}"
dir "${ivyxmldir}"
dir "${datadir}/maven-metadata"
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"

inst_data macros.d/macros.javapackages-filesystem "${rpmmacrodir}"


exec >files-tools

dir "${m2home}"
dir "${javadir}-utils"
dir "${rpmconfigdir}"

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_data target/macros.jpackage "${rpmmacrodir}"

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 etc/javapackages-metadata.xml "${datadir}/maven-metadata"
inst_data configs/configuration.xml "${m2home}"

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


exec >files-local

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

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

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"

inst_data target/macros.fjava "${rpmmacrodir}"

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"


exec >files-generators

dir "${rpmconfigdir}/fileattrs"

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

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

inst_config etc/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"
