#!/bin/bash
#
# check in package to /work/src/done/STABLE or NOARCH
#
# Originally written by		Klaus Kaempf <kkaempf@suse.de>
# modified / beautified by	Stefan Hundhammer <sh@suse.de>
#				Arvin Schnell <arvin@suse.de>
# Changed for LiMaL             Stefan Schubert <schubi@suse.de>                              
#
# $Id: checkin-stable 32 2005-02-07 16:12:08Z schubi $

self=`basename $0`
force=0

if [ "$1" == "-f" -o "$1" == "--force" ]; then
    force=1
fi

if [ ! -f RPMNAME -o ! -f VERSION ]; then
    echo "${self}: FATAL: ./RPMNAME or ./VERSION not found" >&2
    exit 1
fi

pkg_rpmname=`cat RPMNAME`
pkg_version=`cat VERSION`

# check that the devtools version match
if [ "$pkg_rpmname" != "limal-devtools" ] ; then
    v1=`echo $pkg_version | cut -d . -f 1,2`
    v2=`limaltool version | cut -d . -f 1,2`
    if [ "$v1" != "$v2" ] ; then
	echo "${self}: FATAL: The major version of your limal-devtools and this" >&2
	echo "package don't match. Maybe your limal-devtools is outdated or you" >&2
	echo "want to build a package for an old distribution." >&2
	exit 1
    fi
fi

src_dir=package
spec_file=$src_dir/${pkg_rpmname}.spec

dist=NOARCH
if [ -z "`grep BuildArchitectures package/${pkg_rpmname}.spec | grep noarch`" ]; then
    dist=STABLE
fi

target_dir=/work/src/done/$dist/$pkg_rpmname
# target_dir=/tmp/done/$dist/$pkg_rpmname	# DEBUG

if [ -d $target_dir ]; then
    if [ $force -ne 0 ]; then
	echo "Force mode - deleting old $target_dir"
	rm -rf $target_dir
    else
	echo "${self}: FATAL: Directory $target_dir already exists - use "--force" to override" >&2
	exit 2
    fi
fi

mkdir $target_dir
cp $spec_file			$target_dir
cp $src_dir/${pkg_rpmname}.changes	$target_dir

sources=`gawk -F : '/^Source/ || /^Patch/ { print $2; }' $spec_file | sed -e 's/^[[:space:]]*/package\//'`

for source in $sources; do
    if [ -f $source ]; then
	cp $source $target_dir
    else
	echo "${self}: FATAL: Can't find $source"
	rm -rf $target_dir	# Don't leave junk behind!
	exit 42
    fi
done

echo ""
echo ""
echo "Package \"$pkg_rpmname\" checked into $dist:"
echo ""
echo "ls -l $target_dir"
echo ""
( cd $target_dir; LANG=C ls -l )
echo ""

# EOF
