#!/bin/sh
################################################################################
# $Id: create_nfsroot 372 2006-12-15 19:42:22Z makia $
################################################################################
#  Copyright (C) 2004 The Regents of the University of California.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Makia Minich <makia@llnl.gov>
#  UCRL-CODE-155916
#  
#  This file is part of GeDI, a diskless image management tool.
#  For details, see <http://www.llnl.gov/linux/gedi/>.
#  
#  GeDI 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.
#  
#  GeDI 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 GeDI; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
################################################################################
#
# Create a diskless image based on an RPM list.

# Source function library.
. /etc/gedi/variables

usage()
{
   echo "USAGE: $0 [OPTION]... <rpmlist>"
   echo ""
   echo "Options:"
   echo "      -r DIR      Set the RPM Repostory. (Default=${RPMDIR})"
   echo "      -t          Test the rpmlist."
   echo "      -h          Print out this help statement."
   echo ""
   echo "The <rpmlist> must use the name \"rpmlist.<image>\"."
}

while getopts htr: opt ; do
   case "$opt" in
      r) RPMDIR=${OPTARG} ;;
      t) TEST=1 ;;
      h) usage ; exit 0 ;;
   esac
done

shift $(($OPTIND-1))

if [ -f $1 ] ; then
   RPMLIST=$1
elif [ -f $ETCDIR/$1 ] ; then
   RPMLIST=$ETCDIR/$1 
elif [ -f $ETCDIR/rpmlist.$1 ] ; then
   RPMLIST=$ETCDIR/rpmlist.$1 
else
   usage
   exit 1
fi

if [ ! -r "$RPMLIST" ] ; then
   usage
   exit 1
fi

for x in $(seq $(grep -c $ $RPMLIST)) ; do
   x=$(($x))
   rpm="$(sed -n ${x}p $RPMLIST)"
   [ "${rpm/*./}" != "rpm" ] && rpm="${rpm}*"

   found=0
   for f in $RPMDIR/$rpm ; do
      if [ -f $f ] ; then
         found=1
         RPMS=(${RPMS[@]} $(basename $f))
      fi
   done

   [ $found = 0 ] && list=(${list[@]} $rpm)
done

if [ ${#list[@]} -gt 0 ] ; then
   echo "Could not find the following RPMS in ${RPMDIR}:"
   for bad in ${list[@]} ; do
      echo "* $bad"
   done
   exit 1
fi

( echo "$RPMLIST" | grep -q '\.' ) &&
   TYPE=$(echo "$RPMLIST" | sed 's/^.*\/// ; s/.*list\.//') ||
   TYPE=other

[ "$TEST" = 1 ] && IMAGE=${IMAGE}/.${TYPE}_test ||
                   IMAGE=${IMAGE}/${TYPE}

if [ -d $IMAGE ] ; then
   echo "Removing old image ($IMAGE)"
   umount ${IMAGE}/proc > /dev/null 2>&1
   rm -rf $IMAGE
fi

if [ "$TEST" == "1" ] ; then
   mkdir -p $IMAGE/var/{lib/rpm,lock/rpm,tmp}
   pushd $RPMDIR >/dev/null && rpm --ignoresize --justdb --root $IMAGE --ignorearch -ivh ${RPMS[@]}
   popd >/dev/null
   echo "Done Testing"
   rm -rf $IMAGE
   exit 0
fi

echo "Creating the initial directory structure."
mkdir -p $IMAGE/{usr,tmp,boot,proc,root,var/lib/rpm,var/lock/rpm,etc,dev}
[ -f /etc/localtime ] && cp /etc/localtime $IMAGE/etc/localtime
mknod -m 0666 $IMAGE/dev/null c 1 3
cp ${LOCALDIR}/fstab ${IMAGE}/etc/fstab

echo "Mounting proc into the image directory"
mount -t proc none $IMAGE/proc

echo "Initializing the rpm database"
rpm --root $IMAGE --initdb

# Figure out what options were used.
RPM_OPTION=""

if [ "${FORCE}" = "YES" -o "${FORCE}" = "yes" ] ; then
   RPM_OPTION="${AID} --force --nodeps --replacepkgs --ignoresize"
else
   RPM_OPTION="${AID} --replacepkgs --ignoresize"
fi

echo "Installing rpms into $IMAGE"

# First, we'll install the setup rpm from RH, which contains some initial files.
setup_rpm=( $(grep "^\(coreutils\|setup-\)" ${RPMLIST}) )
for i in $(seq 0 $((${#setup_rpm[@]} - 1))) ; do
   [ "${setup_rpm[$i]/*./}" != "rpm" ] &&
      setup_rpm[$i]="${setup_rpm[$i]}*"
done

pushd $RPMDIR >/dev/null && rpm ${NOSIGNATURE} -iv --root ${IMAGE} \
                                --nodeps --noscripts ${RPM_OPTION} $setup_rpm
popd >/dev/null

pushd $RPMDIR >/dev/null && rpm ${NOSIGNATURE} -iv \
                                   --root $IMAGE  ${RPM_OPTION} ${RPMS[@]}
popd >/dev/null

[ -f $IMAGE/\(none\).err ] && rm $IMAGE/\(none\).err

[ -x $SCRIPTDIR/configure ] && $SCRIPTDIR/configure $TYPE
umount $IMAGE/proc

if ( grep -q "$IMAGE" $EXPORTS ) ; then
   echo -n "** Exporting" && exportfs -r 2> /dev/null && 
      echo_success || echo_failure
fi
