#!/bin/bash
#================
# FILE          : preinit
#----------------
# PROJECT       : openSUSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This file is called after the image root
#               : has changed by the linuxrc script
#               :
# STATUS        : BETA
#----------------
#======================================
# Functions...
#--------------------------------------
. /include

#======================================
# 1) start error log
#--------------------------------------
errorLogStart
Echo "Calling pre-init stage in system image"

#======================================
# 2) check for LOCAL_BOOT
#--------------------------------------
if [ "$LOCAL_BOOT" = "yes" ] && [ -z "$KIWI_RECOVERY" ];then
    exit 0
fi

#======================================
# 3) start udev
#--------------------------------------
mountSystemFilesystems
udevSystemStart

#======================================
# 4) update mount table
#--------------------------------------
updateMTAB

#======================================
# 5) create framebuffer devices
#--------------------------------------
createFramebufferDevices

#======================================
# 6) create initrd on diskful
#--------------------------------------
if \
    [ -z "$UNIONFS_CONFIG" ] && [ ! "$kiwi_oemkboot" = "true" ]
then
    #======================================
    # use distro initrd tool
    #--------------------------------------
    setupInitrd
else
    #======================================
    # use kiwi initrd from RW partition
    #--------------------------------------
    bootLoaderOK=1
fi

#======================================
# 7) Install boot loader if ok
#--------------------------------------
if [ $bootLoaderOK = 1 ];then
    installBootLoader
fi

#======================================
# 8) copy recovery related files
#--------------------------------------
if [ "$LOCAL_BOOT" = "no" ] && [ ! -z "$kiwi_oemrecovery" ];then
    Echo "Setting up recovery configuration archive..."
    runHook preRecoverySetup
    mkdir -p /reco-save
    if ! mount $imageRecoveryDevice /reco-save >/dev/null;then
        systemException "Failed to mount recovery device" "reboot"
    fi
    Echo "Installing boot loader into recovery partition"
    setupBootLoaderRecovery / /reco-save OEM
    installBootLoaderRecovery
    Echo "Store recovery UUIDs..."
    if ! blkid $imageRootDevice -s UUID -o value > /reco-save/root.uuid;then
        systemException "Failed to store rootfs UUID" "reboot"
    fi
    if ! blkid $imageBootDevice -s UUID -o value > /reco-save/boot.uuid;then
        systemException "Failed to store bootfs UUID" "reboot"
    fi
    Echo "Store recovery boot files phase 1)..."
    backupBootFiles=""
    for i in \
        /etc/fstab \
        /etc/sysconfig/kernel \
        /etc/sysconfig/bootloader \
        /etc/grub.conf \
        /etc/default/grub \
        /etc/grub.d/40_custom
    do
        test -e $i && backupBootFiles="$backupBootFiles $i"
    done
    if ! tar -czf /reco-save/boot-1.tgz $backupBootFiles;then
        systemException "Failed to tar up recovery boot-1.tgz" "reboot"
    fi
    Echo "Store recovery boot files phase 2)..."
    if [ ! -z "$kiwi_EfiPart" ];then
        efidev=$(ddn $imageDiskDevice $kiwi_EfiPart)
        label=$(blkid $efidev -s LABEL -o value)
        if [ "$label" = "EFI" ];then
            mkdir -p /boot/efi
            if ! mountpoint -q /boot/efi && ! mount $efidev /boot/efi;then
                systemException "Failed to mount EFI boot partition" "reboot"
            fi
        fi
    fi
    backupBootFiles=""
    for i in $(find /boot -type f); do
        if ! echo $i | grep -q -E "\.sys";then
            backupBootFiles="$backupBootFiles $i"
        fi
    done
    if ! tar -czf /reco-save/boot-2.tgz $backupBootFiles;then
        systemException "Failed to tar up recovery boot-2.tgz" "reboot"
    fi
    runHook postRecoverySetup
    umount /reco-save
    umount /boot/efi &>/dev/null
    rmdir /reco-save
fi

#======================================
# 9) setup console
#--------------------------------------
setupConsole

#======================================
# 10) setup machine id
#--------------------------------------
setupMachineID

#======================================
# 11) Run user script
#--------------------------------------
runHook preCallInit

#======================================
# 12) kill udev
#--------------------------------------
udevSystemStop
umountSystemFilesystems
