#!/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 baseproduct link
#--------------------------------------
suseSetupProduct


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

#======================================
# 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
rpm --import /studio/studio_rpm_key_7
rm /studio/studio_rpm_key_7
rpm --import /studio/studio_rpm_key_8
rm /studio/studio_rpm_key_8
rpm --import /studio/studio_rpm_key_9
rm /studio/studio_rpm_key_9
rpm --import /studio/studio_rpm_key_10
rm /studio/studio_rpm_key_10
rpm --import /studio/studio_rpm_key_11
rm /studio/studio_rpm_key_11
rpm --import /studio/studio_rpm_key_12
rm /studio/studio_rpm_key_12
rpm --import /studio/studio_rpm_key_13
rm /studio/studio_rpm_key_13

#======================================
# Setting up overlay files 
#--------------------------------------
echo '** Setting up overlay files...'
echo mkdir -p /
mkdir -p /
echo tar xfp /image/ac040e37efe8adadcd3fc761aa611987 -C /
tar xfp /image/ac040e37efe8adadcd3fc761aa611987 -C /
echo rm /image/ac040e37efe8adadcd3fc761aa611987
rm /image/ac040e37efe8adadcd3fc761aa611987
mkdir -p /root/
mv /studio/overlay-tmp/files/root/add_sle_repos.sh /root/add_sle_repos.sh
chown root:root /root/add_sle_repos.sh
chmod 700 /root/add_sle_repos.sh
mkdir -p /root/
mv /studio/overlay-tmp/files/root/fix_perm.sh /root/fix_perm.sh
chown root:root /root/fix_perm.sh
chmod 700 /root/fix_perm.sh
mkdir -p /root/
mv /studio/overlay-tmp/files/root/add_epbx_repos.sh /root/add_epbx_repos.sh
chown root:root /root/add_epbx_repos.sh
chmod 700 /root/add_epbx_repos.sh
mkdir -p /root/
mv /studio/overlay-tmp/files/root/freepbx_baseinstall.sh /root/freepbx_baseinstall.sh
chmod 700 /root/freepbx_baseinstall.sh
chown root:root /build-custom
chmod +x /build-custom
# run custom build_script after build
test -d /studio || mkdir /studio
cp /image/.profile /studio/profile
cp /image/config.xml /studio/config.xml
/build-custom
rm -rf /studio/overlay-tmp
if [ -s /etc/modprobe.d/unsupported-modules ] ; then
        echo "Enabling unsupported modules ..."
        sed -i s@"allow_unsupported_modules 0"@"allow_unsupported_modules 1"@ /etc/modprobe.d/unsupported-modules
fi
if [ -s /etc/dahdi/modules ] ; then
        echo "Modifing dahdi modules ..."
        sed -i s@"^"@"#"@g /etc/dahdi/modules
        if [ -z "`grep ^dahdi /etc/dahdi/modules`" ] ; then
                echo "Enabling dahdi module ..."
                echo dahdi >> /etc/dahdi/modules
        fi
fi
if [ -n "`grep ^gfxmenu /boot/grub/menu.lst`" ] ; then
        sed -i s@"^gfxmenu"@"#gfxmenu"@g /boot/grub/menu.lst
fi
suseConfig
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"

