#!/bin/bash

PACKAGE_NAME=apophenia

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="+ds3"

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
rm --verbose --force -- tests/Makefile
rm --verbose --force -- test-driver
rm --verbose --force -- depcomp
rm --verbose --force -- install-sh
rm --verbose --force -- ltmain.sh
rm --verbose --force -- missing
rm --verbose --force -- INSTALL
rm --verbose --force -- config.sub
rm --verbose --force -- config.guess
## clean up
rm --verbose --force -- apophenia.pc
rm --verbose --force -- tests/utilities_test
rm --verbose --force -- docs/doxyconfig
rm --verbose --force -- docs/edit_group
rm --verbose --force --recursive  -- eg/.libs
rm --verbose --force --recursive  -- eg/.dirstamp
rm --verbose --force -- eg/apop_map_row{,.log,.trs}
rm --verbose --force -- eg/binning{,.log,.trs}
rm --verbose --force -- eg/db_fns{,.log,.trs}
rm --verbose --force -- eg/dconstrain{,.log,.trs}
rm --verbose --force -- eg/dot_products{,.log,.trs}
rm --verbose --force -- eg/draw_to_db{,.log,.trs}
rm --verbose --force -- eg/fake_logit{,.log,.trs}
rm --verbose --force -- eg/fix_params{,.log,.trs}
rm --verbose --force -- eg/hills2{,.log,.trs}
rm --verbose --force -- eg/iv{,.log,.trs}
rm --verbose --force -- eg/jacobian{,.log,.trs}
rm --verbose --force -- eg/ks_tests{,.log,.trs}
rm --verbose --force -- eg/normalizations{,.log,.trs}
rm --verbose --force -- eg/parameterization{,.log,.trs}
rm --verbose --force -- eg/pmf_test{,.log,.trs}
rm --verbose --force -- eg/simple_subsets{,.log,.trs}
rm --verbose --force -- eg/some_cdfs{,.log,.trs}
rm --verbose --force -- eg/stack_models{,.log,.trs}
rm --verbose --force -- eg/test_distances{,.log,.trs}
rm --verbose --force -- eg/test_fisher{,.log,.trs}
rm --verbose --force -- eg/test_harmonic{,.log,.trs}
rm --verbose --force -- eg/test_kl_divergence{,.log,.trs}
rm --verbose --force -- eg/test_pruning{,.log,.trs}
rm --verbose --force -- eg/test_ranks{,.log,.trs}
rm --verbose --force -- eg/test_regex{,.log,.trs}
rm --verbose --force -- eg/test_updating{,.log,.trs}
##
rm --verbose --force -- m4/lt~obsolete.m4
rm --verbose --force -- m4/ltsugar.m4
rm --verbose --force -- m4/ltoptions.m4
rm --verbose --force -- m4/ltversion.m4
rm --verbose --force -- m4/libtool.m4
####rmdir --verbose --ignore-fail-on-non-empty -- m4
rm --verbose --force -- build-aux/test-driver
rm --verbose --force -- build-aux/depcomp
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
rmdir --verbose --ignore-fail-on-non-empty -- build-aux
rm --verbose --force -- transform/Makefile.in
rm --verbose --force -- model/Makefile.in
rm --verbose --force -- Makefile.in
rm --verbose --force -- cmd/Makefile.in
rm --verbose --force -- tests/Makefile.in
rm --verbose --force -- docs/Makefile.in
rm --verbose --force -- eg/Makefile.in
rm --verbose --force -- acinclude.m4
rm --verbose --force -- aclocal.m4
rm --verbose --force -- config.h.in
rm --verbose --force -- configure
#:

cd ${REPACK_WORKING_FOLDER}
set +f
## end
mv "${UPSTREAM_ROOTFOLDER}" "${DEBIAN_ROOTFOLDER}"

REPACK_TARBALL="${REPACK_TMPDIR}/repacked.tar"
REPACK_TARBALLXZ="${REPACK_TARBALL}.xz"
( cd "${REPACK_TMPDIR}" && \
	find -L "${DEBIAN_ROOTFOLDERNAME}" -xdev -type f -o -type d -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
