#!/bin/bash
#
# buildinstall
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

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

PRODUCTPATH="anaconda"

while [ $# -gt 0 ]; do
    case $1 in
	# general options affecting how we build things
	--nogr)
	    NOGRSTR="--nogr"
	    shift
	    ;;
	--debug)
	    DEBUGSTR="--debug"
	    shift
	    ;;

	# release information
	--version)
	    VERSION=$2
	    shift; shift
	    ;;
	--release)
	    RELEASESTR=$2
	    shift; shift
	    ;;
        --product)
	    PRODUCTSTR=$2
	    shift; shift
	    ;;
	--variant)
	    VARIANT=$2
	    shift; shift
	    ;;
	--bugurl)
	    BUGURL=$2
	    shift; shift
	    ;;

	--output)
	    OUTPUT=$2
	    shift; shift
	    ;;
	--updates)
	    UPDATES=$2
	    shift; shift
	    ;;

	*)
	    REPO=$1
	    shift
	    ;;
    esac
done

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

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

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

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

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

if [[ "$REPO" =~ ^/ ]]; then
    [ -n "$OUTPUT" ] || OUTPUT=$REPO
    REPO="file://$REPO"
fi

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

if [ ! -d "$OUTPUT" ]; then
    mkdir -p $OUTPUT
fi

yumconf=$(mktemp /tmp/yum.conf.XXXXXX)
cat > $yumconf <<EOF
[main]
keepcache=0
gpgcheck=0
plugins=0
reposdir=
tsflags=nodocs

[anacondarepo]
name=anaconda repo
baseurl=$REPO
enabled=1
EOF

BUILDINSTDIR=`mktemp -d ${TMPDIR:-/tmp}/buildinstall.tree.XXXXXX`
TREEDIR=`mktemp -d ${TMPDIR:-/tmp}/treedir.XXXXXX`

echo "Running buildinstall..."

pushd $BUILDINSTDIR
BUILDARCH=`repoquery -c $yumconf --qf "%{ARCH}\n" anaconda-runtime`
yumdownloader -c $yumconf anaconda-runtime || exit 1
rpm2cpio anaconda-runtime*rpm | cpio --quiet -iumd './usr*'
rm -f anaconda-runtime*rpm
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 [ -n "$UPDATES" -a -f $UPDATES/usr/lib/anaconda-runtime/$f ]; then
	cp -a $UPDATES/usr/lib/anaconda-runtime/$f* $BUILDINSTDIR/
    elif [ ! -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

if [ -n "$UPDATES" ]; then UPDATES="--updates $UPDATES"; fi

echo "Building images..."
$UPD_INSTROOT $DEBUGSTR $NOGRSTR --arch $BUILDARCH $UPDATES --mindir $TREEDIR/minstg2 --stg2dir $TREEDIR/stage2 $yumconf

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

# FIXME: need to update mk-images to take the yumconf
echo "Making images..."
$MK_IMAGES $DEBUGSTR $NOGRSTR --mindir $TREEDIR/minstg2 --stg2dir $TREEDIR/stage2 --arch $BUILDARCH --product "$PRODUCTSTR" --version $VERSION --bugurl "$BUGURL" --output $OUTPUT $yumconf

echo "Writing .discinfo file"
$MK_STAMP --releasestr="$RELEASESTR" --arch=$BUILDARCH --discNum="ALL"  --outfile=$OUTPUT/.discinfo

rm -rf $TREEDIR $BUILDINSTDIR
rm -f $yumconf
