#!/bin/sh
# make_package.sh
#
# Simplifies the creation of SD-UX packages
#
# Copyright (c) 2002-2007, Olivier S. Masse
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * Neither the name of Olivier S. Masse nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
 
notfinished="true"
while [ "$notfinished" ]
do
	case $1 in
		-p)	product=$2; shift;;
		-v)	version=$2; shift;;
		-c)	cfgscript=$2; shift;;
		-u)	uncfgscript=$2; shift;;
		-co)    corequisites=$2; shift;;
		-t)     title="$2"; shift;;
		-b)	reboot=1;;
		-f)	shift; paths="$*"; unset notfinished;;
		-listfile)  listfile=$2; shift;;
	esac
	[ "$1" != "" ] && shift || unset notfinished
done

if [ "$listfile" ]
then
	cat $listfile | while read path
	do
		paths="$paths $path"
	done
fi

if [ ! "$product" -o ! "$version" -o ! "$paths" ]
then
        echo
	echo "Simplifies the creation of SD-UX packages"
	echo
        echo "Usage: $(basename $0)"
	echo "          -p product"
	echo "          -v version"
	echo "          -t 'title'"
	echo "         [ -b (force a reboot) ]"
	echo "         [-c configure_script]"
	echo "         [-u unconfigure_script]"
	echo "         [-co 'corequisites']"
	echo "          -f list_of_files ... "
	echo "          -listfile file_with_a_list"
	echo
        echo "Example: $(basename $0) -p Gizmo -v 1.0 -f /opt/gizmo /sbin/init.d/gizmo"
        echo "         $(basename $0) -p Gizmo -t 'Gizmomatic App' -v 2.7.1 -listfile /tmp/filelist.txt"
        echo
        exit
fi

add_dir()
{
        dir="$1"
        echo "          directory       $dir"
}

add_file()
{
        file="$1"
        echo "          file            $file"
}

add_path()
{
	path="$1"
	for item in $(find $path)
	do
		if [ -d "$item" ] 
		then
			[ ! -h $item ] && add_dir $item
		fi
		[ -h $item -o -f $item ] && add_file $item
	done
}

make_psf()
{
        echo "product"
        echo "  tag             $product"
	
	[ "$title" ] && echo "  title           $title" || echo "  title           $product"

        echo "  revision        $version"
        echo "  fileset"
        echo "          tag     $product"
	
	[ "$reboot" = "1" ] && echo "          is_reboot       true"

	[ "$cfgscript" ] && echo "          configure       $cfgscript"
	[ "$uncfgscript" ] && echo "          unconfigure     $uncfgscript"
	[ "$corequisites" ] && echo "          corequisites    $corequisites"

        for path in $paths
        do
		add_path $path
        done
        echo "  end"
        echo "end"
}

make_pkg()
{
        [ -f $product.depot ] && rm $product.depot
        touch $product.depot
        swpackage -vv -s $product.psf -d $product.depot -x media_type=tape
}

make_psf > $product.psf
make_pkg

echo "Package has been created as file $product.depot"

#rm $product.psf

# EOF

