#!/bin/sh

PACKAGE_NAME=normaliz

set -e
set -u

usage() {
	echo "Usage: repack --upstream-version <ver> <downloaded file>"
	exit 1
	}

if [ "$#" != "3" ]; then
	usage
fi
if [ "$1" != "--upstream-version" ]; then
	usage
fi
if [ ! -f "$3" ]; then
	if [ -n "$3" ]; then
		echo "$3 doesn't exist"
	fi
	usage
fi
UPSTREAM_VERSION="$2"
UPSTREAM_TARBALLZZ="$3"

DEBIAN_SUFFIX="+ds"
####DEBIAN_ENCAPSULATEDFOLDERNAME="${PACKAGE_NAME}-usrc"

DEBIAN_UVERSION=${UPSTREAM_VERSION}${DEBIAN_SUFFIX}
DEBIAN_ROOTFOLDERNAME="${PACKAGE_NAME}-${DEBIAN_UVERSION}.orig"
DEBIAN_TARBALLXZ="$(dirname $UPSTREAM_TARBALLZZ)/${PACKAGE_NAME}_${DEBIAN_UVERSION}.orig.tar.xz"

REPACK_TMPDIR=`mktemp -d ./repackXXXXXX`
REPACK_TMPDIR=$(readlink -f "$REPACK_TMPDIR")
trap "/bin/rm -rf \"$REPACK_TMPDIR\"" QUIT INT EXIT

message() {
	echo
	echo "-- -- $1"
	echo
	}

message "Repackaging $UPSTREAM_TARBALLZZ"

UPSTREAM_ROOTFOLDER="${REPACK_TMPDIR}/unpacked"
mkdir "${UPSTREAM_ROOTFOLDER}"
tar -C "${UPSTREAM_ROOTFOLDER}" -xf "${UPSTREAM_TARBALLZZ}" || unzip -d "${UPSTREAM_ROOTFOLDER}" "${UPSTREAM_TARBALLZZ}"
if [ `ls -1 "${UPSTREAM_ROOTFOLDER}" | wc -l` -eq 1 ]; then
	UPSTREAM_ROOTFOLDER="${UPSTREAM_ROOTFOLDER}/`ls -1 "${UPSTREAM_ROOTFOLDER}"`"
fi

DEBIAN_ROOTFOLDER="${REPACK_TMPDIR}/${DEBIAN_ROOTFOLDERNAME}"
## repack
####set -f
REPACK_WORKING_FOLDER=$(pwd)
cd "${UPSTREAM_ROOTFOLDER}"

####. "${REPACK_WORKING_FOLDER}/debian/repack.local"
## wipe out built material
rm --verbose --recursive --force -- source/jNormaliz/build
rm --verbose --force -- source/jNormaliz/build.xml
## wipe out Java source balls
rm --verbose --recursive --force -- source/jNormaliz/lib/commons-exec-1.0.1
rm --verbose --force -- source/jNormaliz/lib/swing-app-framework/appframework-1.0.3-doc.zip
rm --verbose --force -- source/jNormaliz/lib/junit/junit-3.8.2-api.zip
rm --verbose --force -- source/jNormaliz/lib/junit_4/junit-4.5-api.zip
## wipe out Java ARchive files
find . -name '*.jar' | xargs --no-run-if-empty rm --verbose --force
## clean up Java external library material
rmdir --verbose --ignore-fail-on-non-empty -- source/jNormaliz/lib/CopyLibs
rmdir --verbose --ignore-fail-on-non-empty -- source/jNormaliz/lib/junit
rmdir --verbose --ignore-fail-on-non-empty -- source/jNormaliz/lib/junit_4
rmdir --verbose --ignore-fail-on-non-empty -- source/jNormaliz/lib/swing-app-framework
rm --verbose --force -- source/jNormaliz/lib/nblibraries.properties
rmdir --verbose --ignore-fail-on-non-empty -- source/jNormaliz/lib
rmdir --verbose --ignore-fail-on-non-empty -- lib
## wipe out published papers
rm --verbose --force -- "doc/Algorithms for affine monoids and rational cones.pdf"
rm --verbose --force -- "doc/Challenging computations of Hilbert bases of cones associated with algebraic statistics.pdf"
rm --verbose --force -- "doc/Computing the integral closure of an affine semigroup.pdf"
rm --verbose --force -- "doc/The power of pyramid decomposition in Normaliz.pdf"
## wipe out regenerated documentation
rm --verbose --force -- source/jNormaliz/doc/jNormaliz_1.6.pdf
rm --verbose --force -- doc/Normaliz.pdf
rm --verbose --force -- doc/NmzIntegrate.pdf
rm --verbose --force -- doc/jNormaliz_1.6.pdf
## wipe out duplicate material
rm --verbose --force -- doc/jNormaliz.jpg
## wipe out
#rm --verbose --force -- build-aux/test-driver
#rmdir --verbose --ignore-fail-on-non-empty -- build-aux
##
## remove executable bit for source files
#chmod --changes a-x m4/flint-check.m4
## discard autoconf-archive material
#rm --verbose --force -- m4/ax_append_compile_flags.m4
#:

cd ${REPACK_WORKING_FOLDER}
set +f
## end
####if [ -n "${DEBIAN_ENCAPSULATEDFOLDERNAME}" ]; then
####	mkdir "${DEBIAN_ROOTFOLDER}"
####	mv "${UPSTREAM_ROOTFOLDER}" "${DEBIAN_ROOTFOLDER}/${DEBIAN_ENCAPSULATEDFOLDERNAME}"
####else
	mv "${UPSTREAM_ROOTFOLDER}" "${DEBIAN_ROOTFOLDER}"
####fi

REPACK_TARBALL="${REPACK_TMPDIR}/repacked.tar"
REPACK_TARBALLXZ="${REPACK_TARBALL}.xz"
( cd "${REPACK_TMPDIR}" && \
	find -L "${DEBIAN_ROOTFOLDERNAME}" -xdev -type f -print | sort | \
	tar -T-  --owner=root --group=root --mode=a+rX --create --file "${REPACK_TARBALL}" \
	)
xz -9e < "${REPACK_TARBALL}" > "${REPACK_TARBALLXZ}"
mv "${REPACK_TARBALLXZ}" "${DEBIAN_TARBALLXZ}"

message "Testing ${DEBIAN_TARBALLXZ}"

xz --verbose --test "${DEBIAN_TARBALLXZ}"

message "Printing information about ${DEBIAN_TARBALLXZ}"

xz --verbose --list "${DEBIAN_TARBALLXZ}"

message "Quitting"

##
## eos
