#!/bin/bash
# debian/repack - Produce DFSG compliant orig.tar.gz from upstream tarball.
#  1) Unpack upstream tarball.
#  2) Check for DSFG compliance (no PDFs or precompiled binaries).
#  3) Set revision number in mscore/revision.h to match Git commit number.
#  4) Pack into orig.tar.gz

set -e # exit on error

function usage() {
cat <<EOF
debian/repack - set MuseScore revision number and check for DFSG compliance.

Usage:    debian/repack  --upstream-version  VERSION  [UPSTREAM-TARBALL]
Example:  debian/repack  --upstream-version  "2.0.1+dfsg1"
EOF
}

[[ "$1" == "--usage" || "$1" == "--help" || "$1" == -[h?] ]] && usage && exit

if [ "$1" != "--upstream-version" ] || [ ! "$2" ]; then
  printf "$0: Error! Upstream version was not specified.\n\n$(usage)\n" >&2
  exit 1
fi

version="$2"
if [[ $version == *"+dfsg"* ]]; then
  dfsg="+dfsg${version#*+dfsg}"
  version="${version%+dfsg*}"
fi
[ "$3" ] && tarpath="$3" || tarpath="../musescore_$version$dfsg.orig.tar.gz"

echo "$0: Repacking upstream tarball '$tarpath'." >&2

if [ ! -f "$tarpath" ]; then
  printf "$0: Error! Upstream tarball not found.\n\n$(usage)\n" >&2
  exit 1
fi

tarball="$(basename "$tarpath")"
outdir="$(dirname "$tarpath")" # send output files to input directory

if [ "$(which realpath)" ]; then
  outdir="$(realpath --relative-to="$PWD" "$outdir")"
fi

cd "$outdir"

symlink=""
if [ -L "$tarball" ]; then
  # If $tarball was actually a symlink then resolve it
  symlink="$tarball"
  tarball="$(readlink "$symlink")"
fi

tempdir="tmp-musescore-$version$dfsg"

# 1) Unpack upstream tarball
echo "Unpacking upstream tarball into '$tempdir'." >&2
mkdir "$tempdir"
tar -zxf "$tarball" -C "$tempdir"
cd "$tempdir/MuseScore-$version"

# 2) Check for DSFG compliance (no precompiled binaries or PDFs, etc).
cat >&2 <<EOF
Checking for DFSG compliance. If any non compliant files are discovered then
they will be printed on the Terminal and then this script will exit. The non-
compliant files must be added to the Files-Excluded list within debian/copyright
and then you will need to run 'uscan' or 'gbp import-orig' again. See USCAN(1).
EOF

dfsg_compliant="TRUE"

echo "Searching for PDF files..." >&2
pdfs="$(find . -iname "*.pdf" -print)"
if [ "$pdfs" ]; then
  echo "$pdfs"
  echo "PDF files were discovered. PDFs are non DFSG-free." >&2
  echo "Please add them to Files-Excluded in debian/copyright." >&2
  dfsg_compliant="FALSE"
fi

echo "Searching for precompiled binaries..." >&2
binaries="$(find . -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print)"
if [ "$binaries" ]; then
  echo "$binaries"
  echo "Binary files were discovered. Binaries are non DFSG-free." >&2
  echo "Please add them to Files-Excluded in debian/copyright." >&2
  dfsg_compliant="FALSE"
fi

if [ "$dfsg_compliant" == "FALSE" ]; then
  echo "You will need to run 'uscan' or 'gbp import-orig' again afterwards." >&2
  exit 1
fi

# 3) Set revision number in mscore/revision.h to match upstream Git commit
# Note: upstream has a Makefile target "make revision" to set the revision, but
# it only works inside a Git repo so we have to get the revision another way.
echo "Determining upstream revision (first 7 characters of Git commit SHA)." >&2

commit="$( \
  wget -qO - https://api.github.com/repos/musescore/MuseScore/tags \
   | tr -d '[:space:]' | sed -r 's/("name":)/\n\1/g' \
   | grep -F "\"name\":\"v$version\"" \
   | sed -nr 's/.*"sha":"([[:alnum:]]{7})[[:alnum:]]{33}".*/\1\n/p' \
)"

if [ ! "$commit" ]; then
 echo "$0: Error! Couldn't get upstream commit." >&2
 exit 1
fi

echo "Upstream revision is '$commit'." >&2

echo "Setting revision number in 'mscore/revision.h' to '$commit'." >&2
mtime="$(stat -c %y "mscore/revision.h")" # preserve modification time
echo "$commit" > "mscore/revision.h"
touch -d "$mtime" "mscore/revision.h"

# 4) Pack into orig.tar.gz
cd .. # now inside $tempdir
mv "MuseScore-$version" "musescore-$version$dfsg"
tar -cf "../musescore_$version$dfsg.orig.tar" "musescore-$version$dfsg"
cd .. # now back to output directory (outside $tempdir)

# Tidy up (must do this before compressing tar incase tarball has same name)
echo "Removing temporary files." >&2
rm -rf "$tempdir" "$tarball" "$symlink"

# Compress new orig.tar
gzip -9 "musescore_$version$dfsg.orig.tar"

cat >&2 <<EOF
Finished!
The upstream code is in "$outdir/musescore_$version$dfsg.orig.tar.gz".
Use 'uupdate' to upgrade the source package based on the upstream release:
 \$ uupdate --upstream-version $version $outdir/musescore_$version$dfsg.orig.tar.gz
Or, if working in a Git repository, use git-buildpackage to update the source:
 \$ gbp import-orig --merge-mode=replace $outdir/musescore_$version$dfsg.orig.tar.gz
EOF
