#!/bin/bash
#
# upd-instroot
#
# Copyright (C) 2007, 2008 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/>.
#

ORIGDIR=`pwd`
DEBUG=""
NEEDGR=yes
ARCH=`uname -m`

while [ $# -gt 0 ]; do
    case $1 in
	--nogr)
	    NEEDGR=""
	    shift
	    ;;
	--debug)
	    DEBUG="--debug"
	    shift
	    ;;

	--arch)
	    ARCH=$2
	    shift; shift
	    ;;

	--mindir)
	    DEST=$2
	    shift; shift
	    ;;

	--stg2dir)
	    DESTGR=$2
	    shift; shift
	    ;;

	# a filesystem tree to use as updates.  could be the output
	# of 'make install' from anaconda...
	--updates)
	    UPDATES=$2
	    shift; shift
	    ;;

	*)
	    yumconf=$1
	    shift
	    ;;
    esac
done

if [ -z "$yumconf" ]; then
	echo "upd-instroot: updates instimage from a Red Hat RPMS directory"
	echo "usage:        $0 [--nogr] [--debug] [--arch arch] [--mindir mindir] [--stg2dir stg2dir] [yumconf]"
	exit 1
fi

if [ $ARCH = x86_64 -o $ARCH = s390x -o $ARCH = ppc64 ]; then
    LIBDIR=lib64
else
    LIBDIR=lib
fi

if [ -z "$DEST" ]; then
    DEST=`mktemp -d ${TMPDIR:-/tmp}/dest.XXXXXX`
fi

if [ -z "$DESTGR" ]; then
    DESTGR=`mktemp -d ${TMPDIR:-/tmp}/destgr.XXXXXX`
fi

if [ ! -f $yumconf ]; then
    echo "Unable to find yum repo information!"
    exit 1
fi

. $(dirname $0)/buildinstall.functions

expandPackageSet() {
    YUMCONF=$1
    YUMDIR=$2
    RPMS=$3
    PKGDEST=$4
    KEEPFILES=$5

    [ -d $PKGDEST ] || die "ERROR: directory missing: $PKGDEST"
    if [ -z "$DEBUG" ]; then outlvl="--quiet" ; else outlvl="--verbose"; fi
    yum $outlvl -c $YUMCONF -y --installroot=$YUMDIR install $RPMS 2>&1 || die "ERROR: could not install packages"

    if [ -n "$UPDATES" ]; then
	(cd $UPDATES; find) | (cd $UPDATES ; /bin/cpio --quiet -pmdu $YUMDIR)
    fi

    # figure out the theme to keep
    if [ -f $YUMDIR/etc/gtk-2.0/gtkrc ]; then
	gtktheme=$(grep "gtk-theme-name" $YUMDIR/etc/gtk-2.0/gtkrc | awk {'print $3;'} | sed -e 's/"//g')
	echo "usr/share/themes/$gtktheme" >> $KEEPFILES
	# find gtk engine needed
	for engine in `grep engine $YUMDIR/usr/share/themes/$gtktheme/gtk-2.0/gtkrc | awk {'print $2;'} | sed -e 's/"//g' | sort -u` ; do
	    echo "usr/$LIBDIR/gtk-2.0/*/engines/*$engine*" >> $KEEPFILES
	done
	
	theme=$(grep "gtk-icon-theme-name" $YUMDIR/etc/gtk-2.0/gtkrc | awk {'print $3;'} | sed -e 's/"//g')
	while [ -n "$theme" ]; do 
	    echo "usr/share/icons/$theme" >> $KEEPFILES
	    theme=$(grep Inherits $YUMDIR/usr/share/icons/$theme/index.theme | cut -d = -f 2)
	done
    fi

    echo `date` "Installing files"
    pushd $YUMDIR >/dev/null
    cat $KEEPFILES | while read spec ; do
      #Pull off path
      path=`echo "$spec" | sed 's,\([^[*\?]*\)/.*,\1,'`
      for filespec in `find ./$path -path "./$spec" 2> /dev/null` ; do
	if [ ! -e $filespec ]; then
	    continue
	elif [ ! -d $filespec ]; then
	    instFile $filespec $PKGDEST
	else
	    for i in `find $filespec -type f 2> /dev/null` ; do instFile $i $PKGDEST ; done
	    for i in `find $filespec -type l 2> /dev/null` ; do instFile $i $PKGDEST ; done
	    for d in `find $filespec -type d 2> /dev/null` ; do instDir $d $PKGDEST ; done
	fi
      done
    done
    popd >/dev/null
}

die () {
    echo "$@"
    echo "Aborting instroot creation..."
    exit 1
}

PACKAGES="glibc-common setup python newt slang libselinux
	 libsepol libtermcap zlib bash e2fsprogs util-linux-ng raidtools popt
	 procps rpm rpm-libs Xconfigurator anaconda tzdata python-libs
	 anaconda-runtime kudzu hwdata bzip2 beecrypt mdadm libsemanage
	 bzip2-libs dosfstools pciutils reiserfs-utils parted sed
	 busybox-anaconda rpm-python booty hdparm lvm2 beecrypt
	 rhpl pyxf86config libxml2 glib2 sqlite neon libselinux-python
	 elfutils-libelf krb5-libs convertdb1 jfsutils audit-libs pam
	 xfsprogs xfsdump dmapi libacl libattr attr acl python-urlgrabber
	 policy policycoreutils pyparted xorg-x11 selinux-policy-targeted
	 libstdc++ expat libgcc readline ncurses yum python-sqlite
	 python-elementtree pykickstart e2fsprogs-libs iscsi-initiator-utils
	 db4 rhpxl xorg-x11-server-Xorg libuser-python libuser system-config-date
	 yum-metadata-parser gfs2-utils libvolume_id nash yum-fedorakmod
	 libdhcp libnl libdhcp6client libdhcp4client newt-python nfs-utils
	 device-mapper device-mapper-libs dmraid keyutils-libs libsemanage-python
	 python-pyblock mkinitrd libbdevid libbdevid-python nss nspr pcre
	 cryptsetup-luks libgcrypt libgpg-error dbus dbus-python hal
	 cracklib-python module-init-tools cracklib-dicts"

if [ $ARCH = i386 ]; then
    PACKAGES="$PACKAGES glibc.i386 openssl.i386"
else
    PACKAGES="$PACKAGES glibc openssl"
fi

if [ $ARCH = i386 -o $ARCH = x86_64 ]; then
    PACKAGES="$PACKAGES pcmciautils grub"
fi

if [ $ARCH = i386 -o $ARCH = x86_64 -o $ARCH = ia64 ]; then
    PACKAGES="$PACKAGES dmidecode efibootmgr"
fi

if [ $ARCH = ia64 ]; then
    # XXX this needs to switch to grub at some point...
    PACKAGES="$PACKAGES elilo"
fi

if [ $ARCH = s390 -o $ARCH = s390x ]; then
    PACKAGES="$PACKAGES s390utils binutils libgcc tcp_wrappers
              sed net-tools openssh openssh-server coreutils
              login initscripts bash portmap pam 
              login mount grep modutils gawk
              strace xorg-x11-xauth xorg-x11-libs"
fi

if [ $ARCH = ppc -o $ARCH = ppc64 ]; then
    PACKAGES="$PACKAGES pcmciautils pdisk yaboot hfsutils"
fi

if [ $ARCH = sparc -o $ARCH = sparc64 ]; then
    PACKAGES="$PACKAGES silo"
fi

# xpm is missing

# Some packages are listed twice, but that's okay
#
# The packages in this list are needed for the graphical installer to run
# 
# Extra stuff we want for rescue mode should go in PACKAGESRESCUE
#
PACKAGESGR="anaconda libpng gtk2 libglade2 fonts-ISO8859-2 fonts-ISO8859-9
           e2fsprogs coreutils glibc-common
           popt specspo util-linux-ng rpm rpm-libs tzdata
           procps bash cpio gpm pygtk2-libglade pygobject2
           Xconfigurator pygtk2 gdk-pixbuf libpixman
           reiserfs-utils atk pango freetype gnome-python2-canvas cairo
           libgnomecanvas libart_lgpl libgcc libacl libattr libstdc++ expat
           anaconda-images anaconda-help ttfonts-ko taipeifonts rhpl
           system-config-keyboard Xft fontconfig gtk2-engines gnome-themes
           gtkhtml2 gnome-python2-gtkhtml2 gail libthai
           ttfonts-ja ttfonts-zh_TW bitmap-fonts-cjk urw-fonts fonts-arabic
           comps-extras convertdb1 fonts-bengali
           vnc-server vnc-libs libjpeg tcp_wrappers system-config-date
           xfsprogs xfsdump dmapi attr acl policy policycoreutils
           selinux-policy-targeted ttfonts-zh_CN redhat-artwork
           xorg-x11-libs xorg-x11-ISO8859-2-75dpi-fonts echo-icon-theme
           xorg-x11-ISO8859-9-75dpi-fonts xorg-x11-100dpi-fonts
           xorg-x11-xfs xorg-x11 xorg-x11-KOI8-R xorg-x11-KOI8-R-75dpi-fonts
           xorg-x11-base-fonts xorg-x11-ISO8859-15-75dpi-fonts
           fonts-xorg-ISO8859-2-75dpi fonts-xorg-ISO8859-15-75dpi
           fonts-xorg-ISO8859-9-75dpi fonts-xorg-100dpi
           fonts-xorg-KOI8-R fonts-xorg-KOI8-R-75dpi fonts-xorg-base
           xorg-x11-base xorg-x11-ISO8859-15-75dpi system-logos
           xorg-x11-libs-data xorg-x11-xfs prelink redhat-logos fedora-logos
           ttfonts-ta ttfonts-bn ttfonts-gu ttfonts-hi ttfonts-pa
           fonts-chinese fonts-korean fonts-indic fonts-sinhala
           fonts-malayalam fonts-oriya fonts-kannada fonts-telugu cjkunifonts-ukai
           fonts-gujarati fonts-hindi fonts-punjabi fonts-tamil synaptics
           firstboot pycairo pirut dejavu-fonts libxcb VLGothic-fonts
           dmraid python-pyblock libbdevid libbdevid-python zenity udev dbus dbus-python hal"

#
# stuff ONLY included for rescue mode
#
# these packages are combined with the PACKAGES and PACKAGESGR for big stage 2
#
PACKAGESRESCUE="bzip2 bzip2-libs dump libidn
                findutils ftp gzip iputils joe krb5-libs less man
                modutils mtools mt-st mtr net-tools open smartmontools
                openssh openssh-clients pciutils rsh traceroute tar rsync
                device-mapper device-mapper-libs dmraid ntfsprogs samba-client
                firstaidkit-plugin-all"

if [ $ARCH = i386 -o $ARCH = x86_64 ]; then
    PACKAGESRESCUE="$PACKAGESRESCUE gpart grub"
fi

# turn off options we dont want, ie. set package list to something harmless
if [ -z "$NEEDGR" ]; then
    PACKAGESGR="bash"
    PACKAGESRESCUE="bash"
fi

#
# add bootloader for particular arch
#
if [ $ARCH = sparc ]; then
    PACKAGESGR="$PACKAGESGR tilo silo"
fi

if [ $ARCH = i386 -o $ARCH = x86_64 ]; then
    PACKAGESGR="$PACKAGESGR syslinux memtest86+ grub" 
fi

if [ $ARCH = alpha ]; then
    PACKAGESGR="$PACKAGESGR aboot"
fi

if [ $ARCH = ia64 ]; then
    # XXX this needs to switch to grub at some point...
    PACKAGESGR="$PACKAGESGR elilo"
fi

if [ $ARCH = ppc ]; then
    PACKAGESGR="$PACKAGESGR fbset yaboot ppc64-utils"
fi

# dogtail stuff...
PACKAGESGR="$PACKAGESGR gail at-spi libbonobo ORBit2 pyspi GConf2 dogtail libXevie libXtst gnome-python2-gconf"

# modular xorg...
XORGLIBS="libICE libSM libX11 libXcursor libXext libXfixes libXft libXi libxkbfile libXmu libXpm libXrandr libXrender libXt libXxf86misc libXaw liblbxutil libXfont libfontenc libXau libXdmcp libXfont libXinerama"
XORGFONTS="xorg-x11-fonts-75dpi xorg-x11-fonts-base xorg-x11-fonts-cyrillic xorg-x11-fonts-ISO8859-15-75dpi xorg-x11-fonts-ISO8859-1-75dpi xorg-x11-fonts-ISO8859-2-75dpi xorg-x11-fonts-ISO8859-9-75dpi xorg-x11-fonts-misc xorg-x11-fonts-ethiopic"
XORG="$XORGLIBS xorg-x11-drivers $XORGFONTS xorg-x11-server-Xorg xorg-x11-xauth xorg-x11-font-utils xorg-x11-xkb-utils xorg-x11-xkbdata xorg-x11-server-utils xkeyboard-config"

PACKAGESGR="$PACKAGESGR $XORG"


#
# KEEPFILE is all files to keep from the packages in PACKAGES
#
# This is what is present in http, ftp, and hard drive installs, and is
# supposed to be a small image because these methods keep it in RAM.
#
# It also is the base on which the other install images build on top of,
# so if something is needed by all images it should go here
#
KEEPFILE=${TMPDIR:-/tmp}/keepfile.$$

cat > $KEEPFILE <<EOF
boot/efi/EFI/redhat/elilo.efi
boot/efi/EFI/redhat/grub.efi
bin/bash
bin/dbus-daemon
bin/fdisk*
bin/sed
bin/mount
bin/umount
etc/dbus-1
etc/group
etc/hal
etc/passwd
etc/iscsid.conf
etc/mke2fs.conf
etc/nsswitch.conf
etc/pcmcia
etc/PolicyKit
etc/protocols
etc/services
etc/selinux/targeted
etc/shells
etc/udev
etc/yum/pluginconf.d/fedorakmod.conf
lib/terminfo
$LIBDIR/libnss_dns*
$LIBDIR/libnss_files*
$LIBDIR/dbus-1
$LIBDIR/libfreebl3.so
$LIBDIR/libnss3.so
$LIBDIR/libnssckbi.so
$LIBDIR/libnsspem.so
$LIBDIR/libsmime3.so
$LIBDIR/libsoftokn3.so
$LIBDIR/libssl3.so
sbin/badblocks
sbin/busybox.anaconda
sbin/clock
sbin/cryptsetup
sbin/debugfs
sbin/dosfslabel
sbin/dumpe2fs
sbin/e2fsck
sbin/e2fsadm
sbin/e2label
sbin/fsck.ext2
sbin/fsck.ext3
sbin/fsck.jfs
sbin/fsck.xfs
sbin/fdisk
sbin/hdparm
sbin/hwclock
sbin/jfs_tune
sbin/ldconfig
sbin/lspci
sbin/lvm*
sbin/mdadm
sbin/mkdosfs
sbin/mke2fs
sbin/mkfs.ext2
sbin/mkfs.ext3
sbin/mkfs.gfs2
sbin/mkfs.jfs
sbin/mkfs.xfs
sbin/mkfs.msdos
sbin/mkfs.vfat
sbin/mkreiserfs
sbin/mkraid
sbin/mkswap
sbin/mount.nfs*
sbin/parted
sbin/pcmcia-socket-startup
sbin/pdisk
sbin/probe
sbin/reiserfstune
sbin/resize2fs
sbin/tune2fs
sbin/umount.nfs*
sbin/xfsdump
sbin/xfsrestore
sbin/xfs_repair
usr/sbin/xfs_admin
usr/sbin/xfs_db
usr/sbin/xfs_check
usr/sbin/xfs_copy
sbin/*gfs*
sbin/iscsid
sbin/iscsiadm
sbin/iscsistart
sbin/udev*
usr/bin/lshal
usr/bin/python
usr/bin/python?.?
usr/bin/hmount
usr/bin/humount
usr/bin/hcopy
usr/bin/hattrib
usr/bin/hformat
usr/bin/udevinfo
usr/lib/anaconda-runtime
usr/lib/anaconda
usr/lib/anaconda/installclasses
usr/lib/anaconda/textw
usr/lib/booty
usr/$LIBDIR/hal
usr/$LIBDIR/python?.?
usr/$LIBDIR/python?.?/email
usr/$LIBDIR/python?.?/xml/etree/
usr/$LIBDIR/python?.?/site-packages/_dbus*
usr/$LIBDIR/python?.?/site-packages/*kudzu*
usr/$LIBDIR/python?.?/site-packages/bdevid.so
usr/$LIBDIR/python?.?/site-packages/rpmmodule.so
usr/$LIBDIR/python?.?/site-packages/partedmodule.so
usr/$LIBDIR/python?.?/site-packages/ixf86configmodule.so
usr/$LIBDIR/python?.?/site-packages/xf86config.py
usr/$LIBDIR/python?.?/site-packages/libusermodule.so
usr/$LIBDIR/python?.?/site-packages/*sqlite*
usr/$LIBDIR/python?.?/site-packages/rhpl
usr/$LIBDIR/python?.?/site-packages/cElementTree.so
usr/$LIBDIR/python?.?/site-packages/block
usr/$LIBDIR/python?.?/site-packages/*.pth
usr/$LIBDIR/python?.?/site-packages/*selinux*
usr/lib/locale
usr/lib/python?.?/site-packages/dbus
usr/lib/python?.?/site-packages/dbus_bindings.py*
usr/lib/python?.?/site-packages/elementtree
usr/lib/python?.?/site-packages/iniparse
usr/lib/python?.?/site-packages/rpmUtils
usr/lib/python?.?/site-packages/urlgrabber
usr/lib/python?.?/site-packages/yum
usr/lib/python?.?/site-packages/repomd
usr/lib/python?.?/site-packages/pirut
usr/lib/python?.?/site-packages/pykickstart
usr/lib/python?.?/site-packages/rhpxl
usr/lib/python?.?/site-packages/cracklibmodule.so
usr/lib/rpm/macros
usr/lib/rpm/rpmpopt
usr/lib/rpm/rpmrc
boot/efika.forth
usr/lib/yaboot
usr/lib/yaboot
usr/lib/yaboot
usr/lib/yum-plugins/fedorakmod.py*
usr/$LIBDIR/libuser/*
usr/libexec/convertdb1
usr/libexec/hal*
usr/libexec/polkit*
usr/share/cracklib
usr/share/dbus-1
usr/share/hal
usr/share/PolicyKit
usr/share/locale/*/LC_MESSAGES/anaconda.mo
usr/share/locale/*/LC_MESSAGES/system-config-date.mo
usr/share/locale/locale.alias
usr/sbin/anaconda
usr/sbin/ddcprobe
usr/sbin/dmidecode
usr/sbin/efibootmgr
usr/sbin/hald
usr/sbin/mkofboot
usr/sbin/ofpath
usr/sbin/ybin
usr/sbin/load_policy
usr/sbin/semodule
usr/sbin/genhomedircon
sbin/setfiles
usr/sbin/lvm
usr/sbin/gptsync
usr/sbin/showpart
usr/share/anaconda/anaconda.conf
usr/share/system-config-date/zonetab.py*
usr/share/terminfo/l/linux
usr/share/terminfo/v/vt100
usr/share/terminfo/v/vt100-nav
usr/share/hwdata/pci.ids
usr/share/hwdata/videoaliases
usr/share/hwdata/videoaliases
usr/share/hwdata/videodrivers
usr/share/hwdata/MonitorsDB
usr/share/xorg/extramodes
usr/share/xorg/vesamodes
usr/share/zoneinfo/zone.tab
var/lib/dbus
var/lib/hal
var/lib/PolicyKit*
var/lib/misc/PolicyKit*
var/run/dbus
var/run/PolicyKit
var/cache/hald
EOF

if [ $ARCH = s390 -o $ARCH = s390x ]; then
    cat >> $KEEPFILE <<EOF
usr/share/terminfo/a/ansi
usr/share/terminfo/d/dumb
usr/share/terminfo/k/kterm
usr/share/terminfo/s/screen
usr/share/terminfo/v/vt102
usr/share/terminfo/x/xterm
usr/share/terminfo/x/xterm-color
usr/bin/strace
usr/bin/ldd
usr/sbin/chroot
usr/sbin/sshd
usr/sbin/glibc_post_upgrade
sbin/cmsfslst
sbin/cmsfscat
sbin/busybox.anaconda
sbin/ifconfig
sbin/route
sbin/portmap
sbin/fdasd
sbin/dasdfmt
sbin/swapon
sbin/swapoff
sbin/mkswap
bin/bash
bin/dd
bin/gawk
bin/mount
bin/umount
bin/mknod
bin/sleep
bin/login
bin/cat
bin/chmod
bin/sort
lib/modules/ibm
etc/pam.d/sshd
etc/pam.d/other
etc/security/limits.conf
etc/security/pam_env.conf
lib/security
$LIBDIR/security/pam_*
usr/bin/xauth
EOF
fi

#
# KEEPFILEGR is all files to keep from the packages in PACKAGESGR
#
# This defines the files in addition to KEEPFILE that make up the install
# images for NFS and CD/DVD based installs.  This image is not loaded into
# memory so it can be considerably larger.
#
# The rescue mode of anaconda uses these files as well as those defined
# by KEEPFILERESCUE below.  The total size of this image should be
# under the size of the miniature CD used for the rescue CD (around 68MB).
#
KEEPFILEGR=${TMPDIR:-/tmp}/keepfilegr.$$
cp $KEEPFILE $KEEPFILEGR
cat >> $KEEPFILEGR <<EOF
boot/memtest86*
bin/bash
bin/cat
bin/chmod
bin/cp
bin/cpio
bin/dd
bin/df
bin/du
bin/ln
bin/ls
bin/mkdir
bin/mv
bin/ps
bin/rm
bin/rpm
bin/touch
boot/*.b
boot/bootlx
etc/fb.modes
etc/gtk-2.0/gtkrc*
etc/im_palette.pal
etc/imrc
etc/man.config
etc/pango
etc/fonts
etc/prelink.conf
etc/rpm/macros.prelink
$LIBDIR/bdevid
sbin/debugfs
sbin/e2fsck
sbin/e2label
sbin/reiserfsck
sbin/reiserfstune
sbin/fsck
sbin/fsck.ext2
sbin/fsck.ext3
sbin/fsck.jfs
sbin/fsck.reiserfs
sbin/parted
sbin/silo
sbin/tune2fs
usr/bin/Xorg
usr/bin/setxkbmap
usr/bin/xkbcomp
usr/share/X11/XKeysymDB
usr/share/X11/fonts/misc/6x13*
usr/share/X11/fonts/misc/fonts*
usr/share/X11/fonts/misc/cursor*
usr/share/X11/fonts/misc/olcursor*
usr/share/X11/fonts/TTF/GohaTibebZemen.ttf
usr/share/X11/locale
usr/share/X11/rgb*
usr/share/X11/xkb
usr/$LIBDIR/xserver/SecurityPolicy
usr/$LIBDIR/xorg/modules
usr/bin/chattr*
usr/bin/gdialog
usr/bin/gtk-query*
usr/bin/gdk-pixbuf-query-loaders*
usr/bin/gtk-update-icon-cache*
usr/bin/head
usr/bin/lsattr*
usr/bin/maketilo
usr/bin/mini-wm
usr/bin/pango*
usr/bin/syslinux
usr/bin/mkzimage
usr/bin/tac
usr/bin/tail
usr/bin/tilo
usr/bin/uniq
usr/bin/zenity
usr/bin/Xvnc
usr/bin/vncconfig
usr/bin/vncpasswd
usr/bin/reduce-font
usr/lib/anaconda/iw
usr/$LIBDIR/gconv
usr/$LIBDIR/gdk-pixbuf/loaders/*xpm*
usr/$LIBDIR/gdk-pixbuf/loaders/*png*
usr/$LIBDIR/gdk-pixbuf/loaders/*la*
usr/$LIBDIR/gtk-2.0/*/engines/libclearlooks.so
usr/$LIBDIR/gtk-2.0/*/loaders/*xpm*
usr/$LIBDIR/gtk-2.0/*/loaders/*png*
usr/$LIBDIR/gtk-2.0/*/loaders/*la*
usr/$LIBDIR/gtk-2.0/immodules
usr/$LIBDIR/pango
usr/$LIBDIR/python?.?/site-packages/cairo
usr/$LIBDIR/python?.?/site-packages/gtk*/gtk
usr/$LIBDIR/python?.?/site-packages/gtk*
usr/$LIBDIR/python?.?/site-packages/firstboot
usr/$LIBDIR/rpm/rpmpopt
usr/lib/syslinux
usr/lib/kernel-wrapper
boot/efika.forth
usr/sbin/addRamDisk
usr/sbin/wrapper
usr/sbin/chroot
usr/sbin/ddcprobe
usr/sbin/fbset
usr/sbin/prelink
usr/sbin/smartctl
usr/share/anaconda
usr/share/fonts/dejavu/DejaVuSans.ttf
usr/share/fonts/dejavu/DejaVuSans-Bold.ttf
usr/share/fonts/dejavu/DejaVuSansMono.ttf
usr/share/fonts/baekmuk-ttf-dotum/dotum.ttf
usr/share/fonts/VLGothic/VL-Gothic-Regular.ttf
usr/share/fonts/cjkunifonts-uming/uming*.ttf
usr/share/fonts/kacst/KacstFarsi.ttf
usr/share/fonts/kacst/KacstQura.ttf
usr/share/fonts/*/lohit_ta.ttf
usr/share/fonts/*/lohit_gu.ttf
usr/share/fonts/*/lohit_bn.ttf
usr/share/fonts/*/lohit_hi.ttf
usr/share/fonts/*/lohit_pa.ttf
usr/share/fonts/*/lohit_or.ttf
usr/share/fonts/*/lohit_ml.ttf
usr/share/fonts/*/lohit_kn.ttf
usr/share/fonts/*/lohit_te.ttf
usr/share/fonts/*/lklug.ttf
usr/share/locale/*/LC_MESSAGES/anaconda.mo
usr/share/locale/*/LC_MESSAGES/system-config-date.mo
usr/share/locale/*/LC_MESSAGES/redhat-dist.mo
usr/share/locale/*/LC_MESSAGES/system-config-keyboard.mo
usr/share/locale/*/LC_MESSAGES/gtk20.mo
usr/share/locale/*/LC_MESSAGES/libc.mo
usr/share/locale/*/LC_MESSAGES/rhpl.mo
usr/share/locale/*/LC_MESSAGES/rhpxl.mo
usr/share/locale/*/LC_MESSAGES/parted.mo
usr/share/locale/*/LC_MESSAGES/pirut.mo
usr/share/locale/*/LC_MESSAGES/zenity.mo
usr/share/pirut
usr/share/pixmaps/comps/*.png
usr/share/pixmaps/gnome-default-dlg.png
usr/share/pixmaps/gnome-error.png
usr/share/pixmaps/gnome-info.png
usr/share/pixmaps/gnome-question.png
usr/share/pixmaps/gnome-warning.png
usr/share/pixmaps/no.xpm
usr/share/pixmaps/yes.xpm
usr/share/ppc64-utils
usr/share/system-config-keyboard
usr/share/system-config-date
usr/share/terminfo/x/xterm
usr/share/zenity
usr/share/zoneinfo/zone.tab
EOF

TIMEZONES="
usr/share/zoneinfo/Africa
usr/share/zoneinfo/America
usr/share/zoneinfo/Antarctica
usr/share/zoneinfo/Arctic
usr/share/zoneinfo/Asia
usr/share/zoneinfo/Atlantic
usr/share/zoneinfo/Australia
usr/share/zoneinfo/Canada
usr/share/zoneinfo/Europe
usr/share/zoneinfo/Indian
usr/share/zoneinfo/Japan
usr/share/zoneinfo/Mideast
usr/share/zoneinfo/Pacific
usr/share/zoneinfo/US
"

# more dogtail stuff...
cat >> $KEEPFILEGR <<EOF
usr/$LIBDIR/gtk-2.0/modules/libatk-bridge.so
usr/$LIBDIR/gtk-2.0/modules/libgail.so
usr/libexec/bonobo-activation-server
usr/libexec/at-spi-registryd
usr/$LIBDIR/bonobo/servers
usr/libexec/gconfd-2
usr/$LIBDIR/GConf/2/libgconfbackend-xml.so
usr/$LIBDIR/python?.?/site-packages/gtk-2.0/atk.so
usr/$LIBDIR/python?.?/site-packages/gtk-2.0/gconf.so
usr/$LIBDIR/python?.?/site-packages/atspi.so
usr/lib/python?.?/site-packages/dogtail/*.py
EOF



#
# KEEPFILERESCUE is all files to keep from the packages in PACKAGESRESCUE
#
# This defines the files in addition to KEEPFILE and KEEPFILEGR that make up 
# the install images for NFS and CD/DVD based rescue mode installs.  This 
# image is not loaded into memory so it can be considerably larger.
#
# NOTE: hd, ftp, and http rescue mode use and image based on KEEPFILE since
#       it has to be much smaller due to memory usage.
#
KEEPFILERESCUE=${TMPDIR:-/tmp}/keepfilerescue.$$
cat > $KEEPFILERESCUE <<EOF
bin/find
bin/gunzip
bin/gzip
bin/mt
bin/ping
bin/sync
bin/tar
bin/zcat
etc/joe
sbin/arp
sbin/depmod
sbin/dmraid
sbin/dmsetup
sbin/dump
sbin/ifconfig
sbin/insmod
sbin/lsmod
sbin/modinfo
sbin/modprobe
sbin/netstat
sbin/restore
sbin/rrestore
sbin/rmmod
sbin/route
sbin/mount.cifs
sbin/umount.cifs
usr/bin/bunzip2
usr/bin/bzcat
usr/bin/bzip2
usr/bin/emacs
usr/bin/find
usr/bin/ftp
usr/bin/jmacs
usr/bin/joe
usr/bin/jpico
usr/bin/less
usr/bin/mattrib
usr/bin/mbadblocks
usr/bin/mcd
usr/bin/mcopy
usr/bin/mdel
usr/bin/mdeltree
usr/bin/mdir
usr/bin/mdu
usr/bin/mformat
usr/bin/minfo
usr/bin/mlabel
usr/bin/mmd
usr/bin/mmount
usr/bin/mmove
usr/bin/mpartition
usr/bin/mrd
usr/bin/mread
usr/bin/mren
usr/bin/mshowfat
usr/bin/mtools
usr/bin/mtype
usr/bin/mzip
usr/bin/open
usr/bin/rcp
usr/bin/rlogin
usr/bin/rsh
usr/bin/rsync
usr/bin/scp
usr/bin/sftp
usr/bin/shred
usr/bin/ssh
usr/bin/termidx
usr/bin/xargs
usr/bin/ntfscat
usr/bin/ntfscluster
usr/bin/ntfscmp
usr/bin/ntfsdecrypt
usr/bin/ntfsdump_logfile
usr/bin/ntfsfix
usr/bin/ntfsinfo
usr/bin/ntfsls
usr/bin/ntfsmftalloc
usr/bin/ntfsmove
usr/bin/ntfstruncate
usr/bin/ntfswipe
usr/bin/firstaidkit
usr/sbin/mkntfs
usr/sbin/ntfsclone
usr/sbin/ntfscp
usr/sbin/ntfslabel
usr/sbin/ntfsresize
usr/sbin/ntfsundelete
usr/sbin/mtr
usr/sbin/smartctl
usr/sbin/traceroute
usr/$LIBDIR/firstaidkit-plugins/
usr/lib/python?.?/site-packages/pyfirstaidkit/
EOF

if [ $ARCH = i386 -o $ARCH = x86_64 ]; then
    cat >> $KEEPFILERESCUE <<-EOF
sbin/grub
usr/bin/gpart
usr/share/grub
EOF
fi

echo "Assembling package list..."
RPMS="$PACKAGES"
[ -n "$DEBUG" ] && echo "RPMS are $RPMS"
RPMSGR="$PACKAGES $PACKAGESGR $PACKAGESRESCUE"
[ -n "$DEBUG" ] && echo "RPMSGR are $RPMSGR"

rm -rf $DEST; mkdir -p $DEST/usr/sbin
rm -rf $DESTGR; mkdir -p $DESTGR/usr/sbin $DESTGR/var/lib $DESTGR/boot
ln -s /tmp $DESTGR/var/lib/xkb 

#
# concat KEEPFILEGR and KEEPFILERESCUE lists
#
cat $KEEPFILERESCUE >> $KEEPFILEGR
cat $KEEPFILE >> $KEEPFILEGR

echo `date` "Expanding text packages..."
YUMDIR=${TMPDIR:-/tmp}/yumdir.$$
mkdir -p $YUMDIR/var/log
mkdir -p $YUMDIR/var/lib/yum

expandPackageSet $yumconf $YUMDIR "$RPMS" $DEST $KEEPFILE
echo `date` "Done Expanding text packages..."

echo `date` "Expanding graphical packages..."
expandPackageSet $yumconf $YUMDIR "$RPMSGR" $DESTGR $KEEPFILEGR
echo `date` "Done Expanding graphical packages..."

# more dogtail stuff
if [ -n "$NEEDGR" ]; then
    # Dogtail will check this
    echo "Creating customized GConf2 settings for root"
    mkdir -p $DESTGR/.gconf/desktop/gnome/interface
    touch $DESTGR/.gconf/desktop/%gconf.xml
    touch $DESTGR/.gconf/desktop/gnome/%gconf.xml
    cat > $DESTGR/.gconf/desktop/gnome/interface/%gconf.xml <<EOF
<?xml version="1.0"?>
<gconf>
        <entry name="accessibility" mtime="1176200664" type="bool" value="true">
        </entry>
</gconf>
EOF
fi

rm -rf $YUMDIR

chown -R root:root $DEST $DESTGR
chmod -R a+rX-w $DEST $DESTGR

# Remove locales unused during the install
cat $DESTGR/usr/lib/anaconda/lang-table* | awk '
{ gsub("-", "", $4);
  print $4;
  print gensub(/\..*$/,"","",$4);
  print gensub(/_.*$/,"","",$4);
  if (split ($4, a, ".") > 1) {
    print gensub(/\..*$/,tolower("." a[2]),"",$4);
  };
  print $2;
}
' | sed -e 's/latn/Latn/g' | LC_ALL=C sort -u > $DESTGR/locales
for p in lib share; do (
cd $DESTGR/usr/$p/locale && {
ls | grep -v locale.alias | grep -v locale-archive | LC_ALL=C sort > $DESTGR/locales.list
LC_ALL=C comm -13 $DESTGR/locales $DESTGR/locales.list | xargs rm -rf
}
); done
rm -f $DESTGR/locales $DESTGR/locales.list

# fixup joe links
if [ -n "$NEEDGR" -a -d "$DESTDIR"/etc/joe ]; then
    ln -fs jpicorc $DESTGR/etc/joe/picorc
    ln -fs jpicorc $DESTGR/etc/joe/jnanorc
    ln -fs jpicorc $DESTGR/etc/joe/nanorc
    ln -fs jmacsrc $DESTGR/etc/joe/emacsrc
    ln -fs jmacs $DESTGR/usr/bin/emacs
    ln -fs jpico $DESTGR/usr/bin/pico
    ln -fs jpico $DESTGR/usr/bin/nano
fi

# fix up some links for man page related stuff
for p in $DEST $DESTGR; do
    for file in nroff groff iconv geqn gtbl gpic grefer ; do
        ln -fs /mnt/sysimage/usr/bin/$file $p/usr/bin/$file
    done
done

# create selinux config
if [ -e $DEST/etc/selinux/targeted ]; then
  cat > $DEST/etc/selinux/config <<EOF
SELINUX=permissive
SELINUXTYPE=targeted
EOF
  cp $DEST/etc/selinux/config $DESTGR/etc/selinux/config
fi

echo "Creating libuser.conf"
cat > $DEST/etc/libuser.conf <<EOF
[defaults]
skeleton = /mnt/sysimage/etc/skel
mailspooldir = /mnt/sysimage/var/mail
crypt_style = md5
modules = files shadow
create_modules = files shadow
[files]
directory = /mnt/sysimage/etc
[shadow]
directory = /mnt/sysimage/etc
EOF

cp $DEST/etc/libuser.conf $DESTGR/etc/libuser.conf

sed -i 's|\(installforallkernels\) = 0|\1 = 1|' $DEST/etc/yum/pluginconf.d/fedorakmod.conf
cp $DEST/etc/yum/pluginconf.d/fedorakmod.conf $DESTGR/etc/yum/pluginconf.d/

#
# Manual pages in rescue: We dont have man pages in the image, so we point everything (The pages
# and the man scripts to the /mnt/sysimage.  We want the man command to depend only on the
# man.conf file, so we don't use the $MANPATH env variable.  The executables stay unchanged as
# they will be soft links to /mnt/sysimage.
#
if [ -n "$NEEDGR" ]; then
    echo "Fixing up /etc/man.config to point into /mnt/sysimage"
    #
    # Lets avoid the lines with MANPATH_MAP for now
    #
    sed -i "s,^MANPATH[^_MAP][ \t]*,&/mnt/sysimage," $DESTGR/etc/man.config

    #
    # Lets change the lines with MANPATH_MAP.  Don't know how much of a difference this will make.
    #
    sed -i "s,^MANPATH_MAP[ \t]*[a-zA-Z0-9/]*[ \t]*,&/mnt/sysimage," $DESTGR/etc/man.config
fi

rm -f $KEEPFILE $KEEPFILEGR $KEEPFILERESCUE

for p in $DEST $DESTGR; do
	echo "Scrubbing trees..." "$p"
	mkdir -p $p/lib
	mkdir -p $p/firmware
	ln -snf /modules $p/lib/modules
	ln -snf /firmware $p/lib/firmware
	cp $p/usr/lib/anaconda/raidstart-stub $p/usr/bin/raidstart
	cp $p/usr/lib/anaconda/raidstop-stub $p/usr/bin/raidstop
	cp $p/usr/lib/anaconda/losetup-stub $p/usr/bin/losetup
	cp $p/usr/lib/anaconda/dhcpclient-stub $p/usr/bin/dhcpclient
	( cd $p/usr/bin && rm -f pump && ln -sf dhcpclient pump )
	cp $p/usr/lib/anaconda/list-harddrives-stub $p/usr/bin/list-harddrives
	cp $p/usr/lib/anaconda/loadkeys-stub $p/usr/bin/loadkeys
	cp $p/usr/lib/anaconda/mknod-stub $p/usr/bin/mknod
	cp $p/usr/lib/anaconda/syslogd-stub $p/usr/bin/syslogd
	mv $p/usr/sbin/anaconda $p/usr/bin/anaconda
	mv $p/usr/lib/anaconda-runtime/lib* $p/usr/$LIBDIR

	# we only want the libunicode-lite stuff on the minimal image
	if [ $p = $DESTGR ]; then
	    rm -f $p/usr/$LIBDIR/libunicode-lite*
	fi

	find $p -type d | xargs chmod 755

	if [ -f $p/bin/bash ]; then
	    rm -f $p/bin/ash
	    ln -s bash $p/bin/sh
	else
	    ln -sf busybox $p/bin/sh
	fi

	[ -d $p/bin ] || die "ERROR: directory missing: $p/bin"
	[ -d $p/sbin ] || die "ERROR: directory missing: $p/sbin"
	(cd $p/bin; find) | (cd $p/bin; /bin/cpio --quiet -pdmu $p/usr/bin)
	(cd $p/sbin; find) | (cd $p/sbin; /bin/cpio --quiet -pdmu $p/usr/sbin)
	rm -rf $p/bin
	rm -rf $p/sbin

	# Fix relative links like /usr/bin/udevinfo -> ../../sbin/udevadm
	for brokenlink in $(find $p/usr/{bin,sbin} -follow -lname '*'); do
            target="$(readlink $brokenlink)"
            for pathbit in bin sbin; do
            # if it starts with "../../sbin/", remove that
		newtarget="${target##../../$pathbit/}"
            # if we removed something, replace it with the proper path
		if [ "$newtarget" != "$target" ]; then
                # make it ../sbin/ instead
                    ln -sf "../$pathbit/$newtarget" "$brokenlink"
		fi
            done
	done

	# copy bootloader files for sparc
	if [ $ARCH = sparc -a $p = $DESTGR ]; then
	    mkdir -p $p/usr/lib/anaconda-runtime/boot
	    [ -d $p/boot ] || die "ERROR: directory missing: $p/boot"
	    (cd $p/boot; find -name "*.b") | (cd $p/boot; /bin/cpio --quiet -pdmu $p/usr/lib/anaconda-runtime/boot)
        fi

	# copy bootloader file for ppc
	if [ $ARCH = ppc -o $ARCH = ppc64 ]; then
	    mkdir -p $p/usr/lib/anaconda-runtime/boot
	    cp -af $p/boot/efika.forth $p/usr/lib/anaconda-runtime/boot
        fi
	
	# copy bootloader file for alpha
	if [ $ARCH = alpha -a $p = $DESTGR ]; then
	    mkdir -p $p/usr/lib/anaconda-runtime/boot
	    cp -af $p/boot/bootlx $p/usr/lib/anaconda-runtime/boot
        fi
	
	# copy bootloader files for ia64
	if [ $ARCH = ia64 -a $p = $DESTGR ]; then
	    mkdir -p $p/usr/lib/anaconda-runtime/boot
	    cp -af $p/boot/efi/EFI/redhat//* $p/usr/lib/anaconda-runtime/boot
	fi    

	# copy bootloader files for i386/x86_64
	if [ $ARCH = i386 -o $ARCH = x86_64 ]; then
	    if [ $p = $DESTGR ]; then
		mkdir -p $p/usr/lib/anaconda-runtime/boot
		cp -af $p/boot/memtest* $p/usr/lib/anaconda-runtime/boot
	    fi
	fi

	rm -rf $p/boot $p/home $p/root $p/tmp

	find $p -name "*.a" | grep -v kernel-wrapper/wrapper.a | xargs rm -rf
	find $p -name "lib*.la" |grep -v "usr/$LIBDIR/gtk-2.0" | xargs rm -rf

	# nuke some python stuff we don't need
        for d in idle distutils bsddb lib-old hotshot doctest.py pydoc.py site-packages/japanese site-packages/japanese.pth ; do
	    rm -rf $p/$d
	done

	$p/usr/lib/anaconda-runtime/scrubtree $p
done

echo "Creating debug dir"
for p in $DEST $DESTGR ; do
    mkdir -p $p/usr/lib/debug
    mkdir -p $p/usr/src/debug
done

for p in $DEST $DESTGR; do
	find $p -name "*.py" | while read fn; do
	    rm -f ${fn}o
	    rm -f ${fn}c
	    ln -sf /dev/null ${fn}c
	done
done

# some python stuff we don't need for stage2
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/distutils/
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/lib-dynload/japanese
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/encodings/
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/compiler/
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/email/test/
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/curses/
rm -rf $DESTGR/usr/$LIBDIR/python?.?/site-packages/pydoc.py

# this is only for the minimal second stage
echo "Cleaning ramdisk install images..."
rm -rf $DEST/usr/lib/anaconda-runtime
