#!/bin/bash -e
#
#
# Copyright (c) [2013-2022] Novell, Inc.  All Rights Reserved.
#
# THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.  IT
# MAY NOT BE USED, COPIED, DISTRIBUTED, DISCLOSED, ADAPTED, PERFORMED, DISPLAYED,
# COLLECTED, COMPILED, OR LINKED WITHOUT NOVELL'S PRIOR WRITTEN CONSENT.  USE OR
# EXPLOITATION OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT THE PERPETRATOR
# TO CRIMINAL AND CIVIL LIABILITY.
#
# NOVELL PROVIDES THE WORK "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
# INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, AND NON-INFRINGEMENT. NOVELL, THE AUTHORS OF THE WORK, AND
# THE OWNERS OF COPYRIGHT IN THE WORK ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
# FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN
# THE WORK.
#
# This script is executed at the end of appliance creation.  Here you can do
# one-time actions to modify your appliance before it is ever used, like
# removing files and directories to make it smaller, creating symlinks,
# generating indexes, etc.
#
# The 'kiwi_type' variable will contain the format of the appliance (oem =
# disk image, vmx = VMware, iso = CD/DVD, xen = Xen).
#

# define alias to python based on the below checks
[[ -f /usr/bin/python3 ]] && alias python=/usr/bin/python3 || { [[ -f /usr/bin/python2 ]] && alias python=/usr/bin/python2 || { [[ -f /usr/bin/python ]] && alias python=/usr/bin/python; }; }
shopt -s expand_aliases

echo " Echo Start of build-custom"

# read in some variables
echo " Echo execute profile"
test -f /.profile && . /.profile

# read in KIWI utility functions
echo "Echo execute kconfig"
test -f /.kconfig && . /.kconfig

SLESVER=$(. /etc/os-release; echo $VERSION_ID | cut -f 1 -d .)

#This removes the base SuSE license
echo "Echo remove license.txt"
  rm /etc/YaST2/licenses/license.txt || true

#Remove SuSE branding

if [ "${SLESVER}" -le "11" ]; then
  rm /usr/share/YaST2/theme/SLE/wizard/logo.png || true
fi

### SLES 12 setup launch of vabase yast module
#Put in our firstboot this is what causes the novell-yast-vainit package to execute
  echo "Echo Fixup firstboot.xml"
  rm /etc/YaST2/firstboot.xml
  cp /etc/va_config/firstboot.xml /etc/YaST2
# Create reconfig_system to kick off firstboot
echo "Echo touch reconfig_system"
  touch /var/lib/YaST2/reconfig_system || true

### SLES 12 workaround an issue with clock not getting created at install time with Studio
cp /etc/va_config/clock /etc/sysconfig/clock

### SLES12 Specific configuration
# Set runlevel to init3
echo "Echo Set runlevel to int3"
 ln -sf /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target


#clear the runlevel 3 login screen
echo "Echo Clear etc issue"
  clear > /etc/issue

#Modify PermitRootLogin to default to no when remark is removed. 
echo "Echo modify sshd_config"
  sed -i -e 's|#PermitRootLogin yes|#PermitRootLogin no|' /etc/ssh/sshd_config
  python /opt/novell/base_config/setsshd_config.py

#Setup defaults on /etc/sysconfig/services to not restart services for the appliance on updates
  python /opt/novell/base_config/setsysconfig_services.py

#Jetty section
 # Call script to set jetty session-timeout to 2 hours
 echo "Echo set jetty session timout"
 python /opt/novell/base_config/updateSessionTimeoutJetty.py 30

# Firewall isn't enabled by default on beta versions of SLES 15. And public.xml doesn't exist,
# so let's create it by adding the ssh service
if [ "${SLESVER}" -ge "15" ]; then
    systemctl enable firewalld
    systemctl start firewalld
    firewall-cmd --runtime-to-permanent
fi

#VA third party Material location
echo "Echo Create Directory for thirdpartymaterials"
mkdir -p /etc/va_config/thirdpartymaterials

### Create a swap (TODO for SLES12)
echo "Echo Create swap"
if [ "${SLESVER}" -le "11" ]; then
 dd if=/dev/zero of=/swapfile bs=1024 count=2097152
 mkswap /swapfile
 chown root:root /swapfile
 chmod 0600 /swapfile
fi

#Call script to add needed tweaks to /etc/init.d/sshd rc script
#SLES11
echo "Echo Setup sshd script (TODO for SLES12)"
if [ "${SLESVER}" -le "11" ]; then
 python /opt/novell/base_config/updateRcSshd.py
fi

 
#Enable vabase-firstboot.service (MOved to be launched after yastfirstboot completed.
#SLES12
echo "Echo enable vabase-firstboot.service"
systemctl enable vabase-firstboot.service

#Disable sshd.service 
#SLES12
echo "Echo disable sshd.service"
systemctl disable sshd.service



#Change installation.qss theme to style.qss (This is for testing purposes)
#SLES12
#if grep -Fxq "VERSION = 12" /etc/SuSE-release
# then
#  cp /usr/share/YaST2/theme/SLE/wizard/installation.qss /usr/share/YaST2/theme/SLE/wizard/installation.qss.org
 # cp /opt/novell/vabase_install/installation.qss /usr/share/YaST2/theme/SLE/wizard/installation.qss
#fi

 
#Setup va_expand_disk to run at shutdown if it exists (TODO fix for SLES12)
if grep -Fxq "VERSION = 11" /etc/SuSE-release
 then
 echo "if [ -f /opt/novell/base_config/va_expand_disk ];" >> /etc/init.d/halt.local
 echo "then" >> /etc/init.d/halt.local
 echo "/opt/novell/base_config/va_expand_disk" >> /etc/init.d/halt.local 
 echo "rm /opt/novell/base_config/va_expand_disk" >> /etc/init.d/halt.local 
 echo "fi" >> /etc/init.d/halt.local
fi

#Disable IPv6 (TODO BDG Not working for SLES12 need to fix)
if grep -Fxq "VERSION = 11" /etc/SuSE-release
 then
echo "alias net-pf-10 off" >> /etc/modprobe.conf.local
echo "alias ipv6 off" >> /etc/modprobe.conf.local
fi

#Create a Datafile of all packages installed at build time in /opt/novell/base_config/buildTimePackages.txt
echo "Echo Create List of RPMS at build time"
mkdir -p /opt/novell/base_config
/bin/rpm -qa | sort  > /opt/novell/base_config/buildTimeRPMs

#Get build number from /etc/vabuildnumber if it exists and put it in /etc/Novell-VA-release
echo "Echo update build Number"
python /opt/novell/base_config/updateBuildNumber.py

#Get type of virtualizaition platform system was build for.
echo "Echo Get virtualization platform"
if [ -f /opt/novell/base_config/buildformat.ovf ];
then
    rm /opt/novell/base_config/buildformat.ovf
    echo buildformat=ovf > /opt/novell/base_config/buildformat 
    chmod 664 /opt/novell/base_config/buildformat

fi
if [ -f /opt/novell/base_config/buildformat.vhd ];
then
    rm /opt/novell/base_config/buildformat.vhd
    echo buildformat=vhd > /opt/novell/base_config/buildformat 
    chmod 664 /opt/novell/base_config/buildformat

fi
if [ -f /opt/novell/base_config/buildformat.xen ];
then
    rm /opt/novell/base_config/buildformat.xen
    echo buildformat=xen > /opt/novell/base_config/buildformat 
    chmod 664 /opt/novell/base_config/buildformat
    #Bug in Studio temp workaround
    #cp -R /usr/lib/grub2/x86_64-xen /boot/grub2/


fi


#Grub Settings SLES12 
 echo "Echo Set Grub nomodeset for firstboot bug 997455 workaround"
 sed -i -e 's|quiet|nomodeset quiet|' /image/config.xml

##Apache section SLES12
#  # Set apache to start on boot for SLES12
#  echo "Echo enable apache2.service"
# systemctl enable apache2.service
# #addphp5 module to /etc/sysconfig/apache
# phpver=$(basename $(ls /etc/apache2/conf.d/php*.conf) | cut -d. -f1)
# if [ ! -z "${phpver}" ]; then
#     echo "Echo php module for apache"
#    python /opt/novell/base_config/updateApacheModules.py ${phpver}
# fi

#Set defaults in automatic_online_update file
 python /opt/novell/base_config/setAutoOnlineUpdate.py


if [ "${SLESVER}" -ge "15" ]; then
 echo "Echo enable chronyd.service"
 systemctl enable chronyd.service
else
 echo "Echo enable ntpd.service"
 systemctl enable ntpd.service
fi

#Enabale Suse Studio firstboot 
# echo "Echo enable suse-studio-firstboot.service"
#Debug Make a copy of suse-studio-firstboot.service
 echo "Echo copy suse-studio-firstboot.service"
cp /etc/systemd/system/suse-studio-firstboot.service /root/suse-studio-firstboot.service
#echo "Echo locate rpm that contains suse-studio-firstboot.service"
#rpm -qf /etc/systemd/system/suse-studio-firstboot.service > /root/location
# systemctl enable suse-studio-firstboot.service


## Change font Size in stylesheet in Yast for install 
python /opt/novell/base_config/reduceYaSTFontSize.py

#Make sure the root of the file system is owned by root
chown root:root /

#Call appliance specific Custom-build script
echo "Echo Call appliance specific custom-build script"
python /opt/novell/base_config/execApplianceScript.py /opt/novell/va_install/build-custom

echo "Echo build-custom finished"
