#!/usr/bin/env bash
# Make everything.  Really.
# Must be run in the top-level directory.
# By default, builds but does not release.
#  With -release, releases.
# By default, builds everything.
#  Can specify individual products as arguments.

# If anything fails, stop.
set -e

# Make sure 'sort' works correctly.
export LC_ALL="C"

build=1
release=""
all_prods="x3270 tcl3270 s3270 pr3287 c3270 wc3270 wpr3287 ws3270 Playback"

function usage
{
	echo >&2 "Usage: $0 [--release] [--win32] [<product>...]"
	exit 0
}

# Parse the options.
while [ $# -gt 0 ]
do	case "$1" in
	-help|--help)
		usage
		;;
	-release|--release)
		release=1
		shift
		;;
	-win32|--win32|-windows|--windows)
		all_prods="wc3270 wpr3287 ws3270"
		just_win32=1
		shift
		;;
	-*)
		usage
		;;
	*)	break
		;;
	esac
done

# Set up the product list.
if [ $# -gt 0 ]
then	prods=$*
else	prods=$all_prods
    	[ -z "$just_win32" ] && all=1
fi

# Check for a full Windows release.
if [[ "$build" && \
      "$prods" =~ wc3270 && \
      "$prods" =~ ws3270 && \
      "$prods" =~ wpr3287 ]]
then	winrel=1;
fi

# Make sure the product directories exist.
for i in $prods
do	if [ ! -d "$i" ]
	then	echo >&2 "No $i directory -- must be run from top-level dir."
		exit 1
	fi
done

# Trace whatever we do.
#set -x

# Pull in the version.
source Common/version.txt

d1=$PWD
reldir=$d1/../../Release

suite=suite3270-$version-src.tgz
setup=wc3270-$version-setup.exe
zip32=wc3270-$version-noinstall-32.zip
zip64=wc3270-$version-noinstall-64.zip
ver=${version%%[a-z]*}

# Check for existing targets before we do any real work.
if [ "$release" ]
then	for prod in $prods
	do	if [ -f $reldir/$prod-$version-src.tgz ]
		then	echo >&2 "$prod-$version-src.tgz already exists"
			exit 1
		fi
	done
	if [ "$all" -a -f $reldir/$suite ]
	then	echo >&2 "$suite already exists"
		exit 1
	fi
	if [ "$winrel" -a -f $reldir/$setup ]
	then	echo >&2 "$setup already exists"
	    	exit 1
	fi
	if [ "$winrel" -a -f $reldir/$zip32 ]
	then	echo >&2 "$zip32 already exists"
	    	exit 1
	fi
	if [ "$winrel" -a -f $reldir/$zip64 ]
	then	echo >&2 "$zip64 already exists"
	    	exit 1
	fi
fi

# Build the tarball.
echo "===== Building the tarball."
make -f Makefile.aux -j4 src.tgz

# Work in a clean temporary directory
tmpdir=/hd/xfer/mka
rm -rf $tmpdir
mkdir $tmpdir
cd $tmpdir
tar -xzf $d1/$suite
builddir=$tmpdir/suite3270-$ver

cd $builddir

# Configure.
if [ "$prods" != Playback ]
then	# Configure.
	for prod in $prods
	do	if [ "$prod" != Playback ]
	    	then	enable="$enable --enable-$prod"
		fi
	done
	echo "===== Configuring."
	./configure $enable

	# Build.
	echo "===== Building."
	make -j4 CDEBUGFLAGS="-g -Werror"
fi

# Test the installation.
for prod in $prods
do
	cd $builddir

	# Build it.
	d2=$d1/$prod
	if [ "$build" ]
	then	cd $prod
	    	if [ "$prod" = Playback ]
		then	echo "===== Building $prod."
			make -j4
		fi
		if [ -f $d2/instlist.base ]
		then	echo "===== Test installing $prod (base)."
			mkdir i
			make install DESTDIR=$PWD/i
			find i -print | tail -n +2 | sed 's/^..//' | sort >cmp.files
			diff $d2/instlist.base cmp.files || exit 1
			rm -rf i
		fi
		if [ -f $d2/instlist.man ]
		then	echo "===== Test installing $prod (man)."
			mkdir i
			make install.man DESTDIR=$PWD/i
			find i -print | tail -n +2 | sed 's/^..//' | sort >cmp.files
			diff $d2/instlist.man cmp.files || exit 1
			rm -rf i
		fi
	fi
done

# Release wc3270 via Inno Setup under Wine.
if [ "$winrel" ]
then	echo "===== Building setup.exe and noinstall.zip."
	cd $builddir
	make -f Makefile.aux -j4 windows-release
fi

# Release each product.
if [ "$release" ]
then	echo "===== Releasing."

	# Release the tarball.
	if [ "$all" ]
	then	cp -p $d1/$suite $reldir/
		chmod ugo-w $reldir/$suite
	fi

	# Release the setup.exe and zip file.
	if [ "$winrel" ]
	then	cp -p $builddir/wc3270/$setup $builddir/wc3270/$zip32 $builddir/wc3270/$zip64 $reldir/
		chmod ugo-w $reldir/$setup $reldir/$zip32 $reldir/$zip64
	fi
fi
