#!/bin/bash

cd "$(dirname "$0")"/..

all_licenses=doc/all-licenses.txt

# Header
cat <<EOF > debian/copyright-new
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: Red Eclipse
Upstream-Contact: Quinton "Quin" Reeves <qreeves@gmail.com>
                  Lee "Eihrul" Salzman <lsalzman@gmail.com>
Source: https://github.com/red-eclipse/data
 Most of the non-data content is shipped as part of the redeclipse source
 package and thus removed from here, although the makefile is kept for
 installing.
 .
 The Play font files sources are removed since they are shipped in the
 fonts-play package in Debian, and the generated png files are removed to
 simplify the build process.
Files-Excluded: bin config *.bat *.sh src/dpiaware.manifest src/enet
                src/engine src/game src/include src/install src/lib src/*.sh
                src/redeclipse* src/scripts src/shared src/*.h
                src/xcode data/fonts/Play* data/fonts/play*
License: Red-Eclipse
EOF

# Summary license, indented
sed -e 's/^/\ /' -e 's/^\ $/\ \./' doc/license.txt >> debian/copyright-new

# Remove duplicated Format: line and insert upstream license dep5 file
sed '/Format:.*/d' "$all_licenses"  >> debian/copyright-new

# Remove all records related to bin/*, config/*, doc/*, data/fonts/* and src/*
# (not present in the source)
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^Files: (bin|config|doc|src)\// ' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove records for Play font files (deleted from source package)
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^Files: data\/fonts\/[Pp]lay/' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Add marker for inserting Debian chunk
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 ~ /^License: Zlib$/ { print "INSERT_DEBIAN_HERE" } { print }' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Debian chunk
ex -s debian/copyright-new <<EOF
/INSERT_DEBIAN_HERE/c
Files: debian/*
Copyright: 2011-2015 Martin Erik Werner <martinerikwerner@gmail.com>
License: Zlib
.
w
EOF

echo "done -> debian/copyright-new"
