#!/bin/sh

###
### distro-helper -- Handle distributing files to the website
### Author's use only.
###
###     Note we depend on the script 'UPLOAD_TO_WEBSITE' to upload
###     the prepared files to the website, which includes upload details
###     not intended for public consumption. For this reason, that script
###     is not released as part of the distro. (It is a simple script that
###     invokes ftp/scp/rcp (or whatever) to push the files onto the web server.
###
###     However, the script below IS included in the distro to ensure
###     the author has an offsite backup of the logic of this script.
###     It may also help others on how to automate such things.
###

# ARGS
OP=$1
VERSION=$2

# GLOBALS
PROGDIR=/net/erco/src/flruler	# src dir available to all machines
PATH="/usr/bin:${PATH}"		# XXX: FC3 needs this to avoid kerberos rsh(1)

# BUILD TAR FILE
#	o Updates version number in script from top of CHANGES.txt file
#	o Builds tar file with version# from top of CHANGES.txt
#
if [ "$OP" = "-tar" ]; then

    ### UPDATE VERSION NUMBERS ###
    ( echo 'g/Version [0-9]*\.[0-9]*/s//Version '$VERSION'/g'; echo 'wq') | \
    	ex flruler.C
    ( echo 'g/Version [0-9]*\.[0-9]*/s//Version '$VERSION'/g'; echo 'wq') | \
    	ex flruler.H
    echo $VERSION > VERSION

    ### CREATE TEMPORARY RELEASE DIRECTORY ###
    rm -rf reldir
    mkdir -m 755 -p reldir/flruler-$VERSION/docs
    mkdir -m 755 -p reldir/flruler-$VERSION/misc
    umask 022

    # SRC
    cp flruler.C flruler.H Makefile Makefile.* make.bat VERSION \
    	distro-helper TODO.txt LICENSE.txt CHANGES.txt README.txt \
	reldir/flruler-$VERSION

    # DOCS
    cp docs/flruler.pod docs/flruler.html docs/flruler.1 \
	reldir/flruler-$VERSION/docs

    # MISC
    cp misc/flruler.ico misc/flruler.icns misc/flruler-icon.png \
    	misc/flruler-Info.plist  \
	reldir/flruler-$VERSION/misc

    # BUILD TAR FILE
    TARFILE="flruler-$VERSION.tar.gz"
    TARCMD="(cd reldir; tar cfz $TARFILE --owner=0 --group=0 flruler-$VERSION)"
    echo "*** Executing: $TARCMD"
    eval $TARCMD
    tar tvfz reldir/$TARFILE
    echo "*** Created: reldir/$TARFILE"
    exit 0
fi

# UPDATE FILES TO WEBSITE, REMOVES TAR FILE
#    Assumes src tar file has been built.
#    Does everything for you.
#
if [ "$OP" = "-sendtar" ]; then
    MMDDYY=`date "+%m/%d/%Y"`
    sed 's%<<VERSION>>%'$VERSION'%g;s%<<MMDDYY>>%'$MMDDYY'%g' \
    	< www/index.shtml.in > www/index.shtml
    ./UPLOAD_TO_WEBSITE \
        reldir/flruler-$VERSION.tar.gz \
        CHANGES.txt \
	www/index.shtml \
	www/flruler.png \
        docs/flruler.html \
	erco/fltk/flruler
    rm -f www/index.shtml
    rm -rf reldir
    exit 0
fi

# ONLY UPDATE THE WEB PAGE
#     Use this when you're just tweaking the distro's html page
#
if [ "$OP" = "-sendwww" ]; then
    MMDDYY=`date "+%m/%d/%Y"`
    sed 's%<<VERSION>>%'$VERSION'%g;s%<<MMDDYY>>%'$MMDDYY'%g' \
    	< www/index.shtml.in > www/index.shtml
    ./UPLOAD_TO_WEBSITE \
        www/index.shtml www/flruler.png \
	erco/fltk/flruler
    rm -f www/index.shtml
    exit 0
fi

# MAKE BINARY DISTROS
if [ "$OP" = "-makebins" ]; then

    # START OUT SUPER CLEAN
    make distclean

    # UPDATE VERSION NUMBERS FOR RELEASE
    ( echo 'g/Version [0-9]*\.[0-9]*/s//Version '$VERSION'/g'; echo 'wq') | \
    	ex flruler.C
    ( echo 'g/Version [0-9]*\.[0-9]*/s//Version '$VERSION'/g'; echo 'wq') | \
    	ex flruler.H
    echo $VERSION > VERSION

    # CREATE BIN DIRECTORIES
    mkdir bin.linux bin.irix bin.darwin bin.windows

    # MAKE LINUX BINARY DISTRO
    make clean; rsh ta "( cd $PROGDIR; make -s )"
    if [ ! -x flruler ]; then
        echo Failed to build LINUX binary
	exit 1
    fi
    tar cfz bin.linux/flruler-linux.tar.gz --owner=0 --group=0 flruler
    echo '*** Created: linux/flruler-linux.tar.gz'

    # MAKE DARWIN BINARY DISTRO
    make clean; rsh xtal "( cd $PROGDIR; make -s; )"
    ls -la flruler.app/Contents/MacOS/flruler > /dev/null 	# XXX: needed to flush nfs
    if [ ! -x flruler.app/Contents/MacOS/flruler ]; then 
        echo Failed to build DARWIN binary
	exit 1
    fi
    tar cfz bin.darwin/flruler-darwin.tar.gz --owner=0 --group=0 flruler.app
    echo '*** Created: bin.darwin/flruler-darwin.tar.gz'

    # MAKE WINDOWS BINARY DISTRO
    make clean; chmod 777 bin.windows
    rsh sup 'cmd /c "vcvars32 && z: && cd \erco\src\flruler && gmake -f Makefile.MICROSOFT"' > /dev/null
    ls -la flruler.exe > /dev/null		# XXX: needed to flush nfs
    if [ ! -f flruler.exe ]; then
        echo Failed to build WINDOWS binary
	exit 1
    fi
    cp flruler.exe bin.windows
    ( cd bin.windows; zip -r -q flruler-windows.zip flruler.exe )
    echo '*** Created: bin.windows/flruler-windows.zip'

    # MAKE IRIX BINARY DISTRO
    make clean; rsh huron "( cd $PROGDIR; gmake -s )"
    ls -la flruler > /dev/null			# XXX: needed to flush nfs
    if [ ! -x flruler ]; then
        echo Failed to build IRIX binary
	exit 1
    fi
    tar cfz bin.irix/flruler-irix.tar.gz --owner=0 --group=0 flruler
    echo '*** Created: bin.irix/flruler-irix.tar.gz'

    ls -la bin.linux/*tar.gz \
	   bin.darwin/*tar.gz \
	   bin.irix/*.tar.gz \
	   bin.windows/*zip

    echo ''; echo 'Now execute "make sendbins" to update website'
    exit 0
fi

# ONLY UPDATE THE WEB PAGE
#     Use this when you're just tweaking the html page
#
if [ "$OP" = "-sendbins" ]; then
    if [ ! -f bin.windows/flruler-windows.zip  ]; then echo No windows zip file; exit 1; fi
    if [ ! -f bin.irix/flruler-irix.tar.gz     ]; then echo No irix    tar file; exit 1; fi
    if [ ! -f bin.linux/flruler-linux.tar.gz   ]; then echo No linux   tar file; exit 1; fi
    if [ ! -f bin.darwin/flruler-darwin.tar.gz ]; then echo No darwin  tar file; exit 1; fi
    ./UPLOAD_TO_WEBSITE \
        bin.linux/*tar.gz \
	bin.darwin/*tar.gz \
	bin.irix/*.tar.gz \
	bin.windows/*zip \
        erco/fltk/flruler
    exit 0
fi

echo "${0}: unknown operation '$OP'"
exit 1
