#!/bin/bash
#================
# FILE          : linuxrc
#----------------
# 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 includes functions for the OEM
#               : installation mode. Installation means a dump of
#               : the virtual disk file onto a preselected disk
#               :
#               :
# STATUS        : BETA
#----------------
#
#======================================
# OEMInstall
#--------------------------------------
function OEMInstall {
	# /.../
	# Installation mode: find a usable disk to install the image
	# on. The install image is a virtual disk. The system will be
	# rebooted at the end of this function
	# ----
	local count=0
	local dsize=0
	local index=0
	#======================================
	# Check for install mode indicator file
	#--------------------------------------
	if [ ! -f $VMX_SYSTEM ];then
		return
	fi
	#======================================
	# Display license if text exists
	#--------------------------------------
	displayEULA
	#======================================
	# Search CD/DVD/USB stick and mount it
	#--------------------------------------
	USBStickDevice
	if [ $stickFound = 0 ];then
		Echo "Search for USB stick failed, checking CD/DVD drive"
		CDMount
		export OEMInstallType=CD
	else
		Echo "Found Stick: $stickRoot -> $stickSerial"
		mkdir -p /cdrom && mount $(ddn $stickRoot 2) /cdrom
		export OEMInstallType=USB
	fi
	#======================================
	# Search and ask for the install device
	#--------------------------------------
	IFS=$IFS_ORIG
	Echo "Searching harddrive for $OEMInstallType installation"
	hwinfo=/usr/sbin/hwinfo
	if [ "$OEMInstallType" = "USB" ];then
		deviceDisks=`$hwinfo --disk |\
			grep "Device File:" | cut -f2 -d: |\
			cut -f1 -d"(" | sed -e s"@$imageDiskDevice@@"`
	else
		deviceDisks=`$hwinfo --disk |\
			grep "Device File:" | cut -f2 -d: |\
			cut -f1 -d"("`
	fi
	export deviceDisks=`echo $deviceDisks`
	if [ -z "$deviceDisks" ];then
		systemException \
			"No device(s) for installation found... abort" \
		"reboot"
	fi
	Echo "Found following disk device(s)"
	count=0
	for i in $deviceDisks;do
		dsize=`partitionSize $i`
		Echo -b "Disk $count -> $i [ $dsize KByte ]"
		deviceArray[$count]=$i
		deviceDSize[$count]=$dsize
		count=`expr $count + 1`
	done
	if [ "$count" = "1" ];then
		#======================================
		# Found one single disk... use it
		#--------------------------------------
		instDisk=${deviceArray[0]}
	else
		#======================================
		# Found multiple disks...
		#--------------------------------------
		if [ -z "$OEM_SAP_INSTALL" ];then
			#======================================
			# standard mode, ask for install media
			#--------------------------------------
			hd="\"Select disk for installation:\""
			count=0
			for i in $deviceDisks;do
				dsize=`partitionSize $i`
				dname=`getDiskID $i`
				dname=`basename $dname | cut -c1-20`"..."
				if [ $count = 0 ];then
					dpara="$i \"$dname [ $dsize KByte ]\" on"
					count=1
				else
					dpara="$dpara $i \"$dname [ $dsize KByte ]\" off"
				fi
			done
			echo -n "dialog --stdout --radiolist $hd 20 75 15 $dpara" \
				> /tmp/dia.sh
			instDisk=$(runInteractive /tmp/dia.sh)
			if [ ! $? = 0 ];then
				systemException \
					"System installation canceled" \
				"reboot"
			fi
		else
			#======================================
			# SAP mode, use the smallest one
			#--------------------------------------
			count=0
			dsize=${deviceDSize[$count]}
			for i in $deviceDisks;do
				if [ ${deviceDSize[$count]} -lt $dsize ];then
					dsize=${deviceDSize[$count]}
					index=$count
				fi
				count=`expr $count + 1`
			done
			instDisk=${deviceArray[$index]}
		fi
	fi
	Echo "Entering installation mode for disk: $instDisk"
	if [ -z "$OEM_SAP_INSTALL" ];then
		dialog --yesno \
			"This will destroy ALL data on $instDisk, continue ?" 5 60
		if [ ! $? = 0 ];then
			systemException \
				"System installation canceled" \
			"reboot"
		fi
	fi
    clear
	#======================================
	# Import vmx configuration file
	#--------------------------------------
	importFile < $VMX_SYSTEM
	#======================================
	# Install disk system
	#--------------------------------------
	# install virtual disk image from the CD/DVD onto the
	# real disk. All data on the disk will be lost
	# ----
	imageZipped="uncompressed"
	imageDevice=$instDisk
	field=0
	#======================================
	# Evaluate OEM install file
	#--------------------------------------
	IFS=";" ; for n in $IMAGE;do
	case $field in
		0) field=1 ;; 
		1) imageName=$n   ; field=2 ;;
		2) imageVersion=$n; field=3 ;;
		3) imageZipped=$n ;
	esac
	done
	if [ "$imageZipped" = "compressed" ];then
		imageName="/cdrom/$imageName.gz"
		imageMD5="$imageName.md5"
	else
		imageName="/cdrom/$imageName"
		imageMD5="$imageName.md5"
	fi
	IFS=" "
	#======================================
	# Check MBR ID's...
	#--------------------------------------
	# /.../
	# mbr ID check is deactivated by default
	# see bug #525682 for details
	# ----
	nombridcheck=1
	if [ -z "$nombridcheck" ];then
		mbrD=$instDisk
		mbrI="cat $imageName"
		if [ "$imageZipped" = "compressed" ];then
			mbrI="gzip -cd $imageName"
		fi
		mbrM=`dd if=$mbrD bs=1 count=4 skip=$((0x1b8))|hexdump -n4 -e '"0x%x"'`
		mbrI=`$mbrI | dd  bs=1 count=4 skip=$((0x1b8))|hexdump -n4 -e '"0x%x"'`
		if [ $mbrM = $mbrI ];then
			systemException \
				"Base system already installed" \
			"reboot"
		fi	
	fi
	#======================================
	# read MD5 information...
	#--------------------------------------
	read sum1 blocks blocksize zblocks zblocksize < $imageMD5
	#======================================
	# Get available disk space
	#--------------------------------------
	haveKByte=`partitionSize $imageDevice`
	#======================================
	# Get required disk space, setup I/O
	#--------------------------------------
	needBytes=$(expr $blocks \* $blocksize)
	needKByte=`expr $needBytes / 1024`
	needMByte=`expr $needKByte / 1024`
	#======================================
	# Check disk space...
	#--------------------------------------
	haveMByte=`expr $haveKByte / 1024`
	needMByte=`expr $needKByte / 1024`
	Echo "Have size: $imageDevice -> $haveMByte MB"
	Echo "Need size: $imageName -> $needMByte MB"
	if [ $needMByte -gt $haveMByte ];then
		systemException \
			"Not enough space available for this image" \
		"reboot"
	fi
	#======================================
	# Dump image on disk
	#--------------------------------------
	dump="cat $imageName"
	if test "$imageZipped" = "compressed"; then
		dump="gzip -cd $imageName"
	fi
	if [ -x /usr/bin/dcounter ];then
		errorLogStop
		dump="$dump | dcounter -s $needMByte"
	fi
	Echo -n "Loading $imageName [$imageDevice] "
	if ! eval $dump | dd bs=32k of=$imageDevice &>/dev/null; then
		systemException \
			"Failed to install image: $imageName -> $imageDevice" \
		"reboot"
	fi
	echo
	if [ -x /usr/bin/dcounter ];then
		errorLogContinue
	fi
	#======================================
	# Check the md5sum of the raw disk
	#--------------------------------------
	Echo "Download complete, checking data..."
	dd if=$imageDevice bs=1024 |\
		head --bytes=$((blocks * blocksize)) |\
		md5sum - > /etc/ireal.md5
	read sum2 dumy < /etc/ireal.md5
	if [ $sum1 != $sum2 ];then
		systemException \
			"Image checksum test failed" \
		"reboot"
	fi
	Echo "Image checksum test: fine :-)"
	Echo "System installation has finished"
	#======================================
	# Umount CD/DVD USB
	#--------------------------------------
	umount /cdrom
	#======================================
	# Reread partition table
	#--------------------------------------
	blockdev --rereadpt $imageDevice
	deviceTest=$(ddn $imageDevice 1)
	if ! waitForStorageDevice $deviceTest;then
		systemException \
			"Partition $deviceTest doesn't appear... fatal !" \
		"reboot"
	fi
	#======================================
	# Setup/Reset values for further boot
	#--------------------------------------
	export imageDiskExclude=$instDisk
	export imageDiskDevice=$imageDevice
	unset stickSerial
	#======================================
	# Reboot system
	#--------------------------------------
	if [ "$OEMInstallType" = "CD" ];then
		CDEject
	fi
}
