#!/bin/sh

# Script to publish project as src.rpm on project ftp directory

NEEDETERBUILD=162

# You can use this script separately in your project with
#. /usr/share/eterbuild/eterbuild
# instead follow loader

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common

load_mod spec rpm etersoft

if [ "$1" = "-h" ]; then
	echog "rpmpub - Etersoft specific script for publish gear repo to target ftp dir"
	echog "Usage: rpmpub [-r PROJECTVERSION ] [SPEC] [TARGETDIR]"
	echog "You can set default target dir in UPLOADDIR variable in config file"
	exit 0
fi



# publish to unstable by default
ALPHA=
if [ "$1" = "-r" ] ; then
	ALPHA=$2
	shift 2
fi

if [ -n "${1/*.spec/}" ] ; then
	#SPECNAME=$(basename $(pwd)).spec
	SPECNAME=$(echo *.spec)
else
	SPECNAME=$1
	shift
fi

if [ ! -r $SPECNAME ] ; then
	fatal "Spec $SPECNAME does not found in the current dir"
fi

#ETERDESTSRPM=/var/ftp/pub/Etersoft/CIFS@Etersoft/$VERSION/sources
ETERDESTSRPM=$1
if [ -z "$ETERDESTSRPM" ] ; then
	ETERDESTSRPM=$(get_etersoft_srpm_path $SPECNAME "$ALPHA")
	echo "Prepare src.rpm to $ETERDESTSRPM"
else
	test -d "$ETERDESTSRPM" || fatal "You need run with exists dir to publish src.rpm project (I checked $ETERDESTSRPM, it is nonexist)"
fi
export ETERDESTSRPM

# just for your information: rpmbs supports ssh-like target
$ETERBUILDBIN/rpmbs -s $SPECNAME || fatal "Can't build SRPMS"

# broken
# if known target and last link already exists
#if [ -L "$ETERDESTSRPM/../../last" ] ; then
	# set last link (assume PROJECT/VERSION/sources dir structure)
#	set_last_link $ETERDESTSRPM/.. $(get_version $SPECNAME)
#fi
