#!/bin/bash
# 2003-2008 Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain

set_eterbuilddir()
{
	[ -z "$ETERBUILDDIR" ] || return 0

	if dirname $0 | grep "^/usr" >/dev/null ; then
		ETERBUILDETC=/etc/eterbuild
		ETERBUILDDIR=/usr/share/eterbuild
	else
		# if run from no system installation
		ETERBUILDDIR=$(realpath `dirname $0`/../share/eterbuild)
		ETERBUILDETC=$(realpath `dirname $0`/../etc)
		echo "Note: run from source tree, datadir=$ETERBUILDDIR, sysconfdir=$ETERBUILDETC"
	fi
	test -n "$ETERBUILDDIR"
	# returns test result
}

# set if empty and check it
if ! set_eterbuilddir ; then
	echo "Can't detect package files location" >&2
	exit 1
fi

#        
fatal()
{
	echog "Error: $@" >&2
	exit 1
}

#   
warning()
{
	echog "Warning: $@" >&2
}

print_list()
{
	local i
	for i in $@ ; do
		echo "    $i"
	done
}

is_alt()
{
	test -f /etc/altlinux-release
	return $?
}

if [ "$UID" = "0" ] ; then
	fatal "It is strict recommended do not use these scripts as root"
fi

. $ETERBUILDDIR/functions/config
. $ETERBUILDDIR/functions/gettext
. $ETERBUILDDIR/functions/spec


# Internal
set_target_type()
{
	if echo $1 | grep "^M[0-9][0-9]$" >/dev/null ; then
		MENV=$1
		return 0
	fi
	case "$1" in
		("DD" | "SS" | "EE")
			MENV="$1"
			return 0;
			;;
	esac
	return 1;
}

# Internal
# ,    :   -M23 -M24   ,   
detect_target_env()
{
local DISTRNAME=`distr_vendor -e`
MENVARG=""
TARGET=`distr_vendor -p`
VENDOR=`distr_vendor -s`

if [ "$VENDOR" = "alt" ] && [ -n "$MENV" ] ; then
	[ -n "$APTCONF" ] || APTCONF=${APTCONFBASE/.SS}.$MENV
	
	[ -n "$VERBOSE" ] && echog "Target ALT Linux system: `get_distr_version $MENV`, use \$APTCONF"
	MENVARG="-$MENV"
else
	echog "Distribution: \$DISTRNAME (\$VENDOR) (target package: \$TARGET)"
fi
}

#    LISTNAMES
#     MENV
#  LISTARGS   
# :
# -   (MENV)
# -  
# -     
parse_cmd_pre()
{
local i
if [ $# -lt 1 ]; then
    [ -n "$Usage" ] && echog "$Usage"
	fatal "Use -h for help."
fi

# Sisyphus by default
MENV=SS
set_target_type $(basename `pwd`)

LISTNAMES=""
LISTARGS=""
OPTINDEX=1
for i in "$@"
do
	#   ,  ,    .
	if [ -f "$i" ]; then
		LISTNAMES="$LISTNAMES $i"
	else
		set_target_type ${i/-/} || LISTARGS="$LISTARGS $i"
	fi
done
# parse MENV
detect_target_env
[ -n "$VERBOSE" ] && echo "LISTNAMES=$LISTNAMES LISTARGS=$LISTARGS" || :
}

check_key()
{
	echog "Check access to SSH private key..."
	ssh-add -l || ssh-add $SSH_KEYFILE || return 1
	return 0
}

get_distr_version()
{
	echo $1 | sed -e "s|M\([0-9]\)\([0-9]\)|\1\.\2|g"  | sed -e "s|SS|Sisyphus|g" | sed -e "s|DD|Daedalus|g"
}

set_incoming()
{
	INCOMING="Sisyphus"
	test -z "$1" && return
	case "$1" in
		("DD")
			INCOMING="Daedalus"
			;;
		("SS")
			INCOMING="Sisyphus"
			;;
		*)
			if [ -n "$UPDATES" ] ; then
				INCOMING="updates/`get_distr_version $1`"
			else
				INCOMING="backports/`get_distr_version $1`"
			fi
			;;
	esac
}

add_changelog_helper()
{
	# don't work sometime?
	if ! tty -s && [ -z "$@" ] ; then 
		echo "skip changelog fixing without tty"
		return 1
	fi
	add_changelog -e "$@"
	R=$?
	[ -z "$EDITOR" ] && { echo "skip changelog editing without EDITOR var"; return 1 ; }
	# If changelog sucessfully added
	if [ "$R" = "0" ]; then
		shift
		for SPEC in "$@" ; do
			N=`grep -n '^%changelog' $SPEC | head -n 1 | sed s!:.*!!g`
			# +1 -- comment with date and packager name
			# +2 -- place for edit comments
			# +N works for mcedit and vi
			${EDITOR} +$(($N + 2)) $SPEC
		done
	fi
	return $R
}


load_mod()
{
. $ETERBUILDDIR/functions/$1
}
