#!/bin/sh
# Filename:      /etc/grml/fai/config/scripts/GRMLBASE/98-clean-chroot
# Purpose:       clean up chroot system
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2 or any later version.
################################################################################

set -e

# check for policy-rc.d workaround of
# /etc/grml/fai/config/hooks/updatebase.GRMLBASE
if [ -r $target/usr/sbin/policy-rc.d ] ; then
   if grep -q 'FAI_ACTION=updatebase' $target/usr/sbin/policy-rc.d ; then
      rm -f $target/usr/sbin/policy-rc.d
   fi
fi

# remove some big directories when using class LATEX_CLEANUP:
if ifclass LATEX_CLEANUP ; then
   cp /etc/grml/fai/grml/grml_cleanup_chroot.latex_cleanup $target/root/
   $ROOTCMD  /root/grml_cleanup_chroot.latex_cleanup
   rm $target/root/grml_cleanup_chroot.latex_cleanup
fi

# skip tasks which require only when using class NO_ONLINE:
if ! ifclass NO_ONLINE ; then
   cp /etc/grml/fai/grml/grml_cleanup_chroot.online $target/root/
   $ROOTCMD  /root/grml_cleanup_chroot.online
   rm $target/root/grml_cleanup_chroot.online
fi

# make sure to drop from grml-small what's unnecessary:
if ifclass GRML_SMALL ; then
   $ROOTCMD apt-get -y --purge remove aptitude
   for pkg in groff-base info locales man-db manpages nano; do
     $ROOTCMD apt-get --purge remove --yes $pkg || true
   done
fi

# drop unnecessary software:
if ifclass GRML_SMALL || ifclass DEBORPHAN ; then
   if [ -f /etc/grml/fai/grml/grml_cleanup_chroot.deborphan ] ; then
      cp /etc/grml/fai/grml/grml_cleanup_chroot.deborphan $target/root/
      $ROOTCMD  /root/grml_cleanup_chroot.deborphan
      rm $target/root/grml_cleanup_chroot.deborphan
   fi
fi

# remove /usr/share/doc, /usr/share/info,... only in class REMOVE_DOCS:
# (important: remove them *after* deinstalling packages, otherwise
# removing packages might fail due to lack of /usr/share/man/...)
if ifclass REMOVE_DOCS ; then
   cp /etc/grml/fai/grml/grml_cleanup_chroot.remove_docs $target/root/
   $ROOTCMD  /root/grml_cleanup_chroot.remove_docs
   rm $target/root/grml_cleanup_chroot.remove_docs
fi
# misc cleanup:
if [ -f /etc/grml/fai/grml/grml_cleanup_chroot ] ; then
   cp /etc/grml/fai/grml/grml_cleanup_chroot $target/root/
   $ROOTCMD  /root/grml_cleanup_chroot
   rm $target/root/grml_cleanup_chroot
fi

# make sure GRML_SMALL uses the appropriate configuration:
if ifclass GRML_SMALL ; then
   cp $target/etc/inittab.small $target/etc/inittab
   cp $target/etc/runlevel.conf.livecd.small $target/etc/runlevel.conf
fi

if ifclass RELEASE ; then
   # Remove all FAI logs from chroot via grml-live later then:
   touch $target/etc/grml_fai_release
   # Remove all files inside /root/ of chroot:
   rm -rf $target/root && mkdir -m 0755 $target/root
fi

## END OF FILE #################################################################
# vim:ft=sh expandtab ai tw=80 tabstop=4 shiftwidth=3
