#!/bin/bash
# Script to create a complete system to build packages in a chroot
# environment.  This script ensures, that all packages given as
# parameter are installed. All other packges will be deleted.
#
# BUILD_ROOT  here the packages will be installed/deleted
# BUILD_RPMS  here we get our packages to install
# BUILD_ARCH  path of the architectures we try
#
# (c) 1997-2005 SuSE GmbH Nuernberg, Germany

#
# needed globals variables
#
export SRC
export YAST_IS_RUNNING="instsys"
PROC_IS_MOUNTED=

# should RPMs be installed with --force ?
USE_FORCE=false

BUILD_IS_RUNNING=$BUILD_ROOT/not-ready
TMPFILE=$BUILD_ROOT/tmpfile
RPMIDFMT="%{NAME}-%{VERSION}-%{RELEASE} %{BUILDHOST}-%{BUILDTIME}\n"

function set_base_second_files {
    # minimum set of rpms to build a basic buildroot:
    BASE_FILES="aaa_base bash db devs \
		diffutils gdbm glibc grep m4 ncurses pam perl \
		readline rpm"
    # additional rpm to be installed after the initial set is installed:
    SECOND_FILES="cpp file findutils gawk mktemp shadow timezone util-linux"

    case "$1" in
    7.?-*|8.0-*|sles7-*)
	    BASE_FILES="base textutils $BASE_FILES sh-utils fileutils aaa_dir"
	    ;;
    8.1-*|ul1-*|sles8-*)
	    BASE_FILES="fillup sed acl attr textutils $BASE_FILES sh-utils fileutils filesystem libxcrypt zlib"
	    BASE_FILES="$BASE_FILES pam-modules permissions"
	    SECOND_FILES="$SECOND_FILES bind9-utils"
	    ;;
    8.2-*)
	    BASE_FILES="fillup insserv sed libacl libattr attr acl $BASE_FILES filesystem libxcrypt zlib coreutils"
	    BASE_FILES="$BASE_FILES pam-modules permissions"
	    SECOND_FILES="$SECOND_FILES bind9-utils"
	    ;;
    9.0-*)	BASE_FILES="aaa_base acl attr bash bzip2 coreutils db devs
			diffutils filesystem fillup glibc grep insserv
			libacl libattr libgcc libxcrypt m4 ncurses pam
			permissions popt readline sed shadow tar zlib rpm"
	    SECOND_FILES="$SECOND_FILES bind-utils"
	    ;;
    9.1-*|9.2-*|sles9-*)
	    BASE_FILES="aaa_base acl attr bash bzip2 coreutils db devs
			diffutils filesystem fillup glibc grep insserv
			libacl libattr libgcc libxcrypt m4 ncurses pam
			permissions popt readline sed tar zlib rpm"
	    SECOND_FILES="cpp file findutils gawk mktemp pwdutils timezone util-linux bind-utils libselinux"
	    ;;
    *)
	    BASE_FILES="aaa_base acl attr bash bzip2 coreutils db devs
			diffutils filesystem fillup glibc grep insserv
			libacl libattr libgcc libnscd libselinux libxcrypt
			m4 ncurses pam permissions popt pwdutils readline
			sed tar zlib rpm"
	    SECOND_FILES="cpp file findutils gawk mktemp pwdutils timezone util-linux bind-utils libselinux"
	    ;;
    esac
}

#
# needed functions
#

function cleanup_and_exit {
    test -n "$PROC_IS_MOUNTED" && umount -n $BUILD_ROOT/proc 2>/dev/null
    exit ${1:-0}
}

function clean_build_root () {
        test -n "$BUILD_ROOT" && {
            umount -n $BUILD_ROOT/proc 2> /dev/null
            umount -n $BUILD_ROOT/dev/pts 2> /dev/null
            umount -n $BUILD_ROOT/mnt 2> /dev/null
            rm -rf $BUILD_ROOT/*
        }
}

function select_rpm_from_cache () {
    local p="${1%.rpm}"
    local r rp a ap app lr lapp
    ap=":$BUILD_ARCH:"
    lapp=""
    lr=
    for r in `grep "/$p" $2`; do
        rp=${r##*/}
        test "$r" != "$rp" || continue
        if test "${rp%.rpm}" = "$p" -o "${rp%-*-*.*.rpm}" = "$p"; then
	    test "$ap" = "::" && {
		echo $r
		return
	    }
	    if test "${rp%.rpm}" = "$p" ; then
		a=`rpm -qp --nodigest --nosignature --qf '%{ARCH}' $r`
	    else
		a="${rp%.rpm}"
		a="${a##*.}"
	    fi
	    test "$a" = noarch && {
		echo $r
		return
	    }
	    app="${ap#*:$a:}"
	    test "$ap" = "$app" && continue
	    test -z "$lr" -o ${#app} -gt ${#lapp} && {
		lapp="$app"
		lr="$r"
	    }
        fi
    done    
    test -n "$lr" && echo "$lr"
}

function set_src_pkg () {
    BASE=$1.rpm
    CACHE_FILE=$BUILD_ROOT/.srcfiles.cache
    test "$BUILD_RPMS" != "$(cat $CACHE_FILE.id 2>/dev/null)" && rm -f $CACHE_FILE
    for SRC in ${BUILD_RPMS//:/ } ; do
	test $SRC -nt $CACHE_FILE && rm -f $CACHE_FILE
    done
    test -f $CACHE_FILE || {
	    echo initializing $CACHE_FILE with find command...
	    find ${BUILD_RPMS//:/ } -follow -type f -name "*.rpm" -a ! -name "*src.rpm" | grep -v unsorted > $CACHE_FILE.new
	    mv $CACHE_FILE.new $CACHE_FILE
	    echo "$BUILD_RPMS" > $CACHE_FILE.id
    }
    SRC=`select_rpm_from_cache $BASE $CACHE_FILE`
    if test -z "$SRC" -o ! -e "$SRC" ; then
      case $BASE in
        pgp.rpm|pam-modules.rpm|aaa_dir.rpm|aaa_version.rpm|base.rpm|glibc-32bit.rpm|libxcrypt.rpm|gppshare.rpm|libstdc++.rpm|sed.rpm|tar.rpm|permissions.rpm|attr.rpm|acl.rpm|libattr.rpm|libacl.rpm)
          echo $BASE not found. Ignoring...
          SRC=""
        ;;
        *)
          rm -f $CACHE_FILE
          echo can not find $BASE... exit.
          cleanup_and_exit 1
        ;;
      esac
    fi
}

PREINSTALLED_PKGS=

function preinstall {
    if test -n "$1" ; then
        echo "preinstalling $1..."
        cd $BUILD_ROOT || cleanup_and_exit 1
        pac_unpack $BUILD_ROOT/.init_b_cache/rpms/$1.rpm
        test -e install/predoinst.sh && mv install/predoinst.sh .init_b_cache/scripts/$1.pre
        test -e install/doinst.sh && mv install/doinst.sh .init_b_cache/scripts/$1.post
        rm -rf install
        PREINSTALLED_PKGS="$PREINSTALLED_PKGS $1"
    fi
    if test -n "$2" ; then
        for PKG in $PREINSTALLED_PKGS ; do
            if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
                echo "running $PKG preinstall script"
                chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.pre" 0
                rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre"
            fi
            if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
                echo "running $PKG postinstall script"
                chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.post" 1
                rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post"
            fi
        done
        PREINSTALLED_PKGS=
    fi
}

function reorder {
    REORDER_HAVE=
    rm -rf $BUILD_ROOT/.reorder
    mkdir -p $BUILD_ROOT/.reorder/.db
    rpm --initdb --dbpath $BUILD_ROOT/.reorder/.db
    for PKG in "$@" ; do
        touch $BUILD_ROOT/.reorder/$PKG
        test -e $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG && continue
        test -f $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm && REORDER_HAVE="$REORDER_HAVE
$PKG.rpm"
    done
    if test -z "$REORDER_HAVE"; then
        rm -rf $BUILD_ROOT/.reorder
        echo "$@"
        return
    fi
    # manifest must be at least 96 bytes...
    echo "################################################################################################" "$REORDER_HAVE" > $BUILD_ROOT/.reorder/MANIFEST.rpm
    bash -c "cd $BUILD_ROOT/.init_b_cache/rpms && rpm -Uvv --dbpath $BUILD_ROOT/.reorder/.db --nosuggest --nodigest --nosignature --force --nodeps --test $BUILD_ROOT/.reorder/MANIFEST.rpm" 2>&1 | sed -n -e 's/-[^- ]*-[^- ]* / /' -e 's/^D:   install: \([^ ]*\) .*/\1/p' > $BUILD_ROOT/.reorder/.list
    rm -f $BUILD_ROOT/.reorder/MANIFEST.rpm
    REORDER_HAVE=
    for PKG in `cat $BUILD_ROOT/.reorder/.list`; do
        test -e $BUILD_ROOT/.reorder/$PKG || continue
        REORDER_HAVE="$REORDER_HAVE $PKG"
        rm $BUILD_ROOT/.reorder/$PKG
    done
    for PKG in "$@" ; do
        test -e $BUILD_ROOT/.reorder/$PKG || continue
        REORDER_HAVE="$REORDER_HAVE $PKG"
	REORDER_MISSED="$REORDER_MISSED $PKG"
        rm $BUILD_ROOT/.reorder/$PKG
    done
    echo $REORDER_HAVE
}

#
# now test if there was an incomplete run
#
if test -e $BUILD_IS_RUNNING ; then
    echo It seems that there was an incomplete setup of $BUILD_ROOT.
    echo To be sure, we will build it again completely...
    umount -n $BUILD_ROOT/proc 2> /dev/null
    umount -n $BUILD_ROOT/dev/pts 2> /dev/null
    umount -n $BUILD_ROOT/mnt 2> /dev/null
    echo "Your build system is broken!! Shall I execute"
    echo
    echo "    rm -rf $BUILD_ROOT"
    echo
    echo -n "[y/N] "
    read ANSWER
    test "$ANSWER" != y && {
      exit
    }
    clean_build_root
fi

#
# now store, that we start to build system
#
mkdir -p $BUILD_ROOT
touch $BUILD_IS_RUNNING

#
# now make sure that all the packages are installed.
#
rm -f $BUILD_ROOT/pkg-to-install* $BUILD_ROOT/pkg-not-to-install*
rm -rf $BUILD_ROOT/.init_b_cache
mkdir -p $BUILD_ROOT/.init_b_cache/scripts

for PKG in $* ; do
    BASE=`basename $PKG .rpm`
    test "$BASE" = "*" && continue
    case $BASE in
      -*)
        echo $BASE | sed -e"s:^-::" >> $BUILD_ROOT/pkg-not-to-install
        ;;
      *)
        echo $BASE >> $BUILD_ROOT/pkg-to-install
        ;;
    esac
done

for NOPKG in `cat $BUILD_ROOT/pkg-not-to-install 2> /dev/null` ; do
    grep -v -w -- ^$NOPKG $BUILD_ROOT/pkg-to-install > $BUILD_ROOT/pkg-to-install.t
    mv $BUILD_ROOT/pkg-to-install.t $BUILD_ROOT/pkg-to-install
done

touch $BUILD_ROOT/pkg-to-install.last $BUILD_ROOT/pkg-to-install.first
for BASE in `cat $BUILD_ROOT/pkg-to-install | sort -u` ; do
    case $BASE in
      lesstif* | \
      mmbase* | \
      mmcore* | \
      mmslib* | \
      pdflib)
        echo $BASE >> $BUILD_ROOT/pkg-to-install.last
      ;;
      *)
        echo $BASE >> $BUILD_ROOT/pkg-to-install.first
      ;;
    esac
done

cat $BUILD_ROOT/pkg-to-install.first $BUILD_ROOT/pkg-to-install.last \
    > $BUILD_ROOT/pkg-to-install

#
# make sure, that the "last" packages are installed at the end.  So
# delete them, if they are there.
#
if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
    for BASE in `cat $BUILD_ROOT/pkg-to-install.last` ; do
	chroot $BUILD_ROOT rpm --noscripts --nodeps -e $BASE > /dev/null 2>&1
    done
fi

rm -f $BUILD_ROOT/pkg-to-install.last $BUILD_ROOT/pkg-to-install.first

#
# find rpm of the rpm package, then guess BUILD_DIST if not set
#
mkdir -p $BUILD_ROOT/.init_b_cache/rpms
if ! test -L $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm ; then
    set_src_pkg rpm
    test -n "$SRC" && ln -s "$SRC" $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm
fi
if test -z "$BUILD_DIST" -a -L $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm ; then
    DSTR=$(rpm -qp --qf '%{DISTRIBUTION}\n' $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm | tr 'A-Z-' 'a-z_')
    DSTRA=${DSTR##*(}
    DSTRA=${DSTRA%%)*}
    DSTRA=${DSTRA//\/}
    DSTRA=${DSTRA/i[456]86/i386}
    case $DSTRA in
	i386|x86_64|ia64|ppc|ppc64|s390|s390x) ;;
	*) DSTR= ;;
    esac
    case $DSTR in
	"unitedlinux 1.0 "*) BUILD_DIST=ul1-$DSTRA ;;
	"suse sles_7 "*) BUILD_DIST=sles7-$DSTRA ;;
	"suse sles_8 "*) BUILD_DIST=sles8-$DSTRA ;;
	"suse sles_9 "*) BUILD_DIST=sles9-$DSTRA ;;
	"suse linux 8.1 "*) BUILD_DIST=8.1-$DSTRA ;;
	"suse linux 8.2 "*) BUILD_DIST=8.2-$DSTRA ;;
	"suse linux 9.0 "*) BUILD_DIST=9.0-$DSTRA ;;
	"suse linux 9.1 "*) BUILD_DIST=9.1-$DSTRA ;;
	"suse linux 9.2 "*) BUILD_DIST=9.2-$DSTRA ;;
    esac
fi

set_base_second_files $BUILD_DIST

PACKAGES_TO_INSTALL_FIRST="$BASE_FILES $SECOND_FILES"
PACKAGES_TO_INSTALL="`cat $BUILD_ROOT/pkg-to-install`"

#
# find rpms for all packages
#
for PKG in $PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL; do
    test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm && continue
    set_src_pkg $PKG
    test -z "$SRC" && continue
    ln -s "$SRC" $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm
    echo -n "."
done
echo

#
# now test, if there is already a build dir.
#
if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages ; then
    mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
    mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
    mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
    if [ ! -f $BUILD_ROOT/etc/HOSTNAME ] ; then
      /bin/hostname -f > $BUILD_ROOT/etc/HOSTNAME
    fi
    rpm_preinstalled=
    for PKG in $BASE_FILES ; do
	test ${PKG##*/} = rpm && rpm_preinstalled=true
	preinstall ${PKG##*/}
    done
    test -z "$rpm_preinstalled" && preinstall rpm
    preinstall '' true
    echo initializing rpm db...
    chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
fi

mkdir -p $BUILD_ROOT/proc
mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
PROC_IS_MOUNTED=true

test -e $BUILD_ROOT/etc/ld.so.conf || \
    cp $BUILD_ROOT/etc/ld.so.conf.in $BUILD_ROOT/etc/ld.so.conf
chroot $BUILD_ROOT /sbin/ldconfig 2> /dev/null

#
# get list and ids of already installed rpms
#
mkdir -p $BUILD_ROOT/.init_b_cache/alreadyinstalled
if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
    chroot $BUILD_ROOT rpm -qa --qf "%{NAME} $RPMIDFMT" | (
	while read pp ii; do
	    echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
	done
    )
fi

#
# reorder packages
#
echo -n 'reordering...'
REORDER_MISSED=
PACKAGES_TO_INSTALL_FIRST=`reorder $PACKAGES_TO_INSTALL_FIRST`
PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
echo 'done'
test -n "$REORDER_MISSED" && echo "WARNING: reorder missed$REORDER_MISSED"

#
# delete all packages we don't want
#
cp -a $BUILD_ROOT/.init_b_cache/alreadyinstalled $BUILD_ROOT/.init_b_cache/todelete
for PKG in $PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL ; do
    rm -f $BUILD_ROOT/.init_b_cache/todelete/$PKG
done
for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
    PKG=${PKG##*/}
    test "$PKG" = "*" && continue
    echo deleting `sed -e 's/ .*//' < $BUILD_ROOT/.init_b_cache/todelete/$PKG`
    chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
	grep -v "^r.*failed: No such file or directory"
done
rm -rf $BUILD_ROOT/.init_b_cache/todelete

rm -rf $BUILD_ROOT/installed-pkg
mkdir -p $BUILD_ROOT/installed-pkg

RPMCHECKOPTS=
test -x $BUILD_ROOT/usr/lib/rpm/rpmi && RPMCHECKOPTS="--nodigest --nosignature"

for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL ; do
    case $PKG in
      RUN_LDCONFIG)
        chroot $BUILD_ROOT /sbin/ldconfig 2>&1
        continue
      ;;
    esac
    test -f $BUILD_ROOT/installed-pkg/$PKG && continue
    test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm || continue

    PKGID=`rpm -qp --qf "$RPMIDFMT" $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm`

    if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
	if test "$PKGID" != "`cat $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG`" ; then
	    echo deleting unwanted `sed -e 's/ .*//' < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG`
	    chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
		grep -v "^r.*failed: No such file or directory"
	elif test "$VERIFY_BUILD_SYSTEM" = true ; then
	    chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
	    if grep ^missing $TMPFILE > /dev/null ; then
		echo deleting incomplete ${PKGID%% *}
		chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
		    grep -v "^r.*failed: No such file or directory"
	    else
		echo "keeping ${PKGID%% *}"
		echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
		continue
	    fi
	else
	    echo "keeping ${PKGID%% *}"
	    echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
	    continue
	fi
    fi
    export ADDITIONAL_PARAMS=
    if test "$USE_FORCE" = true ; then
        export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
    fi
    echo "installing ${PKGID%% *}"
    rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
    cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm $BUILD_ROOT/.init_b_cache/$PKG.rpm || cleanup_and_exit 1
    ( chroot $BUILD_ROOT rpm --nodeps -U --oldpackage $RPMCHECKOPTS \
		$ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
          touch $BUILD_ROOT/exit ) | \
              grep -v "^warning:.*saved as.*rpmorig$"
    rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
    test -e $BUILD_ROOT/exit && cleanup_and_exit 1
    echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
done

rm -f $BUILD_ROOT/pkg-to-install $BUILD_ROOT/pkg-not-to-install

cd $BUILD_ROOT || cleanup_and_exit 1

#
# setup /etc/mtab
#
rm -f $BUILD_ROOT/etc/mtab
cp /proc/mounts $BUILD_ROOT/etc/mtab
chmod 644 $BUILD_ROOT/etc/mtab

#
# to be sure, path is set correctly, we have to source /etc/profile before
# starting rpm.
#
# XXX
#rm -f $BUILD_ROOT/bin/rpm.sh
#cp $BUILD_LIBDIR/lib/rpm.sh $BUILD_ROOT/bin/rpm.sh
#chmod 755 $BUILD_ROOT/bin/rpm.sh
#test -f $BUILD_ROOT/bin/rpm -a ! -L $BUILD_ROOT/bin/rpm && \
#    mv $BUILD_ROOT/bin/rpm $BUILD_ROOT/bin/rpm.bin
#rm -f $BUILD_ROOT/bin/rpm
#ln -s rpm.sh $BUILD_ROOT/bin/rpm

#
# some packages use uname -r to decide which kernel is used to build for.
# this does not work in autobuild always.  Here is a wrapper script, that
# gets Version from kernel sources.
#
# XXX
#rm -f $BUILD_ROOT/bin/uname.sh
#cp -v $BUILD_LIBDIR/lib/uname.sh $BUILD_ROOT/bin/uname.sh
#chmod 755 $BUILD_ROOT/bin/uname.sh
#test -f $BUILD_ROOT/bin/uname -a ! -L $BUILD_ROOT/bin/uname && \
#    mv $BUILD_ROOT/bin/uname $BUILD_ROOT/bin/uname.bin
#rm -f $BUILD_ROOT/bin/uname
#ln -s uname.sh $BUILD_ROOT/bin/uname

#
# some distributions have a /etc/rpmrc or /etc/rpm/macros and some not.
# make sure, that it is setup correctly.
#
# XXX
#rm -f $BUILD_ROOT/etc/rpmrc
#if test -e $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME ; then
#    cp -v $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME $BUILD_ROOT/etc/rpmrc
#elif test -e $BUILD_LIBDIR/lib/rpmrc ; then
#    cp -v $BUILD_LIBDIR/lib/rpmrc $BUILD_ROOT/etc/rpmrc
#fi

# XXX
#rm -f $BUILD_ROOT/etc/rpm/macros $BUILD_ROOT/etc/rpm/suse_macros
#mkdir -p $BUILD_ROOT/etc/rpm
#if test -e $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME ; then
#    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/macros
#    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/suse_macros
#elif test -e $BUILD_LIBDIR/lib/macros ; then
#    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/macros
#    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/suse_macros
#fi

#
# make sure, that our nis is not present in the chroot system
#
test -e $BUILD_ROOT/etc/nsswitch.conf && {
    echo removing nis flags from $BUILD_ROOT/etc/nsswitch.conf...
    cat $BUILD_ROOT/etc/nsswitch.conf | sed -e"s:nis::g" > \
        $BUILD_ROOT/etc/nsswitch.conf.tmp
    mv $BUILD_ROOT/etc/nsswitch.conf.tmp $BUILD_ROOT/etc/nsswitch.conf
}

#
# creating some default directories
for DIR in /usr/share/doc/packages \
           /usr/X11R6/include/X11/pixmaps \
           /usr/X11R6/include/X11/bitmaps ; do
    mkdir -p $BUILD_ROOT/$DIR
done

for FILE in /var/run/utmp /var/log/wtmp ; do
    touch $BUILD_ROOT/$FILE
done

echo now finalizing build dir...
CHROOT_RETURN="`chroot $BUILD_ROOT /sbin/ldconfig 2>&1`"
case "$CHROOT_RETURN" in
    *warning:*)
      chroot $BUILD_ROOT /sbin/ldconfig
      echo
      echo chroot $BUILD_ROOT /sbin/ldconfig
      echo
      echo "$CHROOT_RETURN"
      echo
      echo "Problem with ldconfig.  It's better to reinit the build system..."
      echo
      cleanup_and_exit 1
    ;;
esac
chroot $BUILD_ROOT /usr/sbin/Check

mkdir -p $BUILD_ROOT/var/adm/packages
touch $BUILD_ROOT/var/adm/packages
if grep norestarts $BUILD_ROOT/sbin/SuSEconfig > /dev/null ; then
    chroot $BUILD_ROOT /sbin/SuSEconfig --norestarts --force
else
    chroot $BUILD_ROOT /sbin/SuSEconfig --force
fi

if test -x $BUILD_ROOT/usr/X11R6/bin/switch2mesasoft ; then
  chroot $BUILD_ROOT /usr/X11R6/bin/switch2mesasoft
fi

for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
    test -x $BUILD_ROOT/$PROG && \
        chroot $BUILD_ROOT bash -c ". /etc/profile ; $PROG"
done

rm -f $BUILD_IS_RUNNING

rm -f $TMPFILE
cleanup_and_exit 0
