#!/bin/sh
# (c) Etersoft 2003-2005
# Author: Vitaly Lipatov <lav@etersoft.ru>
# BS - build source
# Public domain
# 14.12.03, 04.01.04, 14.01.04, 14.03.04, 25.05.04, 30.05.04, 19.12.04, 06.02.05
# 15.04.05      upload
# 29.05.05    gettext
#      
# :
# -  -
#    -s,  
#    --nodeps,    
#    -u,    incoming
# TODO: -r

. /etc/rpm/etersoft-build-functions

SIGN=""
UPLOADNOW=""
CHECKONLY=""

#############################
name=${0##*/}
Descr="$name - make src.rpm from spec"
Usage="Usage: $name [-s -u -c --nodeps] spec..."

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "Options:"
	echog " -s - sign package(s) and move it to upload dir"
	echog " -c - only sign package(s) with checking"
	echog " -u - sign package(s) and upload it to Incoming"
}

while getopts :hsuc opt; do
    case $opt in
    h) phelp; exit 0;;
    s) SIGN=1 ;;
	c) CHECKONLY=1 ; SIGN=1; ;;
    u) UPLOADNOW=1 ; SIGN=1; ;;
    +?) echog "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
#    ?)  echog "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
	?) OPTIND=$((OPTIND-0)); break;
    esac
done
 
# remove args that were options
shift $((OPTIND - 1))

if [ $# -lt 1 ]; then
    echog "$Usage"
	echog "Use -h for help."
    exit
fi

#echo @@ - $@
build_list_and_args "$@"

#############################
LISTBUILT=""

#    src.rpm
# FIXME: -s can be in name... (but not in option!)
#LISTRPMARGS=
#LISTRPMARGS=${LISTRPMARGS/-m/}
echo "@@ - $LISTRPMARGS - '$MENV'"
pack_src_rpm ${LISTRPMARGS}

#LISTARGS=${LISTARGS/-s/}
if  [ -n "$REMOTEBUILD" ] ; then
	echo "Do not realized yet"
	exit 1
	check_key
	echog "Check access to SSH private key..."
	ssh-add -l || ssh-add
	REMCOM="ssh $BUILDSERVER"

	echo
	echog "############ Build at build server `$BUILDSERVER` ############"
	echo
	echog "==== Copying to build server ====="
	echog "It is recommended to rename old version at server for save traffic during rsync"
	rsync -va --partial --progress --checksum \
	 	-e ssh $LISTBUILT $BUILDSERVER:/$RPMDIR/SRPMS/ || exit 1
	$REMCOM rpmbs --helper $LISTRPMARGS $LISTBUILT 
	exit 0
fi


if [ -n "${SIGN}" ]; then
	echog "Will try to sign follow packages: \$LISTBUILT"
	cd $RPMDIR/SRPMS && rpm --addsign $LISTBUILT || rpm --addsign $LISTBUILT
	RET=$?
	############################################
	echog -n "Checking with sisyphus_check..."
	sisyphus_check --files $LISTBUILT || exit 1
	echog "OK"
	if which rpmlint >/dev/null ; then
		echog -n "Checking with rpmlint..."
	echo
		rpmlint $LISTBUILT
	fi
	test -n "$CHECKONLY" && exit 0
	############################################
	
	if [ "$RET" = "0" ]
	then
		if [ -n "${UPLOADNOW}" ] ; then
			check_key
			INCOMING="Sisyphus"
			case $MENV in
				("M24")
					INCOMING="backports/2.4"
					;;
				("M30")
					INCOMING="3.0-branch"
					;;
				("DD")
					INCOMING="Daedalus"
					;;
			esac
			#uploadOLD=$(basename `mktemp -d $UPLOADDIR.old.XXX`)
			#rmdir $uploadOLD
			echog "Upload to ALT server 'incoming:$INCOMING'"
			rsync -va --partial --progress $CHECKSUM \
			 	-e ssh $LISTBUILT incoming:/incoming/$INCOMING/ && \
			echo "---------------------" && \
			echog "All files synced" || { echog "Error during rsync" ; exit 1 ; }
			#mv $UPLOADDIR $uploadOLD && mkdir $UPLOADDIR

		else
		    CURUP=$UPLOADDIR${MENVARG/-/.}
			mkdir -p $CURUP || echog "Error mkdir $CURUP"
			echog "Copying package(s) in \$CURUP"
			cp -fv $LISTBUILT $CURUP || echog "Error during copying"
		fi
		echog "Removing buildroot..."
		for i in $LISTNAMES ; do
			DDIR=$BUILDROOT/${i/.spec/-buildroot}
			echo -e "\t$DDIR"
			test -n "$BUILDROOT" && rm -rf $DDIR
		done
	else
		echog "Impossible to sign package. Check your password and try again."
		echog "Wait for ten seconds."
		sleep 10
		exit 1
	fi
fi
