#!/bin/bash

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

# 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://redeclipse.svn.sourceforge.net
 All data content were removed from the source package, they are shipped in
 redeclipse-data instead.
 The folders src/include/ src/xcode/ src/lib/ bin/ were also removed, the
 header files here are already shipped within other Debian packages, or are
 unnecessary on GNU/Linux.
License: Red-Eclipse
EOF

# "Red-Eclipse" 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' doc/all-licenses.txt >> debian/copyright-new

# Remove record stating some dirs are omitted (the dirs are removed in Debian)
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^Files: bin\//' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove all records related to data/*
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^Files: data\//' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove record for "Akashi-Font" license
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^License: Akashi-Font$/' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove record for "OFL-1.1" license
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^License: OFL-1.1$/' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove record for "CC-BY-3.0" license
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^License: CC-BY-3.0$/' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove record for "CC-BY-SA-3.0-AU" license
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^License: CC-BY-SA-3.0-AU$/' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

# Remove record for "CC-BY-3.0-US" license
awk 'BEGIN{ ORS="\n\n"; RS="" ; FS="\n"} $1 !~ /^License: CC-BY-3.0-US$/' debian/copyright-new > debian/copyright-temp
mv debian/copyright-temp debian/copyright-new

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

# Insert Enet chunk
ed -s debian/copyright-new << "EOF"
/INSERT_ENET_HERE/c
Files: src/enet/*
Copyright: 2002-2012 Lee Salzman
License: Expat

Files: src/enet/aclocal.m4
Copyright: 1996-2012  Free Software Foundation, Inc.
License:
 This file is free software; the Free Software Foundation
 gives unlimited permission to copy and/or distribute it,
 with or without modifications, as long as this notice is preserved.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY, to the extent permitted by law; without
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE.

Files: src/enet/config.guess src/enet/config.sub src/enet/depcomp
       src/enet/missing
Copyright: 1992-2012 Free Software Foundation, Inc.
License: GPL-2+
 This file 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 2 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, write to the Free Software
 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
 02110-1301, USA.
 .
 As a special exception to the GNU General Public License, if you
 distribute this file as part of a program that contains a
 configuration script generated by Autoconf, you may include it under
 the same distribution terms that you use for the rest of that program.
 .
 On Debian systems, the full text of the GNU General Public License version 2
 can be found in the file `/usr/share/common-licenses/GPL-2'.

Files: src/enet/configure
Copyright: 1992-2012 Free Software Foundation, Inc.
License: custom and GPL-2+
 This configure script is free software; the Free Software Foundation
 gives unlimited permission to copy, distribute and modify it.
 .
 This config.status script is free software; the Free Software Foundation
 gives unlimited permission to copy, distribute and modify it.
 .
 GNU Libtool 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 2 of
 the License, or (at your option) any later version.
 .
 As a special exception to the GNU General Public License,
 if you distribute this file as part of a program or library that
 is built using GNU Libtool, you may include this file under the
 same distribution terms that you use for the rest of that program.
 .
 GNU Libtool 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 GNU Libtool; see the file COPYING.  If not, a copy
 can be downloaded from http://www.gnu.org/licenses/gpl.html, or
 obtained by writing to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 .
 On Debian systems, the full text of the GNU General Public License version 2
 can be found in the file `/usr/share/common-licenses/GPL-2'.

Files: src/enet/install-sh
Copyright: 1994 X Consortium
License: custom and public-domain
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to
 deal in the Software without restriction, including without limitation the
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 sell copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 .
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 .
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
 TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 .
 Except as contained in this notice, the name of the X Consortium shall not
 be used in advertising or otherwise to promote the sale, use or other deal-
 ings in this Software without prior written authorization from the X Consor-
 tium.
 .
 FSF changes to this file are in the public domain.

Files: src/enet/ltmain.sh
Copyright: 1996-2008 Free Software Foundation, Inc.
License: GPL-2+
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 .
 GNU Libtool 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 2 of the License, or
 (at your option) any later version.
 .
 As a special exception to the GNU General Public License,
 if you distribute this file as part of a program or library that
 is built using GNU Libtool, you may include this file under the
 same distribution terms that you use for the rest of that program.
 .
 GNU Libtool 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 GNU Libtool; see the file COPYING.  If not, a copy
 can be downloaded from http://www.gnu.org/licenses/gpl.html,
 or obtained by writing to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 .
 On Debian systems, the full text of the GNU General Public License version 2
 can be found in the file `/usr/share/common-licenses/GPL-2'.

Files: src/enet/m4/*
Copyright: 2004-2009 Free Software Foundation, Inc.
License:
 This file is free software; the Free Software Foundation gives
 unlimited permission to copy and/or distribute it, with or without
 modifications, as long as this notice is preserved.

Files: src/enet/m4/libtool.m4
Copyright: 1996-2011 Free Software Foundation, Inc.
License: custom, GPL-2+
 This file is free software; the Free Software Foundation gives
 unlimited permission to copy and/or distribute it, with or without
 modifications, as long as this notice is preserved.
 .
 GNU Libtool 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 2 of
 the License, or (at your option) any later version.
 .
 As a special exception to the GNU General Public License,
 if you distribute this file as part of a program or library that
 is built using GNU Libtool, you may include this file under the
 same distribution terms that you use for the rest of that program.
 .
 GNU Libtool 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 GNU Libtool; see the file COPYING.  If not, a copy
 can be downloaded from http://www.gnu.org/licenses/gpl.html, or
 obtained by writing to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 .
 This config.lt script is free software; the Free Software Foundation
 gives unlimited permision to copy, distribute and modify it.
 .
 On Debian systems, the full text of the GNU General Public License version 2
 can be found in the file `/usr/share/common-licenses/GPL-2'.

Files: src/enet/Makefile.in
Copyright: 1994-2011
           Free Software Foundation, Inc.
License:
 This Makefile.in is free software; the Free Software Foundation
 gives unlimited permission to copy and/or distribute it,
 with or without modifications, as long as this notice is preserved.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY, to the extent permitted by law; without
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 PARTICULAR PURPOSE.

INSERT_DEBIAN_HERE
.
w
EOF

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

