#!/bin/sh
#
# Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# This program 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 3 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, see <http://www.gnu.org/licenses/>.
# ===
#
# make-setup.sh
#
# Builds a "setup" self-extracting archive -- or a payload file for
# one -- derived from running "make install" into a temporary directory.
#
# usage: make-setup.sh [-d] <output> {<stub>|NONE} <pack-utility> <icon>
#
# Normally run by "make setup" in the "src/gui".
#
# The packed files are those that appear under "/usr", which 
# matches the default installation directory presented in the 
# gui (see Dir::os_install()).
#
# There is only one level of packing, so the gui executable 
# should be statically linked or repacked with its shared 
# libraries if targeting other machines.
#

# parse the command line
debug="0" ; if test "$1" = "-d" ; then shift ; debug="1" ; fi
setup="$1"
stub="$2"
pack="$3"
icon="$4"

# define temporary directories and cleanup code
tmp="/tmp/`basename $0 .sh`.$$.tmp"
install="$tmp/install"
list="$tmp/list"
trap Cleanup 0 
trap Fail 1 2 3 13 15
Cleanup()
{
	if test "$debug" -eq 0
	then
		rm -rf "$tmp"
	fi
}
Fail()
{
	Cleanup
	exit 1
}

# check the command-line
if test "$setup" = "" -o \( "$stub" != "NONE" -a ! -f "$stub" \) -o ! -x "$pack"
then
	echo usage: `basename $0` '<setup> <stub> <pack>' >&2
	exit 2
fi

# run "make install" 
echo `basename $0`: running make install into $install
mkdir -p $install
( cd ../.. && make install HAVE_DOXYGEN=no DESTDIR=$install ) > /dev/null 2>&1

# check the "./configure" directories were sane (see bin/configure-fhs.sh)
if test ! -d "$install/usr/lib/emailrelay" -a ! -d "$install/Applications/E-MailRelay"
then
	echo `basename $0`: cannot see expected directories in the install tree >&2
	exit 1
fi

# add some extras into the install
if test -d "$install/usr/lib/emailrelay/"
then
	cp "$icon" $install/usr/lib/emailrelay/
else
	cp "$icon" $install/Applications/E-MailRelay/
fi

# build a file list
Filter()
{
	fgrep -v /doxygen/ | fgrep -v /emailrelay-gui
}
Edit()
{
	sed 's: \./: :'
}
echo `basename $0`: building a file list
find $install -type f | Filter | grep -n . | sed 's/:/1 /' > $list
( cd $install && find . -type f ) | Filter | grep -n . | sed 's/:/2 /' | Edit >> $list
sort -n $list | sed 's/^[0-9][0-9]* //' > $list.tmp && mv $list.tmp $list

# create the packed file - dont bother compressing any more (-p)
echo `basename $0`: packing
PATH=".:$PATH"
"$pack" -p -q -f "$list" "$setup" "$stub"

