#!/bin/sh

PACKAGE_NAME=tth

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_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"

rm --verbose --force tth4.05.tar.gz.asc

rm --verbose --force --recursive tthfunc tthgif TtMdir ttmC tth-gui

find . \( \
		-name '*.bat' -o \
		-name '*.lnk' -o \
		-name '*.tlg' -o \
		-name '*.log' -o \
		-name '*.ilg' -o \
		-name '*.idx' -o \
		-name '*.toc' -o \
		-name '*.aux' -o \
		-name '*.dvi' -o \
		-name '*.bak' -o \
		-name '*.res' -o \
		-name '*.exe' -o \
		-name '*~' \
		\) \
	-print -delete

find . \( \
		-wholename './manual/split/*.html' \
		\) \
	-print -delete

rm --verbose --force manual/ttm_manual.tex
rm --verbose --force manual/mozilla.tex

rm --verbose --force ttmL/ttm.gif ttmL/ttm_icon.gif ttmL/ttm_mini.gif
rm --verbose --force ttmL/ttm ttmL/ps2gif ttmL/ps2png ttmL/latex2gif
rm --verbose --force ttmL/ttm_manual.html
rm --verbose --force ttmL/ttm.kdelnk
rm --verbose --force ttmL/uninstall ttmL/ttminstall

rm --verbose --force --recursive tthgold/golddesc
rm --verbose --force tthgold/tthsplit.c
rm --verbose --force tthgold/tthsplit tthgold/tthrfcat
rm --verbose --force tthgold/latex2gif
rm --verbose --force tthgold/tth tthgold/tth.c tthgold/tth.1
rm --verbose --force tthgold/tth.gif tthgold/tth_icon.bmp
rm --verbose --force tthgold/tthntbib.sty
rm --verbose --force tthgold/tth_man.html tthgold/gold_man.html
rm --verbose --force tthgold/tth_man.tex

rm --verbose --force tools/ps2gif_transparent
rm --verbose --force tools/structure tools/numbering
rm --verbose --force tools/choice.c

rm --verbose --force latex2gif ps2gif transfer
rm --verbose --force tth16.png tth32.png tth_screen.gif

rmdir --verbose --ignore-fail-on-non-empty tars

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