#!/bin/bash

LIVECD=/mnt/livecd
TARGET=$LIVECD/target
TARGET_SOURCE=$TARGET/files/source
SOURCE=$LIVECD/source


echo -n "Йде копіювання файлів для LiveCD ... "
rm -rf $TARGET
mkdir -p $TARGET/squash
cp -a $SOURCE/boot $TARGET/squash/
mkdir -p $TARGET_SOURCE
#cp -pRPd $SOURCE/* $TARGET_SOURCE
cp -pRPd $SOURCE/bin $TARGET_SOURCE/
cp -pRPd $SOURCE/chroot $TARGET_SOURCE/
cp -pRPd $SOURCE/dev $TARGET_SOURCE/
cp -pRPd $SOURCE/diskless $TARGET_SOURCE/
cp -pRPd $SOURCE/etc $TARGET_SOURCE/
cp -pRPd $SOURCE/home $TARGET_SOURCE/
cp -pRPd $SOURCE/lib $TARGET_SOURCE/
cp -pRPd $SOURCE/mnt $TARGET_SOURCE/
cp -pRPd $SOURCE/opt $TARGET_SOURCE/
cp -pRPd $SOURCE/proc $TARGET_SOURCE/
cp -pRPd $SOURCE/root $TARGET_SOURCE/
cp -pRPd $SOURCE/sbin $TARGET_SOURCE/
cp -pRPd $SOURCE/selinux $TARGET_SOURCE/
cp -pRPd $SOURCE/sys $TARGET_SOURCE/
cp -pRPd $SOURCE/tmp $TARGET_SOURCE/

#cp -pRPd $SOURCE/usr $TARGET_SOURCE
mkdir -p $TARGET_SOURCE/usr
cp -pRPd $SOURCE/usr/bin $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/i686-pc-linux-gnu $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/include $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/info $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/kde $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/kdevbdb $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/lib $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/libexec $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/local $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/man $TARGET_SOURCE/usr/
mkdir $TARGET_SOURCE/usr/portage
cp -pRPd $SOURCE/usr/qt $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/sbin $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/share $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/spool $TARGET_SOURCE/usr/
mkdir $TARGET_SOURCE/usr/src
cp -pRPd $SOURCE/usr/tmp $TARGET_SOURCE/usr/
cp -pRPd $SOURCE/usr/X11R6 $TARGET_SOURCE/usr/

#cp -pRPd $SOURCE/var $TARGET_SOURCE
mkdir -p $TARGET_SOURCE/var
cp -pRPd $SOURCE/var/arpwatch $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/bind $TARGET_SOURCE/var/
mkdir $TARGET_SOURCE/var/cache
cp -pRPd $SOURCE/var/cache/edb $TARGET_SOURCE/var/cache/
cp -pRPd $SOURCE/var/db $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/empty $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/ftp $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/lib $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/lock $TARGET_SOURCE/var/
mkdir $TARGET_SOURCE/var/log
cp -pRPd $SOURCE/var/mail $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/run $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/spool $TARGET_SOURCE/var/
cp -pRPd $SOURCE/var/state $TARGET_SOURCE/var/
mkdir $TARGET_SOURCE/var/tmp; chmod 777 $TARGET_SOURCE/var/tmp
cp -pRPd $SOURCE/var/www $TARGET_SOURCE/var/
cp -prf /home/DYSTRYK/LiveCD/*  $TARGET_SOURCE/

cp -f /home/DYSTRYK/bin/LiveCDbefore-build.sh $TARGET_SOURCE/
echo " OK!"

echo -n "Монтуання спецрозділів ... "
cd $TARGET_SOURCE
mount -o bind /sys $TARGET_SOURCE/sys
mount -o bind /dev $TARGET_SOURCE/dev
mount -o bind /proc $TARGET_SOURCE/proc
mount -o bind /usr/portage $TARGET_SOURCE/usr/portage
mount -o bind /usr/portage/distfiles $TARGET_SOURCE/usr/portage/distfiles
echo " OK!"

echo "Зараз виконується ./before-build, але спочатку ВПЕВНІТЬСЯ, що ви \
правильно виконали команду chroot у livecd $TARGET_SOURCE каталозі."
chroot $TARGET_SOURCE/ /LiveCDbefore-build.sh
echo -n "Розмонтуання спецрозділів ... "
umount $TARGET_SOURCE/sys
umount $TARGET_SOURCE/dev
umount $TARGET_SOURCE/proc
umount $TARGET_SOURCE/usr/portage/distfiles
sleep 3
umount $TARGET_SOURCE/usr/portage
echo " OK!"
env-update
source /etc/profile

echo -n "Очистка дерева LiveCD ... "
cd $TARGET_SOURCE
rm -rf boot
#rm etc/make.profile
rm -f etc/mtab
touch etc/mtab
#rm -rf etc/portage
rm -rf etc/sysconfig/*
rm -rf root/.ccache
rm root/.bash_history
if [ -f root/.zcompdump ];then
    rm root/.zcompdump
fi
rm -rf home/ebuild/.ccache
rm home/ebuild/.bash_history
if [ -f home/ebuild/.zcompdump ];then
    rm home/ebuild/.zcompdump
fi
rm -rf home/monitoring/*
rm -rf mnt/sda*
rm -rf tmp/*
rm -rf usr/portage/*
rm -rf usr/share/doc/*
rm -rf usr/src/*
rm -rf var/tmp/*
rm -rf var/run/*
rm -rf var/lock/*
rm -rf var/cache/*
rm -rf var/www/localhost/htdocs/administration/monitoring/*

cd var/db
tar -cf pkg.tar pkg
bzip2 -z9 pkg.tar
mv pkg.tar.bz2 ../../../../../
cd $TARGET_SOURCE

rm -rf var/db/monitoring/*
rm -rf var/db/pkg/*

rm -rf diskless

rm -rf var/log
cp -prf /home/DYSTRYK/LiveCD/* ./
cp -rf /home/DYSTRYK ./home/
rm -rf ./home/DYSTRYK/tmp
rm LiveCDbefore-build.sh
rm info
echo " OK!"

cd $TARGET/files
mksquashfs source/ $TARGET/squash/livecd.squashfs
cd $TARGET
touch squash/livecd
#rm -rf $TARGET/files/
mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -iso-level 4 -hide boot.catalog -o $LIVECD/LiveCD.iso $TARGET/squash/
echo "

Обрраз готовий для запису на диск 8-)"

exit 0