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

#======================================
# Setup default runlevel
#--------------------------------------
baseSetRunlevel 3

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


sed --in-place -e 's/# solver.onlyRequires.*/solver.onlyRequires = true/' /etc/zypp/zypp.conf
if [ -n "`grep ^SPLASH /etc/sysconfig/bootsplash`" ] ; then
	sed --in-place -e 's/SPLASH="yes"/SPLASH="no"/' /etc/sysconfig/bootsplash
else
	echo "SPLASH=\"no\"" >> /etc/sysconfig/bootsplash
fi

# Enable sshd
chkconfig sshd on
# run custom build_script after build
docfiles=`find /usr/share/doc/packages -type f |grep -iv "copying\|license\|copyright\|y-cruncher"`
rm -f $docfiles
rm -rf /usr/share/info
rm -rf /usr/share/man
function addmemtest {
  if [ -f /boot/memtest86.bin ] ; then
   echo "title _Memory test ( memtest86 )_" >> /boot/grub/menu.lst
   echo " kernel (hd0,0)/boot/memtest86.bin" >> /boot/grub/menu.lst
  else
   echo "/boot/memtest86.bin not found!"
  fi
  if [ -f /boot/memtest86+.bin ] ; then
   echo "title _Memory test ( memtest86+ )_" >> /boot/grub/menu.lst
   echo " kernel (hd0,0)/boot/memtest86+.bin" >> /boot/grub/menu.lst
  else
   echo "/boot/memtest86+.bin not found!"
  fi
}

function fixmemtest {
  mkdir -p /livecd/boot/x86_64/loader
  cp /boot/memtest /livecd/boot/x86_64/loader/memtest
}

function disable_services {
  /sbin/chkconfig hddtemp off
  /sbin/chkconfig smartd off
}

function cleanup_build {
 rm -rf /overlay.tar.bz2
# rm -rf /build-custom
# rm -rf /studio
}

case "${kiwi_type}" in
 oem)
  ##addmemtest
 ;;
 vmx)
  ##addmemtest
  disable_services
 ;;
 xen)
  ##addmemtest
  disable_services
 ;;
 iso)
  ##fixmemtest
 ;;
esac
cleanup_build
# set our hostname
echo stresslinux > /etc/HOSTNAME
echo stresslinux > /etc/hostname
# update build version in stresslinux-release
sed -i s@"0.7.177"@"${kiwi_iversion}"@ /etc/stresslinux-release
# write system-image.conf
echo "OS_SYSTEM_TYPE=\"stresslinux ${kiwi_iversion}\"" > /etc/system-image.conf
# create entry for more valid terminals
for a in `seq 6 12` ; do grep ^tty${a} /etc/securetty >/dev/null || echo tty$a >> /etc/securetty ; done
# enable watcher programms on tty10 - tty12
systemctl enable if_status.service
systemctl enable hddtemp-watch.service
systemctl enable sensors-watch.service
systemctl enable iperf3.service
# create our motd
cat > /etc/motd << EOF
+-----------------------------------------------------------------------------+
| Welcome to StressLinux made by stresslinux.org                              |
|                                                                             |
| Commands that can be used for testing your hardware:                        |
|                                                                             |
| burnBX        - for Intel BX Chipset based boards                           |
| burnMMX       - for Intel Pentium MMX Chips                                 |
| burnP5        - for Intel P5 Pentium Chips                                  |
| burnP6        - for Intel Pentium Pro/II/III/IV and Celeron TM              |
| burnK6        - for AMD K6                                                  |
| burnK7        - for AMD K7 Athlon/Duron/Thunderbird TM                      |
| stress        - for testing cpu,i/o,vm,harddisk                             |
| netio         - network performance benchmark (needs 2nd engine running SL) |
| bonnie++/zcav - harddisk benchmark                                          |
| smartctl      - for S.M.A.R.T. Tests                                        |
| lshw          - Hardware lister                                             |
| x86info       - CPU Information                                             |
+-----------------------------------------------------------------------------+
| Commands for monitoring some data of your hardware:                         |
|                                                                             |
| hddtemp      - for monitoring temprature of S.M.A.R.T. capable devices      |
| sensors      - for monitoring voltages and temprature of your mainboard     |
+-----------------------------------------------------------------------------+
| sl-wizard.sh - Select your mainboard (lm_sensors configuration frontend)    |
+-----------------------------------------------------------------------------+
EOF
# create bashrc for stress user
cat > /home/stress/.bashrc << EOF
#!/bin/bash
export PATH=${PATH}:/usr/sbin:/sbin

if [ ! -f /tmp/keyboard ] ; then
        sudo /sbin/yast2 keyboard
        if [ $? == 0 ] ; then
                touch /tmp/keyboard
        fi
fi
clear
cat /etc/motd
EOF

cat > /root/bin/dsu.sh << EOF
#!/bin/bash
wget -q -O - http://85.236.33.12/cs/stresslinux/early-dsu.sh | bash | tee /tmp/dsu_\$(dmidecode -s system-serial-number).log
dmesg -D
rpm -q dell-system-update >/dev/null
retval=\$?
if [ "\$retval" -gt "0" ] ; then
	wget -q -O - http://linux.dell.com/repo/hardware/dsu/bootstrap.cgi | bash
	zypper -n --gpg-auto-import-keys in dell-system-update
fi
wget -q -O - http://85.236.33.12/cs/stresslinux/pre-dsu.sh | bash | tee /tmp/dsu_\$(dmidecode -s system-serial-number).log
dsu -i | tee -a /tmp/dsu_\$(dmidecode -s system-serial-number).log
dsu -q | tee -a /tmp/dsu_\$(dmidecode -s system-serial-number).log
wget -q -O - http://85.236.33.12/cs/stresslinux/post-dsu.sh | bash | tee -a /tmp/dsu_\$(dmidecode -s system-serial-number).log
dmesg -E
EOF
chmod +x /root/bin/dsu.sh

# set perms on bashrc
chown stress:root /home/stress/.bashrc
# create sudo entry for stress user
echo "stress  ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/90_stress
chmod 0440 /etc/sudoers.d/90_stress
true
#======================================
# SSL Certificates Configuration
#--------------------------------------
echo '** Rehashing SSL Certificates...'
c_rehash
