# Exit on error:
set -e

METAPKG=$@

PKGLIST=`grep -v '#' /src/recipes-1.9.0/meta-$METAPKG.txt`

for PKG in $PKGLIST;
do {
    echo  0. Putting $PKG NAME in /root/meta-$METAPKG.txt...
    echo $PKG >> /root/meta-$METAPKG.txt

    echo  1. Creating pkgusr for $PKG...
    add_package_user $PKG $PKG 10000 20000 install 10000 20000

    echo  2. Getting recipe from /src/recipes...
    if [[ -f /src/recipes-1.9.0/$PKG ]]
        then cp /src/recipes-1.9.0/$PKG /src/$PKG/build-$PKG.sh
    elif [[ -f /src/recipes-1.8/$PKG ]]
        then cp /src/recipes-1.8/$PKG /src/$PKG/build-$PKG.sh
    elif [[ -f /src/recipes-1.7/$PKG ]]
        then cp /src/recipes-1.7/$PKG /src/$PKG/build-$PKG.sh
    elif [[ -f /src/recipes-1.6/$PKG ]]
        then cp /src/recipes-1.6/$PKG /src/$PKG/build-$PKG.sh
    elif [[ -f /src/recipes-1.5/$PKG ]]
        then cp /src/recipes-1.5/$PKG /src/$PKG/build-$PKG.sh
    fi

    echo  3. Removing build-me.sh...
    rm /src/$PKG/build-me.sh

    echo  4. Changing ownership of the recipe...
    chown $PKG:install /src/$PKG/build-$PKG.sh

    echo  5. Make the recipe executable...
    chmod u+x /src/$PKG/build-$PKG.sh

    echo  6. Building the recipe as pkgusr...
    su - $PKG -c ./build-$PKG.sh
};
done;

exit 0
