#!/bin/sh
#
# Shorewall Release Processing -- (C) 2003,2004,2005 -- Tom Eastep (teastep@shorewall.net)
#                              -- (C) 2005,2006 -- Cristian Rodriguez (webmaster@shorewall.net)
# Version : $Id$
#
#     This program is under GPL [http://www.gnu.org/copyleft/gpl.htm]
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of Version 2 of the GNU General Public License
#	as published by the Free Software Foundation.
#
#	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., 675 Mass Ave, Cambridge, MA 02139, USA
#
# I install this script in /usr/local/bin/makeshorewall.
#
# Usage:
#
#   makeshorewall [ -trhxl ] <version> [ <previous version> ]
#
#      -t      Build tarball
#      -r      Build RPM
#      -h      Build HTML documentation
#      -x      Build XML documentation
#
# If no options are given, all options are assumed.
#
# If <previous version> is given, a patch file reflecting the differences
# between that version and the current version ( <version> ) is
# generated. The directory ./shorewall-<previous version> must exist and
# contain the version against which the patch is generated.
################################################################################
#                          C O N F I G U R A T I O N
################################################################################
#
# XSL Stylesheet to use for XML->HTML conversion
#
STYLESHEET=/usr/share/xml/docbook/stylesheet/nwalsh/current/xhtml/docbook.xsl
#
# Directory where the build log will be placed. The log has the name
# shorewall_build_<version>.log
#
LOGDIR=$PWD
#
# Your RPM build directory
#
RPMDIR=~/rpm/
#
# Directory where you want the release to be built
#
DIR=$PWD
################################################################################
#                               V A R I A B L E S
################################################################################
VERSION=
OLDVERSION=
SHOREWALLDIR=
SOURCEDIR=
SVNBRANCH=
XMLPROJ=
RPMNAME=
TARBALL=
LOGFILE=
HTMLDIR=
BUILDTARBALL=
BUILDRPM=
BUILDXML=
BUILDHTML=
SAMPLESTAG=
################################################################################
#                               F U N C T I O N S
################################################################################
progress_message()
{
    echo         >>    $LOGFILE
    echo "$@" | tee -a $LOGFILE
    echo         >>    $LOGFILE
}

report()
{
    echo "$@" | tee -a $LOGFILE
}

do_or_die()
{
    eval $@ || { progress_message "Step \"$*\" FAILED" ; exit 2; }
}

fatal_error() {
    progress_message "$*"
    exit 2
}

list_search() # $1 = element to search for , $2-$n = list
{
    local e=$1

    while [ $# -gt 1 ]; do
	shift
	[ "x$e" = "x$1" ] && return 0
    done

    return 1
}

usage()
{
    echo "usage: $(basename $0) [ -trhxl] <version> [ <old-version> ]"
    exit 2
}
################################################################################
#                E X E C U T I O N   S T A R T S   H E R E
################################################################################

set -e
set -u

done=

[ $# -eq 0 ] && usage

case $1 in
    -*)
	;;
    *)
	BUILDTARBALL=Yes
	BUILDRPM=Yes
	BUILDHTML=Yes
	BUILDXML=Yes
	BUILDRPM=Yes
	done=Yes
	;;
esac

while [ -z "$done" ]; do
    [ $# -eq 0 ] && break

    option=$1
    case $option in
	-*)
	    option=${option#-}

	    [ -z "$option" ] && break

	    while [ -n "$option" ]; do
		case $option in
		    t*)
			BUILDTARBALL=Yes
			option=${option#t}
			;;
		    r*)
			BUILDTARBALL=Yes
			BUILDRPM=Yes
			option=${option#r}
			;;
		    h*)
			BUILDHTML=Yes
			option=${option#h}
			;;
		    x*)
			BUILDXML=Yes
			option=${option#x}
			;;
	    	    *)
			usage
			;;
		esac
	    done
	    shift
	    ;;
	*)
	    done=Yes
	    ;;
    esac
done

case $# in
    1)
	;;
    2)
	OLDVERSION=$2
	;;
    *)
	usage
	;;
esac

VERSION=$1
LOGFILE=$LOGDIR/shorewall_build_${VERSION}.log
# location and options for GnuPG
GPG="/usr/bin/gpg -ab --batch --comment 'To verify this, you can download our public key at https://lists.shorewall.net/shorewall.gpg.key'"
touch $LOGFILE
progress_message "Build of Shorewall $VERSION on $(date)"

case $VERSION in
    2.4.*)
	SVNBRANCH="branches/2.4/Shorewall"
	DOCTAG="branches/2.4/docs"
	XMLPROJ="docs-2.4"
	;;
    3.0.*)
	SVNBRANCH="branches/3.0/Shorewall"
	DOCTAG="trunk/docs"
	XMLPROJ="docs-3.0"
	SAMPLESTAG="trunk/Samples"
	;;
    3.1.*)
	SVNBRANCH="trunk/Shorewall"
	DOCTAG="trunk/docs"
	XMLPROJ="docs-3.1"
	SAMPLESTAG="trunk/Samples"
	;;
    *)
	echo "Unsupported Version: $VERSION"
	exit 2
	;;
esac

[ -d $DIR ] || { echo "Directory $DIR does not exist or is unaccessible" ; exit 2 ; }

progress_message "Distribution directory is $DIR"

cd $DIR

case $VERSION in
    *Beta*|*RC*)
	#
	# Beta or Release Canditate
	#
	SHOREWALLDIR=shorewall-${VERSION%-*}
	TARBALL=shorewall-${VERSION%-*}.tgz
	RPMNAME=shorewall-${VERSION%-*}-0${VERSION#*-}.noarch.rpm
	;;
    *)
	#
	# Normal Release
	#
	SHOREWALLDIR=shorewall-$VERSION
	TARBALL=shorewall-$VERSION.tgz
	RPMNAME=shorewall-${VERSION}-1.noarch.rpm
	;;
esac

HTMLDIR=shorewall-docs-html-$VERSION

if [ -n "${BUILDTARBALL}${BUILDRPM}" ]; then
    report "Shorewall directory is $DIR/$SHOREWALLDIR"
    report "SVN tag is $SVNBRANCH"
    [ -n "$BUILDTARBALL" ] && report "TARBALL is $TARBALL"
    [ -n "$BUILDRPM" ]     && report "RPM is $RPMNAME"
fi

[ -n "$BUILDHTML" ] && report "HTML Directory is $HTMLDIR"

if [ -n "${BUILDTARBALL}${BUILDRPM}" ]; then
    progress_message "Exporting $SVNBRANCH from SVN..."

    rm -rf $SHOREWALLDIR

	do_or_die "svn export --non-interactive --force https://svn.sourceforge.net/svnroot/shorewall/$SVNBRANCH  $SHOREWALLDIR >> $LOGFILE 2>&1"

    fgrep VERSION=$VERSION $SHOREWALLDIR/install.sh > /dev/null 2>&1     || fatal_error "install.sh has wrong version"
    fgrep VERSION=$VERSION $SHOREWALLDIR/uninstall.sh > /dev/null 2>&1   || fatal_error "uninstall.sh has wrong version"
    fgrep VERSION=$VERSION $SHOREWALLDIR/fallback.sh > /dev/null 2>&1    || fatal_error "fallback.sh has wrong version"
    [ -f $SHOREWALLDIR/shorecap ] && \
	{ fgrep VERSION=$VERSION $SHOREWALLDIR/shorecap > /dev/null 2>&1 || fatal_error "shorecap has wrong version"; }

    if [ -n "$SAMPLESTAG" ]; then
	cd $SHOREWALLDIR
	do_or_die "svn export --non-interactive --force https://svn.sourceforge.net/svnroot/shorewall/$SAMPLESTAG Samples >> $LOGFILE 2>&1"
	cd $DIR
    fi

    do_or_die "rm -rf $SHOREWALLDIR/debian"

    if [ -n "$BUILDTARBALL" ]; then
	progress_message "Creating $DIR/$TARBALL..."
	do_or_die "tar -zcvf $TARBALL $SHOREWALLDIR >> $LOGFILE 2>&1"
        do_or_die "tar -jcvf shorewall-${VERSION%-*}.tar.bz2 $SHOREWALLDIR >> $LOGFILE 2>&1"
	for shoresuffix in tgz tar.bz2; do
	    shoreball=shorewall-${VERSION%-*}.${shoresuffix}
	    report "GPG signing $DIR/$shoreball"
	    rm -f ${shoreball}.asc
	    do_or_die "$GPG $shoreball"
        done
    fi

    if [ -n "$BUILDRPM" ]; then
	progress_message "Building $RPMNAME..."

	do_or_die "rpmbuild -tb --sign $TARBALL >> $LOGFILE 2>&1"
	do_or_die cp -a $RPMDIR/RPMS/noarch/$RPMNAME .
    fi
fi

if [ -n "${BUILDXML}${BUILDHTML}" ]; then
    progress_message "Exporting $XMLPROJ from SVN..."

    rm -rf $XMLPROJ
    rm -rf shorewall-docs-xml-$VERSION

    do_or_die "svn export --non-interactive --force https://svn.sourceforge.net/svnroot/shorewall/$DOCTAG $XMLPROJ >> $LOGFILE 2>&1"
    do_or_die mv $XMLPROJ shorewall-docs-xml-$VERSION

    rm -f shorewall-docs-xml-$VERSION/images/*.vsd
    rm -f shorewall-docs-xml-$VERSION/images/~*
    rm -f shorewall-docs-xml-$VERSION/images/*.JPG
    rm -f shorewall-docs-xml-$VERSION/images/publish
    rm -f shorewall-docs-xml-$VERSION/images/Thumbs.db

    if [ -n "$BUILDXML" ]; then
	progress_message "Creating $DIR/shorewall-docs-xml-$VERSION tarballs"
        do_or_die "tar -zcvf shorewall-docs-xml-$VERSION.tgz shorewall-docs-xml-$VERSION >> $LOGFILE 2>&1"
        do_or_die "tar -jcvf shorewall-docs-xml-$VERSION.tar.bz2 shorewall-docs-xml-$VERSION >> $LOGFILE 2>&1"
	for shoresuffix in tgz tar.bz2; do
	    xmlball=shorewall-docs-xml-$VERSION.${shoresuffix}
	    report "GPG signing $DIR/$xmlball tarball"
	    rm -f ${xmlball}.asc
	    do_or_die "$GPG $xmlball"
        done
    fi

    if [ -n "$BUILDHTML" ]; then
	progress_message "Building $HTMLDIR ..."

	rm -rf $HTMLDIR

	do_or_die mkdir $HTMLDIR
	do_or_die mkdir $HTMLDIR/images
	#
	# The original HTML documents were created using MS FrontPage and used
	# the .htm suffix. The remainder use the .html suffix.
	#
	HTMFILES="
	    6to4.htm
	    blacklisting_support.htm
	    configuration_file_basics.htm
	    CorpNetwork.htm
	    dhcp.htm
	    Documentation.htm
	    errata.htm
	    fallback.htm
	    FAQ.htm
	    GnuCopyright.htm
	    Install.htm
	    IPIP.htm
	    IPSEC.htm
	    kernel.htm
	    myfiles.htm
	    NAT.htm
	    ports.htm
	    PPTP.htm
	    ProxyARP.htm
	    quotes.htm
	    samba.htm
	    shorewall_extension_scripts.htm
	    shorewall_features.htm
	    shorewall_mirrors.htm
	    shorewall_prerequisites.htm
	    shorewall_quickstart_guide.htm
	    shorewall_setup_guide_fr.htm
	    shorewall_setup_guide.htm
	    Shorewall_sfindex_frame.htm
	    standalone.htm
	    starting_and_stopping_shorewall.htm
	    support.htm
	    three-interface.htm
	    traffic_shaping.htm
	    troubleshoot.htm
	    two-interface.htm
	    upgrade_issues.htm
	    VPN.htm
	    whitelisting_under_shorewall.htm"

	for file in shorewall-docs-xml-$VERSION/*.xml; do
	    a=$(basename $file)
	    b=${a%.*}
	    list_search $b.htm $HTMFILES && b=$b.htm || b=$b.html
	    f="shorewall-docs-html-$VERSION/$b"

	    report "Converting $DIR/$file from XML to HTML ($DIR/$f) ..."

	    do_or_die xsltproc --output $f --stringparam html.stylesheet html.css --stringparam ulink.target _self -param toc.section.depth 3 $STYLESHEET $file
	done

	progress_message "Copying images to $DIR/$HTMLDIR/images ..."

	do_or_die cp -a shorewall-docs-xml-$VERSION/images/*.png $HTMLDIR/images
	do_or_die cp -a shorewall-docs-xml-$VERSION/images/*.gif $HTMLDIR/images
	do_or_die cp -a shorewall-docs-xml-$VERSION/images/*.jpg $HTMLDIR/images
	do_or_die cp -a shorewall-docs-xml-$VERSION/*.css $HTMLDIR

	do_or_die ln -s Documentation_Index.html shorewall-docs-html-$VERSION/index.html

	progress_message "Creating $DIR/shorewall-docs-html-$VERSION tarballs ..."

	do_or_die "tar -zcvf shorewall-docs-html-$VERSION.tgz shorewall-docs-html-$VERSION >> $LOGFILE 2>&1"
	do_or_die "tar -jcvf shorewall-docs-html-$VERSION.tar.bz2 shorewall-docs-html-$VERSION >> $LOGFILE 2>&1"
	for shoresuffix in tgz tar.bz2; do
	    htmlball=shorewall-docs-html-$VERSION.${shoresuffix}
	    report "GPG signing $DIR/$htmlball tarball"
	    rm -f ${htmlball}.asc
	    do_or_die "$GPG $htmlball"
	done
    fi
fi

[ -n "$BUILDTARBALL" ] && case $VERSION in
    *Beta*|*RC*)
	#
	# The original tarball created above didn't include the -Beta or -RC portion of the
	# name in either the tarball name or the directory name. Create it here
	#
	progress_message "Creating $DIR/shorewall-$VERSION..."

	rm -rf shorewall-$VERSION

	do_or_die mv $SHOREWALLDIR shorewall-$VERSION

	progress_message "Creating $DIR/shorewall-${VERSION}.tgz ..."

        do_or_die "tar -zcvf shorewall-${VERSION}.tgz shorewall-$VERSION >> $LOGFILE 2>&1"
        do_or_die "tar -jcvf shorewall-$VERSION.tar.bz2 shorewall-$VERSION >> $LOGFILE 2>&1"

	for shoresuffix in tgz tar.bz2; do
	    betaball=shorewall-$VERSION.${shoresuffix}
	    report "GPG signing $DIR/$betaball tarball"
	    rm -f ${betaball}.asc
	    do_or_die "$GPG $betaball"
        done
	;;
esac

progress_message "Creating md5sums and sha1sums"

rm -f $VERSION.md5sums $VERSION.sha1sums

for file in *; do
    case $file in
	*$VERSION[-.]*tgz|*$VERSION[-.]*rpm|*$VERSION[-.]*bz2)
	    do_or_die "md5sum $file >> $VERSION.md5sums"
	    do_or_die "sha1sum $file >> $VERSION.sha1sums"
	    ;;
    esac
done

if [ -n "$OLDVERSION" ]; then
    progress_message "Creating patch-$VERSION ..."

    diff -Naur shorewall-$OLDVERSION shorewall-$VERSION > patch-$VERSION || true
fi

progress_message "Shorewall $VERSION Build complete - $(date)"