#!/bin/sh

#----------------------------------------------------------------------
# Generated files from the upstream tarball (before repacking) are
# excluded from the repacked .orig tarball, so we need to generate them
# back at build time.  This script can be used to list missing files
# after building the debs.
# Copyright © 2020 Guilhem Moulin <guilhem@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#----------------------------------------------------------------------

set -ue
PATH="/usr/bin:/bin"
export PATH

SOURCE_NAME="$(dpkg-parsechangelog -S Source)"
VERSION="$(dpkg-parsechangelog -S Version)"
UPSTREAM_VERSION="${VERSION%-*}"
UPSTREAM_VERSION="${UPSTREAM_VERSION%+dfsg.*}"

TEMPDIR="$(mktemp --tmpdir --directory)"
trap 'rm -rf "$TEMPDIR"' EXIT INT TERM

uscan --no-download --dehs >"$TEMPDIR/dehs.xml" || true
SOURCE_URL="$(xmlstarlet sel -T -t -v "/dehs/upstream-url" <"$TEMPDIR/dehs.xml")"
wget -P "$TEMPDIR" "$SOURCE_URL"

for f in /etc/devscripts.conf ~/.devscripts; do
    [ ! -f "$f" ] || . "$f"
done

# Assume gbp's export-dir is uscan's $USCAN_DESTDIR
DESTDIR="${USCAN_DESTDIR:-".."}"

for p in "$TEMPDIR/roundcubemail-$UPSTREAM_VERSION".tar*;    do UPSTREAM_TARBALL="$p"; break; done
for p in "$DESTDIR/${SOURCE_NAME}_${VERSION%-*}".orig.tar.*; do ORIG_TARBALL="$p";     break; done

tarball_filelist() {
    # `tr | sort | tr` to emulate a sorted FS-traversal
    tar -ta -f "$1" | sed "s,^[^/]*/,," | tr "/" "\\0" | sort | tr "\\0" "/"
}
tarball_diff() {
    local a="$1" alabel="a/${UPSTREAM_TARBALL##*/}"
    local b="$2" blabel="b/${ORIG_TARBALL##*/}"
    local i
    for i in $(seq 1 80); do printf "#"; done; echo
    ! diff --unified --color="auto" --label="$alabel" --label="$blabel" \
           -- "$a" "$b"
    for i in $(seq 1 80); do printf "#"; done; echo
}

tarball_filelist "$UPSTREAM_TARBALL" >"$TEMPDIR/list-orig"
tarball_filelist "$ORIG_TARBALL"     >"$TEMPDIR/list-repacked"

diff -- "$TEMPDIR/list-orig" "$TEMPDIR/list-repacked" >"$TEMPDIR/list-diff" && exit 0 || true

printf "INFO %s contains %d files\\n" "${UPSTREAM_TARBALL##*/}" "$(wc -l <"$TEMPDIR/list-orig")"
printf "INFO %s contains %d files (-%d, +%d)\\n" "${ORIG_TARBALL##*/}" "$(wc -l <"$TEMPDIR/list-repacked")" \
    "$(grep -c "^< " <"$TEMPDIR/list-diff")" "$(grep -c "^> " <"$TEMPDIR/list-diff")"

# Uncomment to show excluded/extra files
#tarball_diff "$TEMPDIR/list-orig" "$TEMPDIR/list-repacked"

mkdir "$TEMPDIR/dest"
dpkg_extract() {
    local dir="$1" pkg="$2" deb
    for deb in "$dir/$pkg" "$dir/$pkg"_*.deb; do
        [ ! -e "$deb" ] || break
    done
    dpkg -x "$deb" "$TEMPDIR/dest"
}

for pkg in $(sed -n "s/Package:\\s*//p" debian/control); do
    dpkg_extract "$DESTDIR" "${pkg}_${VERSION}"
done

INSTALLDIR="/usr/share/roundcube"
sed -n -i "s/^< //p" "$TEMPDIR/list-diff"

# Cf. debian/patches/Use-system-JQueryUI.patch.  We remove these from
# the diff as missing "js/i18n/$package-$lang_l.min.js" files is an
# issue for libjs-jquery-ui not us.
sed -ri "\,plugins/jqueryui/js/i18n/jquery\.ui\.datepicker-[[:alnum:]_-]+\.js$,d" "$TEMPDIR/list-diff"

while IFS= read -r p; do
    p2="$TEMPDIR/dest/$INSTALLDIR/${p%/}"
    if [ ! -h "$p2" ] && [ ! -e "$p2" ]; then
        printf "%s\\n" "$p" >>"$TEMPDIR/missing"
    fi
done <"$TEMPDIR/list-diff"

RV=0
if [ -s "$TEMPDIR/missing" ]; then
    printf "ERROR %d files found in the orig tarball but not in any .deb:\\n" "$(wc -l <"$TEMPDIR/missing")"
    cat <"$TEMPDIR/missing"
    RV=1
else
    printf "OK All %d files excluded during repacking are rebuilt anew and shipped by one of our .deb\\n" \
           "$(wc -l <"$TEMPDIR/list-diff")"
fi
exit $RV
