#!/bin/sh
set -Cefuv

pkg=libgnatcoll-db

github_user=Adacore
github_project=gnatcoll-db
github_url=https://github.com/$github_user/$github_project

# Upstream uses git branches for releases, so there is no reproducibley
# way to deduce the commit from the version.
# See $github_url/network
deb_version_upstream=19.2
commit=fbc46346dc67dfa83ae5132ef72fdd64fbe7e199

orig=${pkg}_$deb_version_upstream.orig.tar.xz
orig_dir=${pkg}-$deb_version_upstream

zip=$commit.zip
zipurl=$github_url/archive/$zip
zipdir=$github_project-$commit

# Sanity check.
test ! -e ../$orig
test ! -e $zipdir

# If we were interrupted, do not download the archive again.
test -r $zip || wget $zipurl

# Check that the archive contains no suspicious path.
status=0
unzip -l $zip \*..\* || status=$?
test $status = 11

# Check that all files lie in the expected directory.
a=`unzip -l $zip $zipdir/\* | tail -n1`
b=`unzip -l $zip            | tail -n1`
test "$a" = "$b"

unzip $zip

# The interesting part.

# Since we have to repackage anyhow, remove all files that would
# pollute the VCS diffs or make the packaging more complex, or could
# be used unwantedly.
# However, attempt to report any removed file during upgrades.

find $zipdir \( \
         -name .gitattributes \
     -or -name .gitreview \
     -or -name .gitignore \
     \) -delete

# GCC runtime exception is not granted to non-paying customers.
for f in \
    COPYING.RUNTIME \

do
    # Avoid -f, report unnecessary removals.
    rm $zipdir/$f
done

# xref/generated is regenerated at build time.
# sqlite3 is packaged separately.
for d in \
    sqlite/amalgamation \
    xref/generated \

do
    # Report unnecessary removals.
    test -d $zipdir/$d
    rm -fr $zipdir/$d
done

empty_comment='--                                                                          --'
find $zipdir -type f -a \( -name '*.ad[bs]' -o -name '*.gpr' \) | xargs sed -i \
  -e s/'^-- As a special exception under Section 7 of GPL version 3, you are granted --$'/"$empty_comment"/ \
  -e s/'^-- additional permissions described in the GCC Runtime Library Exception,   --$'/"$empty_comment"/ \
  -e s/'^-- version 3\.1, as published by the Free Software Foundation\.               --$'/"$empty_comment"/ \
  \
  -e s/'^--  As a special exception,  if other files  instantiate  generics from this --$'/"$empty_comment"/ \
  -e s/'^--  unit, or you link  this unit with other files  to produce an executable, --$'/"$empty_comment"/ \
  -e s/'^--  this  unit  does not  by itself cause  the resulting  executable  to  be --$'/"$empty_comment"/ \
  -e s/'^--  covered  by the  GNU  General  Public  License\.  This exception does not --$'/"$empty_comment"/ \
  -e s/'^--  however invalidate  any other reasons why  the executable file  might be --$'/"$empty_comment"/ \
  -e s/'^--  covered by the  GNU Public License\.                                      --$'/"$empty_comment"/ \

# All went OK, create the archive and clean.
tar -caf ../$orig --transform=s/^$zipdir/$orig_dir/ $zipdir
rm -fr $zipdir
rm $zip
