#!/bin/bash -e
#
# Copyright (c) [2013] 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).
#

# read in some variables
test -f /.profile && . /.profile

# read in KIWI utility functions
test -f /.kconfig && . /.kconfig



echo "build-custom_csp-appliance started"

#Setup /etc/products.d to use csp appliance channel
cp /etc/products.d/Leap.prod /root/Leap.prod
rm /etc/products.d/baseproduct
rm /etc/products.d/Leap.prod
cp /etc/products.d/csp.prod.tmp /etc/products.d/csp.prod
ln -s /etc/products.d/csp.prod /etc/products.d/baseproduct

#Update stylesheet used in Yast for install
mv /opt/novell/va_install/logo.png /usr/share/YaST2/theme/current/wizard/logo.png
mv /opt/novell/va_install/inst_checkbox-off.png /usr/share/YaST2/theme/current/wizard/inst_checkbox-off.png
mv /opt/novell/va_install/inst_checkbox-on.png /usr/share/YaST2/theme/current/wizard/inst_checkbox-on.png

sed -i 's/inst_checkbox-off.svg/inst_checkbox-off.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/inst_checkbox-on.svg/inst_checkbox-on.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/inst_radio-button-unchecked.svg/inst_radio-button-unchecked.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/inst_radio-button-checked.svg/inst_radio-button-checked.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/inst_radio-button-unchecked-disabled.svg/inst_radio-button-unchecked.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/inst_radio-button-checked-disabled.svg/inst_radio-button-checked.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/logo.svg/logo.png/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/min-width: 210px/min-width: 115px/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/min-width: 180px/min-width: 115px/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/margin-top:-15px/margin-top: -12px/g' /usr/share/YaST2/theme/current/wizard/installation.qss
sed -i 's/padding: 80px 2em 1em 0.5em/padding: 80px 2em 0em 0.5em/g' /usr/share/YaST2/theme/current/wizard/installation.qss

#Make sure Yast updates the correct issue file, /etc/issue
sed -i 's/\/usr\/lib\/issue.d\/10-APPLIANCE/\/etc\/issue/g' /usr/share/YaST2/modules/NovellVainit.rb

#Remove Nokogiri warning from keyboard list output
sed -i 's/y2base keyboard -S '\''(list)'\'' UI 2>&1/y2base keyboard -S '\''(list)'\'' UI 2\>\&1 \| grep -v '\''^WARNING: Nokogiri'\''/' /usr/share/YaST2/modules/NovellVainit.rb

#Remove Nokogiri warning from LAN cards list output
sed -i 's/y2base lan -S '\''(list)'\'' UI/y2base lan -S '\''(list)'\'' UI 2\>\&1 \| grep -v '\''^WARNING: Nokogiri'\'' \>\&2/' /usr/share/YaST2/modules/NovellVainit.rb

sed -i "s/local DPI=\`ruby -e \"puts ((\$MON_WIDTH_PX\/(\$MON_WIDTH_MM\*0\.0393701)\/48)\.round)\*48\"\`/local DPI=0/" /usr/lib/YaST2/startup/YaST2.call

#Remove mac address from configuration dialog
sed -i '/Left(Label(macAddrLabel)),/d' /usr/share/YaST2/include/novell-vainit/dialogs.rb

## Change font Size in stylesheet in Yast for install
## Modify CAF's file and re-run it
sed -i 's/font: 24pt/font-size: 18pt/g' /opt/novell/base_config/reduceYaSTFontSize.py
sed -i 's/24/18/g' /opt/novell/base_config/reduceYaSTFontSize.py
python3 /opt/novell/base_config/reduceYaSTFontSize.py

# Remove files created by the upgrade-available packages
rm -rf /opt/novell/product_upgrade
rm -rf /opt/novell/os_upgrade

# Set default editor to nano
echo 'export EDITOR=nano' >> /etc/profile

# Set JAVA_HOME environment variable in the vabase-datamodel.service configuration file
sed -i "/^\[Service\]/a Environment=\"JAVA_HOME=$(readlink -f /etc/alternatives/java | sed 's:/bin/java::')\"" /usr/lib/systemd/system/vabase-datamodel.service

# Create rpc group if it doesn't already exist
getent group rpc > /dev/null || groupadd rpc
#  Add the rpc user to the group if it doesn't already belong
id -nG rpc | grep -q rpc || usermod -g rpc rpc

# Configure Mesa to always use software rendering
sed -i '/^Environment=/ s/$/ LIBGL_ALWAYS_SOFTWARE=1/' /usr/lib/systemd/system/YaST2-Firstboot.service

#If you don't see this in /var/opt/novell/va/logs/va_firstboot.log then one of the above commands returned an error
echo "build-custom_csp-appliance finished"

