#!/bin/sh -e
#
# Copy altlinux live to fixed disk
#
# Copyright (C) 2010-2013 Eugene Prokopiev  <enp@altlinux.org>
# Copyright (C) 2011      Michael Pozhidaev <msp@altlinux.org>

THIS="${0##*/}"

if [ -z "$2" ]; then
	cat <<EOF

Copy altlinux live to fixed disk

Usage:
	$THIS \$BOOT_DEVICE \$ROOT_DEVICE [\$LOADER] [\$NEW_USER]

EOF
	exit 1
fi

BOOT_DEVICE=$1
ROOT_DEVICE=$2
LOADER=${3:-extlinux}
NEW_USER=$4

if grep -q "^$ROOT_DEVICE " /proc/mounts; then
	TMPDIR=`grep "^$ROOT_DEVICE " /proc/mounts |awk '{print $2}'`
else
	mkfs.ext4 $ROOT_DEVICE > /dev/null
	TMPDIR=`/bin/mktemp -d`
	mount $ROOT_DEVICE $TMPDIR
	mountpoint -q "$TMPDIR" || exit
fi

cd $TMPDIR
rsync -a /bin /boot /etc /lib* /root /sbin /usr /var /home ./
mkdir dev media mnt proc srv sys tmp selinux cgroup run
cat /usr/share/live-install/dev.cpio.bz2 | bunzip | cpio -i > /dev/null
rm -f etc/rc.d/rc.local
rm -f etc/issue
rm -f etc/mtab
touch etc/mtab
subst "/^REMOUNT_ROOTFS_RW_COMMAND/d" etc/sysconfig/init

mount -o bind /dev dev
mount -o bind /sys sys
mount -o bind /proc proc

head -n 3 /etc/fstab > etc/fstab

for part in `awk '(NR>2){print $4}' /proc/partitions`; do
	if [ "`fstyp /dev/$part`" = "swap" ]; then
		echo "/dev/$part	swap	swap	defaults	0 0" >> etc/fstab
	fi
done

cat >> etc/fstab << EOF
$ROOT_DEVICE	/		ext4	relatime	1 1
EOF

grep " $TMPDIR/" /proc/mounts | while read DEV MPOINT FSTYPE Z Z Z; do
	MPOINT=`echo $MPOINT | sed -e "s|$TMPDIR||"`
	case $MPOINT in
		/proc|/dev|/sys)
			continue ;;
		/usr|/usr/local|/var/www)
			FSOPTS="nodev,relatime" ;;
		/var|/home)
			FSOPTS="nosuid,relatime" ;;
		/srv|/opt|/var/ftp)
			FSOPTS="nosuid,nodev,relatime" ;;
		*)
			FSOPTS="relatime,noexec,nosuid,nodev" ;;
	esac

	echo -e "$DEV\t$MPOINT\t$FSTYPE\t$FSOPTS\t1 2" >> etc/fstab
done

mdadm -Ebsc partitions >> etc/mdadm.conf

for script in `ls /usr/share/live-install/scripts.d/`
do
	. /usr/share/live-install/scripts.d/$script
done

LIVEPKGS=`rpm -qa | grep ^live`

chroot . installkernel --nolaunch "$(uname -r)"

if [ $LOADER == "extlinux" ]; then
	echo "DEFAULT /boot/vmlinuz initrd=/boot/initrd.img root=$ROOT_DEVICE" >> boot/extlinux/extlinux.conf
	chroot . extlinux -i /boot/extlinux
elif [ $LOADER == "grub" ]; then
	chroot . grub-mkconfig -o /boot/grub/grub.cfg
else
	echo "Warning: unsupported LOADER $LOADER"
fi

install_loader() {
	echo install loader into $1
	if [ $LOADER == "extlinux" ]; then
		cat /usr/lib/syslinux/mbr.bin > $1
	elif [ $LOADER == "grub" ]; then
		grub-install --boot-directory=./boot $1
	fi
}

if echo $BOOT_DEVICE |grep -q /dev/md; then
	mdadm --detail $BOOT_DEVICE |grep active.*/dev/ |awk '{print substr($7, 1, length($7)-1)}' | \
		while read REAL_BOOT_DEVICE; do
			install_loader $REAL_BOOT_DEVICE
		done
else
	install_loader $BOOT_DEVICE
fi

if [ -n "$NEW_USER" ]; then
	OLD_USER=`ls /home/`
	chroot . usermod -m -d /home/$NEW_USER -l $NEW_USER $OLD_USER
	chroot . groupmod -n $NEW_USER $OLD_USER
fi

chroot . rpm -e $LIVEPKGS
chroot . chkconfig sshd on
chroot . chkconfig acpid on
chroot . update_chrooted all

umount proc
umount sys
umount dev

awk '{print $2}' /proc/mounts |grep "^$TMPDIR/" |sort -r |while read FS; do umount $FS; done

cd /
umount $ROOT_DEVICE && rm -rf $TMPDIR
