#!/bin/sh

release=true

bindos=bin/djdemo.exe
binlnx=bin/demo
binwin=bin/demo-win32.exe
binarm=bin/demopi
binbsd=bin/demobsd
binsgi=bin/demosgi
binosx=bin/demo-macosx

if [ ! -f $bindos ]; then
	echo "DOS build missing (expected: $bindos)"
	exit 1
fi
if [ ! -f $binlnx ]; then
	echo "GNU/Linux x86 build missing (expected: $binlnx)"
	exit 1
fi
if [ ! -f $binwin ]; then
	echo "Win32 build missing (expected: $binwin)"
	exit 1
fi
if [ ! -f $binarm ]; then
	echo "Warning: ARM GNU/Linux build missing (expected: $binarm)"
	binarm=
fi
if [ ! -f $binbsd ]; then
	echo "Warning: FreeBSD build missing (expected: $binbsd)"
	binbsd=
fi
if [ ! -f $binsgi ]; then
	echo "Warning: SGI IRIX build missing (expected: $binsgi)"
	binsgi=
fi
if [ ! -f $binosx ]; then
	echo "Warning: MacOS X build missing (expected: $binosx)"
	binosx=
fi

if [ ! -f cwsdpmi.exe ]; then
	echo "cwsdpmi.exe not found"
	exit 1
fi

if $release; then
	outdir=aleph0
	archive=aleph0.zip
	echo "packaging release archive: $archive ..."
else
	outdir=alephpre
	archive=aleph-preview.zip
	echo "packaging preview archive: $archive ..."
fi

rm -rf $outdir
mkdir -p $outdir
cp $bindos $outdir/aleph0.exe
cp $binlnx $outdir/aleph0
strip $outdir/aleph0
cp $binwin $outdir/alephwin.exe
[ -n "$binarm" ] && cp $binarm $outdir/alephpi
[ -n "$binbsd" ] && cp $binbsd $outdir/alephbsd
[ -n "$binsgi" ] && cp $binsgi $outdir/alephsgi
[ -n "$binosx" ] && cp $binosx $outdir/alephosx

chmod 775 $outdir/*
cp cwsdpmi.exe $outdir/cwsdpmi.exe

mkdir -p $outdir/data $outdir/data/3dzoom $outdir/data/bump $outdir/data/grise
cp data/* $outdir/data 2>/dev/null
cp data/3dzoom/* $outdir/data/3dzoom
cp data/bump/* $outdir/data/bump
cp data/grise/* $outdir/data/grise
cp demo.seq $outdir/data/demo.seq

cp aleph0.nfo $outdir/aleph0.nfo
unix2dos $outdir/aleph0.nfo

echo 'debug = false' >$outdir/demo.cfg

if ! $release; then
	echo 'PREVIEW BUILD, DO NOT DISTRIBUTE' >$outdir/PREVIEW.txt
fi

git archive -o $outdir/alephsrc.zip --prefix=alephsrc/ HEAD

rm -f $archive
zip -rq $archive $outdir
