#!/bin/bash -p
#  version date: 2010-07-07 00:25:25 -0400
#
#  bundle:  group files into a distribution package
#
#  Copyright 2010, Sugar Labs
#  Frederick Grose <fgrose@sugarlabs.org>
#
#  adapted from 'The Unix Programming Environment'
#            by Brian W. Kernighan and Rob Pike
#            (Englewood Cliffs, NJ : Prentice-Hall, 1984) p. 98
#
#  Usage:
#       bundle <[@]file1> <file2> [<file3> ... <fileN>] [@bundleFile]
#
#       ./bundleFile [options or arguments for file1]
#
#       The first command line outputs the bundle of input files to @bundleFile
#       when that option is provided with the '@' prefix.  Otherwise, the files
#       are echoed to standard output.
#
#       The second command line unbundles the files into $TMPDIR, and if '@' was
#       prefixed to the file1 parameter in command1, it will be launched after
#       the unbundling. For example, where <file1> = launcher,
#       
#       ${TMPDIR}/launcher $*
#            
#            $*    are options or arguments given to bundleFile and passed on to
#            launcher.
#       
#  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; version 2 of the License.
#
#  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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

export \
   PS4='+(${LINENO}:${BASH_SOURCE}:${EUID}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

# Set up this bundling process (process special @arguments)
bundleFile="${@: -1:1}"
if [[ "${bundleFile:0:1}" = "@" ]]; then
    bundleFile=$(readlink -f "${bundleFile#\@}")
    : > "$bundleFile"
    chmod +x $bundleFile
    set "${@:1:$#-1}"
else
    bundleFile=":"
fi

if [[ "${1:0:1}" = "@" ]]; then
    scriptToLaunch="${1#\@}"
    shift
    set "$scriptToLaunch" $*
    scriptToLaunch=$(basename "$scriptToLaunch")
fi

# Build bundleFile
echo -e '#!/bin/bash
# build date: '$(date +"%Y-%m-%d %H:%M:%S %z")'
#
# To unbundle, bash this file.
#' >> $bundleFile

echo 'export PS4='\''+(${LINENO}:${BASH_SOURCE}:${EUID}): 
        ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'\' >> $bundleFile

echo 'TMPDIR=${TMPDIR:-/tmp}' >> $bundleFile

for i
do
    bname=$(basename $i)
    echo 'cat >|${TMPDIR}/'$bname" <<'End of $bname'" >> $bundleFile
    cat $i >> $bundleFile
    echo "End of $bname" >> $bundleFile
    echo 'chmod 777 ${TMPDIR}/'$bname >> $bundleFile
done

if [[ -n "$scriptToLaunch" ]]; then
    echo 'thisScriptPath=$( readlink -f $0 )' >> $bundleFile
    echo -e '${TMPDIR}/'${scriptToLaunch} '$thisScriptPath $*' >> $bundleFile
    echo "exit 0" >> $bundleFile
fi


