#!/bin/bash

usage() {
    echo "Usage: buildinstall [--pkgorder <file>] [--version <version>] [--product <product>] [--release <comment>] [--prodpath <path>] [--discs <discstring>] <root>" >&2
    exit 1
}

PRODUCTPATH="anaconda"

while [ $# -gt 0 ]; do
    case $1 in
    --pkgorder)
        PKGORDER=$2
        PKGORDERSTR="--pkgorder $2"
        shift; shift
        ;;
    --comp)
        COMPNAME=$2
        shift; shift
        ;;
    --version)
        VERSION=$2
        shift; shift
        ;;
    --release)
        RELEASESTR=$2
        shift; shift
        ;;
        --product)
        PRODUCTSTR=$2
        shift; shift
        ;;
    --variant)
        VARIANT=$2
        shift; shift
        ;;
    --prodpath)
        PRODUCTPATH=$2
        shift; shift
        ;;
    --nogr)
        NOGRSTR="--nogr"
        shift
        ;;
    --debug)
        DEBUGSTR="--debug"
        shift
        ;;
    --buildinstdir)
        BUILDINSTDIR=$2
        shift; shift
        ;;
    --discs)
        DISCSTR=$2
        shift; shift
        ;;
    --bugurl)
        BUGURL=$2
        shift; shift
        ;;
    *)
        DIR=$1
        shift
        ;;
    esac
done

if [ -z "$PRODUCTSTR" ]; then
    usage
fi

if [ -z "$VERSION" ]; then
    usage
fi

if [ -z "$DIR" ]; then
    usage
fi

if [ -z "$RELEASESTR" ]; then
    usage
fi

if [ -z "$DISCSTR" ]; then
    DISCSTR="ALL"
fi

if [ -z "$BUGURL" ]; then
    BUGURL="your distribution provided bug reporting tool."
fi

p=`cd $DIR; /bin/pwd`
PKGDIR=$p/$PRODUCTPATH

if [ -z "$BUILDINSTDIR" ]; then
    BUILDINSTDIR=$p/buildinstall.tree.$$
    rm -rf $BUILDINSTDIR
    mkdir -p $BUILDINSTDIR
fi
TREEDIR=${TMPDIR:-/tmp}/treedir.$$

BUILDARCH=`rpm -qp --qf "%{ARCH}\n" $PKGDIR/anaconda-runtime-[0-9]* |head -n 1`

echo "Running buildinstall..."

echo "Checking for repository metadata..."
if ! [ -d $p/repodata ]; then
    echo "Repodata must exist in the tree!" >&2
    exit 1
fi

pushd $BUILDINSTDIR
rpm2cpio $PKGDIR/anaconda-runtime-[0-9]* | cpio --quiet -iumd './usr*'
popd

UPD_INSTROOT=./upd-instroot
MK_IMAGES=./mk-images
MK_TREEINFO=./maketreeinfo.py
MK_STAMP=./makestamp.py
BUILDINSTALL=./buildinstall

for f in $UPD_INSTROOT $MK_IMAGES $MK_STAMP $MK_TREEINFO $BUILDINSTALL; do
    if [ ! -f $f ]; then
    cp -a $BUILDINSTDIR/usr/lib/anaconda-runtime/$f* $BUILDINSTDIR/
    else
    cp -a $f* $BUILDINSTDIR/
    fi
done

UPD_INSTROOT=$BUILDINSTDIR/upd-instroot
MK_IMAGES=$BUILDINSTDIR/mk-images
MK_TREEINFO=$BUILDINSTDIR/maketreeinfo.py
MK_STAMP=$BUILDINSTDIR/makestamp.py
BUILDINSTALL=$BUILDINSTDIR/buildinstall

echo "Building images..."
$UPD_INSTROOT $DEBUGSTR $NOGRSTR $PKGDIR $TREEDIR/image-template $TREEDIR/instimage $p

if [ -n "$PKGORDER" ]; then
    echo "Getting package order..."
    echo "PYTHONPATH=$TREEDIR/instimage/usr/lib/anaconda $TREEDIR/instimage/usr/lib/anaconda-runtime/pkgorder $p $BUILDARCH $PRODUCTPATH"
    PYTHONPATH=$TREEDIR/instimage/usr/lib/anaconda $TREEDIR/instimage/usr/lib/anaconda-runtime/pkgorder $p $BUILDARCH $PRODUCTPATH > $PKGORDER
fi

echo "Writing .treeinfo file..."
$MK_TREEINFO --family="$PRODUCTSTR" ${VARIANT:+--variant="$VARIANT"} --version=$VERSION --arch=$BUILDARCH --packagedir=${PKGDIR#$p/} --outfile=$p/.treeinfo

echo "Making images..."
$MK_IMAGES $DEBUGSTR $NOGRSTR $PKGDIR $p $TREEDIR/image-template $TREEDIR/instimage $BUILDARCH "$PRODUCTSTR" $VERSION $PRODUCTPATH "$BUGURL"

echo "Writing .discinfo file"
$MK_STAMP --releasestr="$RELEASESTR" --arch=$BUILDARCH --discNum="$DISCSTR" --baseDir=$PRODUCTPATH/base --packagesDir=$PKGDIR --pixmapsDir=$PRODUCTPATH/pixmaps --outfile=$p/.discinfo

rm -rf $TREEDIR/image-template $TREEDIR/instimage $BUILDINSTDIR
