#!/bin/bash

# Abort script at first error, so that the script doesn't exit with
# status 0 for missing files 
set -o errexit

if [ $# -ne 1 ] && [ $# -ne 2 ]; then
	echo "Simple packaging file for the Windows port.  Only really useful when"
	echo "cross-compiling, or using Cygwin.  Originally by Robert Ruehlmann."
	echo ""
	echo "Use in the root directory of the game, like so:"
	echo "   scripts/pkg_win <name of of release, e.g. angband-4.0.1> [<output filename>]"
	echo ""

	exit 1
fi

DIR=$1
if [ $# -eq 1 ]; then
	OUT=$1.zip
else
	OUT=$2
fi


function cp_unix2dos {
  # Just in case they're already DOS_format, we strip them to UNIX first. :)
  awk '{ sub("\r$", ""); sub("$", "\r"); print }' $1 > $2
}


mkdir -p $DIR
cd $DIR

mkdir docs
mkdir docs/_sources
mkdir docs/_sources/hacking
mkdir docs/_static
mkdir docs/hacking
mkdir lib
mkdir lib/gamedata
mkdir lib/customize
mkdir lib/help
mkdir lib/screens
mkdir lib/fonts
mkdir lib/tiles
mkdir lib/tiles/adam-bolt
mkdir lib/tiles/gervais
mkdir lib/tiles/nomad
mkdir lib/tiles/old
mkdir lib/tiles/shockbolt
mkdir lib/sounds
mkdir lib/icons
mkdir lib/user
mkdir lib/user/save
mkdir lib/user/scores
mkdir lib/user/info
mkdir lib/user/archive
mkdir lib/user/panic

touch lib/user/delete.me lib/user/save/delete.me lib/user/scores/delete.me \
      lib/user/info/delete.me lib/user/archive/delete.me \
	  lib/user/panic/delete.me

cp ../*.exe .
cp ../*.dll .

# Copy the readmes and suchlike, converting to DOS line endings on the way

cp_unix2dos ../changes.txt changes.txt

# Assumes that all the converted files in docs have names that don't contain
# spaces or other characters that the shell uses as field separators.
# First copy the ones that will benefit from newline conversion; then
# do the rest.
for f in `find ../docs/_build/html \( -name '*.css' -o -name '*.html' \
		-o -name '*.js' -o -name '*.svg' -o -name '*.txt' \) \
		\! -type d -print` ; do
	cp_unix2dos "$f" \
		`echo "$f" | sed -E -e 's%^\.\./docs/_build/html%docs%'`
done
for f in `find ../docs/_build/html \! -name '*.css' \! -name '*.html' \
		\! -name '*.js' \! -name '*.svg' \! -name '*.txt' \
		\! -type d -print` ; do
	cp "$f" `echo "$f" | sed -E -e 's%^\.\./docs/_build/html%docs%'`
done

cp_unix2dos ../lib/readme.txt lib/readme.txt

# Ditto the comment above for the way find is used here.
for f in `find ../lib/gamedata -name '*.txt' -print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done
for f in `find ../lib/screens -name '*.txt' -print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done
for f in `find ../lib/help -name '*.txt' -print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done

for f in `find ../lib/customize -name '*.prf' -print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done

cp ../lib/fonts/*.fon ../lib/fonts/*.woff lib/fonts

for f in `find ../lib/icons \( -name '*.desktop' -o -name '*.svg' \) \
		-print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done
cp ../lib/icons/*.png lib/icons

for f in `find ../lib/tiles \( -name '*.txt' -o -name '*.prf' \) -print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done
cp ../lib/tiles/adam-bolt/*.png lib/tiles/adam-bolt
cp ../lib/tiles/gervais/*.png lib/tiles/gervais
cp ../lib/tiles/nomad/*.png lib/tiles/nomad
cp ../lib/tiles/old/*.png lib/tiles/old
cp ../lib/tiles/shockbolt/*.png lib/tiles/shockbolt

cp ../lib/sounds/*.mp3 lib/sounds

for f in `find ../lib/user/info \( -name '*.txt' -o -name '*.hlp' \) \
		-print` ; do
	cp_unix2dos "$f" `echo "$f" | sed -E -e 's%^\.\./%%'`
done

cd ..
zip -9 -r $OUT $DIR
rm -rf $DIR
