#!/bin/sh

# stage1 need this
mkdir -p /image

# setup stage2 as init
mv -f /usr/sbin/install2-init /sbin/init

# remove all docs
rpmquery -adl |grep ^/ |xargs -r rm -f --
rm -rf /usr/share/{doc,man,info,license,gfxboot}

# remove unused icons
rm -rf /usr/share/icons/{hicolor,large,mini}

# remove PAM plugins
rm -rf /lib*/security

# remove some unused files
rm -rf /usr/share/ca-certificates
rm -rf /usr/lib/qt4/plugins/codecs
rm -f /usr/lib*/gconv/IBM*
rm -f /usr/lib*/gconv/BIG5*
rm -f /usr/lib*/gconv/EBCDIC*

# remove cpp, gcc and perl
rpmquery -a cpp\* gcc\* perl-base |
	xargs -r rpmi -e --nodeps --

# remove unneeded kernel modules
rm -rf /lib/modules/*/kernel/arch
rm -rf /lib/modules/*/kernel/drivers/atm
rm -rf /lib/modules/*/kernel/drivers/char/watchdog
rm -rf /lib/modules/*/kernel/drivers/cpufreq
rm -rf /lib/modules/*/kernel/drivers/edac
rm -rf /lib/modules/*/kernel/drivers/firmware
rm -rf /lib/modules/*/kernel/drivers/hwmon
rm -rf /lib/modules/*/kernel/drivers/infiniband
rm -rf /lib/modules/*/kernel/drivers/input/{gameport,joy*}
rm -rf /lib/modules/*/kernel/drivers/isdn
rm -rf /lib/modules/*/kernel/drivers/media
rm -rf /lib/modules/*/kernel/drivers/mmc
rm -rf /lib/modules/*/kernel/drivers/mtd*
rm -rf /lib/modules/*/kernel/drivers/net/irda
rm -rf /lib/modules/*/kernel/drivers/spi
rm -rf /lib/modules/*/kernel/drivers/telephony
rm -rf /lib/modules/*/kernel/drivers/usb/{misc,mon}
rm -rf /lib/modules/*/kernel/drivers/w1
rm -rf /lib/modules/*/kernel/fs/9p
rm -rf /lib/modules/*/kernel/fs/adfs
rm -rf /lib/modules/*/kernel/fs/affs
rm -rf /lib/modules/*/kernel/fs/afs
rm -rf /lib/modules/*/kernel/fs/autofs
rm -rf /lib/modules/*/kernel/fs/autofs4
rm -rf /lib/modules/*/kernel/fs/befs
rm -rf /lib/modules/*/kernel/fs/bfs
rm -rf /lib/modules/*/kernel/fs/cifs
rm -rf /lib/modules/*/kernel/fs/coda
rm -rf /lib/modules/*/kernel/fs/configfs
rm -rf /lib/modules/*/kernel/fs/cramfs
rm -rf /lib/modules/*/kernel/fs/efs
rm -rf /lib/modules/*/kernel/fs/freevxfs
rm -rf /lib/modules/*/kernel/fs/hfs
rm -rf /lib/modules/*/kernel/fs/hfsplus
rm -rf /lib/modules/*/kernel/fs/hpfs
rm -rf /lib/modules/*/kernel/fs/jffs
rm -rf /lib/modules/*/kernel/fs/jffs2
rm -rf /lib/modules/*/kernel/fs/lockd
rm -rf /lib/modules/*/kernel/fs/minix
rm -rf /lib/modules/*/kernel/fs/ncpfs
rm -rf /lib/modules/*/kernel/fs/ocfs2
rm -rf /lib/modules/*/kernel/fs/qnx4
rm -rf /lib/modules/*/kernel/fs/smbfs
rm -rf /lib/modules/*/kernel/fs/sysv
rm -rf /lib/modules/*/kernel/fs/udf
rm -rf /lib/modules/*/kernel/fs/ufs
rm -rf /lib/modules/*/kernel/net/*/netfilter
rm -rf /lib/modules/*/kernel/net/appletalk
rm -rf /lib/modules/*/kernel/net/ax25
rm -rf /lib/modules/*/kernel/net/bluetooth
rm -rf /lib/modules/*/kernel/net/dccp
rm -rf /lib/modules/*/kernel/net/decnet
rm -rf /lib/modules/*/kernel/net/econet
rm -rf /lib/modules/*/kernel/net/ipv6
rm -rf /lib/modules/*/kernel/net/ipx
rm -rf /lib/modules/*/kernel/net/irda
rm -rf /lib/modules/*/kernel/net/netfilter
rm -rf /lib/modules/*/kernel/net/netrom
rm -rf /lib/modules/*/kernel/net/rose
rm -rf /lib/modules/*/kernel/net/sctp
rm -rf /lib/modules/*/kernel/net/tipc
rm -rf /lib/modules/*/kernel/net/x25
rm -rf /lib/modules/*/kernel/sound

# remove blacklisted kernel modules
sed -n 's/^blacklist[[:space:]]\+\([^[:space:]]\+\).*/\1/p' /etc/modprobe.d/* |
	while read i; do
		find /lib/modules/ -type f -name "$i.ko" -delete
	done

# remove rpm database
rm -rf /var/lib/rpm/*

# remove apt data files
rm -rf /var/cache/apt /var/lib*/apt

# remove unneeded l10n
find /usr/share/locale/ -type f \! -name 'alterator*' -delete

# remove non-utf8 locales
find /usr/lib*/locale -mindepth 1 -maxdepth 1 -type d \! -name '*.utf8' -print0 |
	xargs -r0 rm -rf --

# run depmod after kernel modules removal
for i in /lib*/modules/*; do
    /sbin/depmod -a -F /boot/System.map-${i##*/} ${i##*/}
done

# remove ogfs
rm -f /lib*/evms/*/ogfs*

# remove kernel images
rm -rf /boot/*

# udev needed this
ln -sf /proc/mounts /etc/mtab
