#!/bin/sh -e
#
# Copy running system to fixed disk
#
# Copyright (C) 2010-2018 Eugene Prokopiev  <enp@altlinux.org>
# Copyright (C) 2011-2012 Michael Pozhidaev <msp@altlinux.org>
# Copyright (C) 2013      Michael A. Kangin <prividen@altlinux.org>

error()
{
    echo "ERROR : $1"
    exit 1
}

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


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

[ "$1" == "help" ] && error "Run $THIS [\$ROOT_LABEL] [\$LOADER] [\$NEW_USER] to copy running system to fixed disk"

ROOT_LABEL=${1:-system}
LOADER=${2:-extlinux}
NEW_USER=$3

FSTYPE=$(blkid -t LABEL=$ROOT_LABEL -s TYPE -o value | head -1)
[ -z $FSTYPE ] && error "Partition with label [$ROOT_LABEL] is not found"

for PART in $(blkid -t LABEL=$ROOT_LABEL -o device)
do
    grep -q "^$PART " /proc/mounts && error "Partition with label [$ROOT_LABEL] must not be mounted"
done

[ $FSTYPE == 'btrfs' ] && MOUNT_OPTIONS=',compress=lzo' && LOADER=${2:-grub}

TMPDIR=$(/bin/mktemp -d)
mount -L $ROOT_LABEL -o defaults$MOUNT_OPTIONS $TMPDIR

[ -z "$(ls -1 $TMPDIR | grep -v lost+found)" ] || error "Partition with label [$ROOT_LABEL] is not empty"

cd $TMPDIR

rm -rf *

rsync -a /bin /boot /etc /lib* /root /sbin /usr /var /home .
mkdir dev media mnt proc srv sys tmp selinux cgroup run
mkdir -p var/log/journal
cat /usr/share/live-install/dev.cpio.bz2 | bunzip | cpio -i > /dev/null

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

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

echo >> etc/fstab

for SWAP in $(blkid -t TYPE=swap -s UUID -o value)
do
    echo "UUID=$SWAP		swap	swap	defaults	0 0" >> etc/fstab
done

echo >> etc/fstab
echo "LABEL=$ROOT_LABEL	/	$FSTYPE		relatime$MOUNT_OPTIONS	1 1" >> etc/fstab

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

for PART in $(blkid -t LABEL=$ROOT_LABEL -o device)
do
    if echo $PART | grep -q /dev/md
    then
        mdadm --detail $PART | grep active.*/dev/ | awk '{print substr($7, 1, length($7)-1)}' | \
            while read BOOT_DEVICE
            do
               install_loader $BOOT_DEVICE
            done
    else
        BOOT_DEVICE=$(echo $PART | sed 's/[0-9]$//')
        install_loader $BOOT_DEVICE
    fi
done

if [ $LOADER == "extlinux" ]; then
    chroot . rpm -e --nodeps grub-common grub-pc
    echo "DEFAULT /boot/vmlinuz initrd=/boot/initrd.img root=LABEL=$ROOT_LABEL audit=0" >> boot/extlinux/extlinux.conf
    chroot . extlinux -i /boot/extlinux
elif [ $LOADER == "grub" ]; then
    chroot . rpm -e --nodeps syslinux syslinux-extlinux
    subst 's|GRUB_AUTOUPDATE_CFG=true|GRUB_AUTOUPDATE_CFG=false|g' etc/sysconfig/grub2
    cat > boot/grub/grub.cfg << EOF
set default=0
set timeout=0
menuentry "linux" {
    linux	/boot/vmlinuz root=LABEL=$ROOT_LABEL audit=0
    initrd	/boot/initrd.img
}
EOF
fi

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

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

LIVEPKGS=$(rpm -qa | grep ^live)

chroot . rpm -e $LIVEPKGS
chroot . update_chrooted all

umount proc
umount sys
umount dev

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