#!/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

# File /etc/SuSE-release is deprecated and will be removed
# in a future service pack or release. Therefore we would
# like to use /etc/os-release about details release,
# but for SLES 11 SP3 /etc/os-release doesn't exist.
function is_suse() {
    [ -f /etc/SuSE-release ] || [ -f /etc/os-release ]
}

function suse_ver() {
    local ver=$1
    local suse_ver=0

    if is_suse; then
        if [ -f /etc/os-release ]; then
            . /etc/os-release
            suse_ver=${VERSION%-SP[0-9]}
        else
            suse_ver=`cat /etc/SuSE-release  | awk '/VERSION/ {print $3}'`
        fi
    fi

    [ "$suse_ver" -eq "$ver" ]
}

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

#==========================================
# setup build day
#------------------------------------------
baseSetupBuildDay

#======================================
# Mount system filesystems
#--------------------------------------
baseMount

#==========================================
# remove unneded kernel files
#------------------------------------------
suseStripKernel
baseStripLocales en_US.utf-8 C.utf8

#==========================================
# remove unneeded files
#------------------------------------------
# Seems to not work with the SLES kiwi - result doesn't boot :/
# suseStripInitrd

#======================================
# Setup baseproduct link
#--------------------------------------
suseSetupProduct

#======================================
# Add missing gpg keys to rpm
#--------------------------------------
suseImportBuildKey

#======================================
# Activate services
#--------------------------------------
suseInsertService sshd
if suse_ver 12; then
    suseInsertService sleshammer
else
    suseInsertService boot.device-mapper
    suseRemoveService avahi-dnsconfd
    suseRemoveService avahi-daemon
fi

#======================================
# Setup default target, multi-user
#--------------------------------------
baseSetRunlevel 3

echo 'DEFAULT_TIMEZONE="UTC"' >> /etc/sysconfig/clock
baseUpdateSysConfig /etc/sysconfig/clock HWCLOCK "-u"
baseUpdateSysConfig /etc/sysconfig/clock TIMEZONE UTC
baseUpdateSysConfig /etc/sysconfig/network/dhcp DHCLIENT_SET_HOSTNAME no
baseUpdateSysConfig /etc/sysconfig/network/dhcp WRITE_HOSTNAME_TO_HOSTS no

#==========================================
# remove package docs
#------------------------------------------
rm -rf /usr/share/doc/packages/*
rm -rf /usr/share/doc/manual/*
rm -rf /opt/kde*

#======================================
# only basic version of vim is
# installed; no syntax highlighting
#--------------------------------------
sed -i -e's/^syntax on/" syntax on/' /etc/vimrc

#======================================
# SuSEconfig
#--------------------------------------
suseConfig
if suse_ver 11; then
    insserv sleshammerstartup
fi

#======================================
# Fix DHCP config
#--------------------------------------
function setsc()
{
    # $1 = file, $2 = varname, $3 = value
    # regex for zero or more spaces
    local S='[[:space:]]*'
    sed -i -r -e "s%^($S$2$S=$S).*%\1\"$3\"%" "$1"
}

if suse_ver 11; then
    setsc /etc/sysconfig/network/dhcp DHCLIENT_SET_HOSTNAME yes
fi

#======================================
# Remove yast if not in use
#--------------------------------------
suseRemoveYaST

if suse_ver 11; then
    rpm -e --nodeps ruby
    ln -sf /usr/bin/ruby.ruby2.1 /usr/bin/ruby
    rm etc/systemd -fr
elif suse_ver 12; then
    rm etc/init.d/sleshammerstartup
fi

rpm -e cron logrotate --nodeps
rpm -e glibc-locale xz bzip2 openssl dirmngr gpg2 \
    gfxboot gfxboot-branding-SLE perl-HTML-Tagset --nodeps

#======================================
# Remove package manager
#--------------------------------------
suseStripPackager

#rm -f usr/lib/perl5/*/*/auto/Encode/??/??.so # 9MB

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

ln -s /sbin/init /init

#==========================================
# umount
#------------------------------------------
umount /proc >/dev/null 2>&1

exit 0
