#!/bin/bash
#================
# FILE          : config.sh
#----------------
# 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   : configuration script for SUSE based
#               : operating systems
#               :
#               :
# STATUS        : BETA
#----------------
#======================================
# Functions...
#--------------------------------------
test -f /.kconfig && . /.kconfig
test -f /.profile && . /.profile

#======================================
# Greeting...
#--------------------------------------
echo "Configure image: [$name]..."

#======================================
# SuSEconfig
#--------------------------------------
echo "** Running suseConfig..."
suseConfig

echo "** Running ldconfig..."
/sbin/ldconfig

#======================================
# RPM GPG Keys Configuration
#--------------------------------------
#echo '** Importing GPG Keys...'
#rpm --import /studio/studio_rpm_key_0
#rm /studio/studio_rpm_key_0
#rpm --import /studio/studio_rpm_key_1
#rm /studio/studio_rpm_key_1
#rpm --import /studio/studio_rpm_key_2
#rm /studio/studio_rpm_key_2

sed --in-place -e 's/# solver.onlyRequires.*/solver.onlyRequires = true/' /etc/zypp/zypp.conf

# Enable sshd
chkconfig sshd on
# enable rpcbind
chkconfig rpcbind on
chkconfig splash off
chkconfig splash_early off
#systemctl enable image-installer.service
#
#cat > /etc/systemd/system/after-local.service << EOF
##  This file is part of systemd.
##
##  systemd is free software; you can redistribute it and/or modify it
##  under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
#
#[Unit]
#Description=/etc/init.d/after.local Compatibility
#ConditionFileIsExecutable=/etc/init.d/after.local
#
#[Service]
#Type=oneshot
#ExecStart=/etc/init.d/after.local
#TimeoutSec=0
#StandardOutput=tty
#RemainAfterExit=yes
#SysVStartPriority=99
#
#[Install]
#WantedBy=multi-user.target
#EOF
#systemctl enable after-local.service
#echo "chvt 12" >> /etc/init.d/after.local
#
echo "12:2345:respawn:/sbin/agetty -n -L -l /usr/sbin/bios-installer 115200 tty12" >> /etc/inittab

mkdir -p /etc/sysconfig/network/scripts
cat > /etc/sysconfig/network/scripts/dsu-up.sh << EOF
#!/bin/bash
SCRIPTNAME=\$0

function add_dsu_repo {
	WGET=\$(which wget 2>/dev/null)
	BASH=\$(which bash 2>/dev/null)
	if [ -x "\${WGET}" -a -x "\${BASH}" ] ; then
		\${WGET} -q -O - http://linux.dell.com/repo/hardware/dsu/bootstrap.cgi | \${BASH}
	fi
}

function install_dsu {
	ZYPPER=\$(which zypper 2>/dev/null)
	if [ -x "\${ZYPPER}" ] ; then
		\${ZYPPER} -n in dell-system-update
	fi
}

case \${SCRIPTNAME} in
*if-up.d*)
	if [ -f /root/add_sle_repos.sh ] ; then
		chmod +x /root/add_sle_repos.sh
		/root/add_sle_repos.sh
	fi
        add_dsu_repo
	install_dsu
;;
*if-down.d*)
        :
;;
esac
EOF

chmod +x /etc/sysconfig/network/scripts/dsu-up.sh
ln -s /etc/sysconfig/network/scripts/dsu-up.sh /etc/sysconfig/network/if-up.d/dsu


# bios installer config
cat > /etc/bios-installer.conf << EOF
TITLE="BIOS-INSTALLER by LINUX-ADMINISTRATOR.COM (c) 2o15"
BASEMOUNT="/mnt/source"
# IMAGELOCATION can be NFS, DIR
IMAGELOCATION="NFS"
IMAGELOCATION_NFS="85.236.33.12:/home/bios-installer-repo"
IMAGELOCATION_DIR="/image-installer-img"
DEBUG=1
EOF

# cleanup
baseStripInfos
baseStripLocales en_US
baseStripMans

test -d /studio || mkdir /studio
cp /image/.profile /studio/profile
cp /image/config.xml /studio/config.xml
rm -rf /studio/overlay-tmp
echo "Checking for qemu-img ..."
type -p qemu-img
echo "Checking for PATH environment ..."
echo $PATH
true

#======================================
# Umount kernel filesystems
#--------------------------------------
baseCleanMount
