#!/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" ] && [ -z "$COMBINED_IMAGE" ] && \
    [ ! "$OEM_KIWI_INITRD" = "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 "$OEM_RECOVERY" ];then
    Echo "Setting up recovery configuration archive..."
    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 boot files phase 1)..."
    backupBootFiles=""
    for i in \
        /etc/fstab \
        /etc/sysconfig/kernel \
        /etc/sysconfig/bootloader \
        /etc/grub.conf \
        /etc/lilo.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)..."
    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
    umount /reco-save
    rmdir /reco-save
fi

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

#======================================
# 10) create origin snapshot if possible
#--------------------------------------
createOriginSnapshot

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

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