#!/bin/bash

y2pmbuildconfdir=/etc/y2pmbuild

if [ -z "$tried_to_get_root" ]; then
        export tried_to_get_root="$LOGNAME"
        exec sudo -H "$0" "$@"
        exit 1
fi

if [ -z "$tried_to_get_root" ]; then
        echo "unable to find a sane LOGNAME"
        exit 1
fi

if [ -z "$BUILD_DIST" ]; then
        ver=""
        arch=`uname -m`

        case "$arch" in
                i?86) arch=i386;;
        esac

        case "$0" in
                *build-*) ver=${0#*build-};;
        esac

        if [ -e "$y2pmbuildconfdir/dists/$ver-$arch" ]; then
		export BUILD_DIST="$ver-$arch"
	elif [ -n "$ver" -a -e "$y2pmbuildconfdir/dists/$ver" ]; then
		export BUILD_DIST="$ver"
	fi
		
fi

export LOGNAME=$tried_to_get_root

exec y2pmbuild "$@"
