#!/bin/sh
#  Copyright 2010 Intel Corp.
# 
#  Authors: Anas Nashif <anas.nashif@intel.com>
# 
#  This program is free software; you can redistribute it and/or modify it
#  under the terms and conditions of the GNU Lesser General Public License,
#  version 2.1, as published by the Free Software Foundation.
# 
#  This program is distributed in the hope it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
#  more details.
# 
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
# 

if [ -e "$HOME/.pkg-tools" ]; then
    . $HOME/.pkg-tools
else
    echo "$HOME/.pkg-tools does not exist, taking default..."
fi

if [ -z "$LOCAL_BUILD_ROOT" ]; then
    LOCAL_BUILD_ROOT=/var/tmp/$USER-build-root
fi

TMPF=`mktemp`
osc repos > $TMPF
CNT=`cat $TMPF | wc -l`
if [ "$CNT" = "1" ]; then
    rep=$(cat $TMPF | awk ' {print $1 }')
    arch=$(cat $TMPF | awk ' {print $2 }')
else
    C=1
    echo "Multiple build targets found, please select one:"
    cat $TMPF | while read -r LINE
    do
        echo -n " $C) " 
        echo $LINE
        C=$(( C + 1 ))
    done
    read -e REPONUM
    if num=`expr "$REPONUM" : '[^0-9]*\([0-9]\+\)'`; then
        repoline=$(cat $TMPF | sed -n "${num}p " )
        rep=$(echo $repoline | awk ' {print $1 }')
        arch=$(echo $repoline | awk ' {print $2 }')
    fi
fi
rm $TMPF
export OSC_BUILD_ROOT=$LOCAL_BUILD_ROOT/build-root-$rep
osc build $rep $arch $@
