#!/bin/bash
if [ $# -gt 0 ]; then
    razor="$1"
else
    razor=`pwd`/../src/razor
fi
check_file()
{
    $razor list-files | grep -F -x "$1" > /dev/null
    if [ $? -ne 0 ]; then
	echo $1: Not in database >&2
	$razor list-files >&2
	exit 1
    fi
    if [ ! -e "$tmproot$1" ]; then 
	echo $1: Not in filesystem >&2
	ls -R "$tmproot" >&2
	exit 1
    fi
}
tmproot=`mktemp -dt` || exit 1
export RAZOR_ROOT="file:$tmproot"
scratchdir=`mktemp -dt` || exit 1
(cd base && ../tftpd $scratchdir/tftpd.pid $scratchdir/tftpd.port) || exit 1
cd $scratchdir
port=`cat tftpd.port`
pid=`cat tftpd.pid`
export YUM_URL="tftp://localhost:$port/"
if $razor init; then
    :
else
    kill $pid
    exit 1
fi
if $razor import-yum; then
    :
else
    kill $pid
    exit 1
fi
if $razor install zap; then
    :
else
    kill $pid
    exit 1
fi
check_file /usr/bin/zap
rm -rf "$scratchdir" "$tmproot"
kill $pid
exit 0
