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

# kiwi doesn't copy /.kconfig from source to build dir
test -f /kconfig && . /kconfig 

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

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

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

#======================================
# Umount kernel filesystems
#--------------------------------------
echo "** Running baseCleanMount..."
baseCleanMount

#======================================
# Clean up kconfig
#--------------------------------------
echo "** Removing kconfig..."
rm /kconfig

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



#=====================================
# setting kdm theme to studio
#-------------------------------------
echo '** Setting kdm theme...'
sed -i 's/DISPLAYMANAGER_KDM_THEME=.*/DISPLAYMANAGER_KDM_THEME=studio/g' /etc/sysconfig/displaymanager

#=====================================
# make the xdm theme a little bit prettier
#-------------------------------------

if [ -e /etc/X11/Xresources ]
then
  echo "xlogin*greeting: Login"                    >> /etc/X11/Xresources
  echo "xlogin*namePrompt: Name: "              >> /etc/X11/Xresources
  echo "xlogin*passwdPrompt: Password: "        >> /etc/X11/Xresources
  echo "xlogin*fail: Failed!"                      >> /etc/X11/Xresources
  echo "xlogin.Login.greetFont: 9x15bold"          >> /etc/X11/Xresources
  echo "xlogin.Login.promptFont: 6x13bold"         >> /etc/X11/Xresources
  echo "xlogin.Login.font: 6x13"                   >> /etc/X11/Xresources
  echo "xlogin.Login.failFont: 6x13"               >> /etc/X11/Xresources
  echo "xlogin*geometry: 300x200"                  >> /etc/X11/Xresources
  echo "xlogin*borderWidth: 1"                     >> /etc/X11/Xresources
  echo "xlogin*frameWidth: 0"                      >> /etc/X11/Xresources
  echo "xlogin*innerFramesWidth: 0"                >> /etc/X11/Xresources
  echo "xlogin*shdColor: black"                    >> /etc/X11/Xresources
  echo "xlogin*hiColor: black"                     >> /etc/X11/Xresources
  echo "xlogin*greetColor: white"                  >> /etc/X11/Xresources
  echo "xlogin*failColor: red"                     >> /etc/X11/Xresources
  echo "xlogin*promptColor: grey75"                >> /etc/X11/Xresources
  echo "xlogin*foreground: grey75"                 >> /etc/X11/Xresources
  echo "xlogin*background: black"                  >> /etc/X11/Xresources
  echo "xlogin*borderColor: grey50 "               >> /etc/X11/Xresources
fi

if [ -e /etc/icewm/preferences ]
then
  sed -i 's#DesktopBackgroundImage=.*#DesktopBackgroundImage="/etc/X11/xdm/BackGround.xpm"#' /etc/icewm/preferences 
fi
if [ -e /etc/X11/xdm/Xsetup ]
then
  sed -i 's#^exit 0$#kill `cat /var/run/xconsole.pid`;/sbin/startproc /usr/bin/icewmbg || /usr/bin/xsetroot -solid lightgray;\nexit 0#g' /etc/X11/xdm/Xsetup 
fi


#======================================
# 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
rpm --import /studio/studio_rpm_key_3
#rm /studio/studio_rpm_key_3
rpm --import /studio/studio_rpm_key_4
#rm /studio/studio_rpm_key_4
rpm --import /studio/studio_rpm_key_5
#rm /studio/studio_rpm_key_5
rpm --import /studio/studio_rpm_key_6
#rm /studio/studio_rpm_key_6

sed --in-place -e 's/icewm/icewm-session/' /usr/bin/wmlist

#======================================
# Setting up overlay files
#--------------------------------------
echo '** Setting up overlay files...'
echo mkdir -p "/"
mkdir -p "/"
echo tar xfp "/image/b34a30877d7368722ae65c1729c50838" -C "/"
tar xfp "/image/b34a30877d7368722ae65c1729c50838" -C "/"
echo rm "/image/b34a30877d7368722ae65c1729c50838"
rm "/image/b34a30877d7368722ae65c1729c50838"
chown root:root //build-custom
chmod +x //build-custom
mkdir /studio
cp /image/.profile /studio/profile
cp /image/config.xml /studio/config.xml
true#======================================
# Configure MySQL database
#--------------------------------------

# Helper function to wait 30s for MySQL socket to appear.
wait_for_socket() {
  local i
  for((i=0; i<150; i++)); do
    sleep 0.2
    test -S $1 && i='' && break
  done
  test -z "$i" || return 1
  return 0
}

# Helper function to execute the given sql file.
execute_sql_file() {
  local socket=$1
  local sql_file=$2
  mysql --socket="$socket" -u root < "$sql_file" 2>&1
}

# Initialize MySQL
echo "## Initializing MySQL databases and tables..."
mysql_install_db --user=mysql

# Start MySQL without networking
echo "## Starting MySQL..."
mkdir -p /var/log/mysql/
socket=/var/run/mysql/mysql.sock
mysqld_safe --skip-networking --user=mysql --pid-file=/tmp/mysqld.pid --socket=$socket &
wait_for_socket $socket || {
  echo "## Error: $socket didn't appear within 30 seconds"
}

# Load MySQL data dump, if it exists
mysql_dump=/tmp/mysql_dump.sql
if [ -f "$mysql_dump" ]; then
  echo "## Loading MySQL data dump..."
  execute_sql_file "$socket" "$mysql_dump"
else
  echo "## No MySQL data dump found, skipping"
fi

# Load MySQL users and permissions, if setup file exists
mysql_perms=/tmp/mysql_config.sql
if [ -f "$mysql_perms" ]; then
  echo "## Loading MySQL users and perms..."
  execute_sql_file "$socket" "$mysql_perms"
else
  echo "## No MySQL user/perms config found, skipping"
fi

# Auto-start MySQL
echo "## Configuring MySQL to auto-start on boot..."
chkconfig mysql on

# Stop MySQL service (for uncontained builds)
echo "## Stopping MySQL..."
mysql_pid=/tmp/mysqld.pid
kill -TERM `cat $mysql_pid`

# Clean up temp files (for uncontained builds)
rm -f "$mysql_perms" "$mysql_dump" "$mysql_pid"

echo "## MySQL configuration complete"

