#!/bin/bash

# forces the use of english messages everywhere
export LC_ALL=C

prog=$(readlink -f "$0")
progname=$(basename $prog)
fbr_root=$(dirname $prog)

# architecture for which a FBR is to be built
fbr_arch=${FBR_ARCH:-x86}

# try to determine terminal type
if [ -n "$FBR_DUMBTTY" ]
then
	fbr_dumbtty="$FBR_DUMBTTY"
elif [ -t 2 ]
then
	fbr_dumbtty=no
else
	fbr_dumbtty=yes
fi

fbr_category=$(svn info "$fbr_root" 2>/dev/null | grep "^URL:" | sed -n 's,^.*/\([^/]\+\)/[^/]\+/src/packages/src/src/fbr$,\1,p')
fbr_branch=$(svn info "$fbr_root" 2>/dev/null | grep "^URL:" | sed -n 's,^.*/[^/]\+/\([^/]\+\)/src/packages/src/src/fbr$,\1,p')
[ -n "$fbr_category" ] || fbr_category=unknown
[ -n "$fbr_branch" ] || fbr_branch=custom

if [ -n "$FBR" ]
then
	fbr_workdir="$FBR"
elif [ -n "$FBR_BASEDIR" ]
then
	fbr_workdir=$FBR_BASEDIR/fbr-$fbr_category-$fbr_branch-$fbr_arch
else
	fbr_workdir=~/.fbr/fbr-$fbr_category-$fbr_branch-$fbr_arch
fi

if echo "$fbr_root" | grep -q " "
then
	echo "$(basename $0): FBR root directory $fbr_root may not contain spaces" >&2
	exit 1
fi
if echo "$fbr_workdir" | grep -q " "
then
	echo "$(basename $0): FBR working directory $fbr_workdir may not contain spaces" >&2
	exit 1
fi

# help requires special parameter parsing
if [ $# -eq 0 -o "$1" = "--help" -o "$1" = "-help" -o "$1" = "-h" -o "$1" = "-?" ]
then
	set -- help
fi

# include modules
for f in $(ls -1 {$fbr_root,$fbr_workdir}/include/*.inc 2>/dev/null | sort -n)
do
	. "$f"
done
