#!/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)
		if [ -r "$ETERBUILDETC/../AUTHORS" ] ; then
			if [ -n "$VERBOSE" ] ; then
				echo "Note: run from source tree, datadir=$ETERBUILDDIR, sysconfdir=$ETERBUILDETC"
			fi
		else
			ETERBUILDETC=/etc/eterbuild
			ETERBUILDDIR=/usr/share/eterbuild
		fi
	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_gear()
{
	local DIR=$1
	[ -z "$IGNOREGEAR" ] || return 1

	[ -n "$DIR" ] && [ -d "$DIR/.gear" ] && return 0
	[ -d ".gear" ] && return 0

	# compat
	[ -n "$DIR" ] && [ -r "$DIR/.gear-rules" ] && return 0
	[ -r ".gear-rules" ] && return 0

	# hack for support spec in some subdir
	[ -d "../.gear" ] && return 0
	return 1
}


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


load_mod()
{
	local i
	for i in $@ ; do
		. $ETERBUILDDIR/functions/$i
	done
}

load_mod config gettext alt spec


# 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
	# FIXME: echog breakes vars
	echo "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
		# echo is workaround for missed readlink
		LISTNAMES="$LISTNAMES $(readlink -f $i 2>/dev/null | echo $i)"
	else
		set_target_type ${i/-/} || LISTARGS="$LISTARGS $i"
	fi
done
# remove extra space before list
LISTNAMES=$(echo "$LISTNAMES" | sed -e "s|^ ||")
LISTARGS=$(echo "$LISTARGS" | sed -e "s|^ ||")
# 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
}

make_temp_file()
{
	# Workaround about broken mktemp
	if mktemp -V >/dev/null 2>/dev/null ; then
		mktemp || exit 1
	else
		mktemp /tmp/$1.XXXXXX || exit 1
	fi
}

