#!/bin/sh

PACKAGE_NAME=singular

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"
## discard dot-git files
rm --verbose --force -- .tarball-git-version
## discard other distribution material to avoid confusion
rm --verbose --force --recursive -- redhat
## discard third party packages
rm --verbose --force --recursive -- Singular/LIB/surfex
## wipe out
rm --verbose --force -- README.md
rm --verbose --force -- m4/lt~obsolete.m4
rm --verbose --force -- m4/ltoptions.m4
rm --verbose --force -- m4/ltsugar.m4
rm --verbose --force -- m4/ltversion.m4
rm --verbose --force -- m4/libtool.m4
rm --verbose --force -- build-aux/ylwrap
rm --verbose --force -- build-aux/depcomp
rm --verbose --force -- build-aux/compile
rm --verbose --force -- build-aux/ar-lib
rm --verbose --force -- build-aux/install-sh
rm --verbose --force -- build-aux/ltmain.sh
rm --verbose --force -- build-aux/missing
rm --verbose --force -- build-aux/config.sub
rm --verbose --force -- build-aux/config.guess
rm --verbose --force -- build-aux/test-driver
rmdir --verbose --ignore-fail-on-non-empty -- build-aux
rm --verbose --force -- make_tar.sh
rm --verbose --force -- warn_not_found.sh
##
find . -name 'autom4te.cache' | xargs --no-run-if-empty rm --verbose --recursive --force
find . -name 'aclocal.m4' | xargs --no-run-if-empty rm --verbose --force
find . -name 'Makefile.in' | xargs --no-run-if-empty rm --verbose --force
rm --verbose --force -- gfanlib/_config.h.in
rm --verbose --force -- gfanlib/configure
rm --verbose --force -- libpolys/_config.h.in
rm --verbose --force -- libpolys/configure
rm --verbose --force -- factory/acinclude.m4
####rm --verbose --force -- factory/aminclude.am
rm --verbose --force -- factory/_config.h.in
rm --verbose --force -- factory/configure
rm --verbose --force -- omalloc/_config.h.in
rm --verbose --force -- omalloc/configure
rm --verbose --force -- resources/_config.h.in
rm --verbose --force -- resources/configure
rm --verbose --force -- _config.h.in
rm --verbose --force -- configure
##
rm --verbose --force -- factory/readcf.cc
rm --verbose --force -- Singular/feOptES.inc
rm --verbose --force -- Singular/feOptTS.inc
rm --verbose --force -- Singular/all.lib
##
rm --verbose --force -- doc/Singular.man
rm --verbose --force -- doc/TSingular.man
rm --verbose --force -- doc/ESingular.man
rmdir --verbose --ignore-fail-on-non-empty -- doc
## remove executable bit for source files
chmod --changes a-x m4/flint-check.m4
chmod --changes a-x gfanlib/configure.ac
## discard autoconf-archive material
rm --verbose --force -- m4/ax_append_compile_flags.m4
rm --verbose --force -- m4/ax_append_flag.m4
rm --verbose --force -- m4/ax_append_link_flags.m4
rm --verbose --force -- m4/ax_check_compile_flag.m4
rm --verbose --force -- m4/ax_check_link_flag.m4
rm --verbose --force -- m4/ax_compute_relative_paths.m4
rm --verbose --force -- m4/ax_cxx_gcc_abi_demangle.m4
rm --verbose --force -- m4/ax_normalize_path.m4
####rm --verbose --force -- m4/ax_prefix_config_h.m4
###### comment:
######  at the time of writting, the latest ax_prefix_config_h.m4
######  as distributed within the autoconf-archive is not compatible
######  with this version (serial 11); more precisely, not providing
######  a prefix is an error in the lastest version (serial 15).
rm --verbose --force -- m4/ax_pthread.m4
rm --verbose --force -- m4/ax_python_embed.m4
####rm --verbose --force -- m4/ax_python_with_version.m4
###### comment:
######  ax_python_with_version.m4 does not belong to autoconf-archive.
## remove executable bit for source files
chmod --changes a-x Singular/extra.cc
#:

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- --numeric-owner --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
