# openmamba mambawelcome script
# Copyright (c) 2007-2011 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
# Released under the terms of the GNU GPLv3 License
#
VERSION=1.1
TEXTDOMAIN=mambawelcome
TEXTDOMAINDIR=/opt/kde3/share/locale/
INSTALL_DATE=`date +%Y%m%d-%H%M`
INPUT_FILE=/root/.installer.conf
LOG_FILE=/var/log/mambawelcome-$INSTALL_DATE.log
KERNEL_RELEASE=`uname -r`
DCOP=/opt/kde3/bin/dcop

function abort() {
   [ "$DCOPID" ] && {
      $DCOP $DCOPID KommanderIf setText progressText $"Installation aborted; press Next to see the error log."
      $DCOP $DCOPID KommanderIf setText finallabel $"Installation aborted :-("
      $DCOP $DCOPID KommanderIf setEnabled next true
   }
   exit 1
}

function dcop_write() {
   [ "$DCOPID" ] && {
      $DCOP $DCOPID KommanderIf setText progressText "$1"
      $DCOP $DCOPID KommanderIf setText progressBar $2
   }
}


[ "$1" = "-y" ] || {
   echo $"Warning: this script is intended to be run by mambawelcome.kmdr."
   echo $"         Use $0 -y to actually start the operation."
   exit 0
}

DCOPID=$2

echo $"\
openmamba mambawelcome script - version $VERSION
Copyright (c) 2007-2010 by Silvan Calarco <silvan.calarco@mambasoft.it>
"

dcop_write $"Configuring..." 5

# update grub device mappings and config
[ -e /usr/sbin/grub-mkdevicemap ] && /usr/sbin/grub-mkdevicemap -n
/usr/sbin/update-grub

[ -r $INPUT_FILE ] || {
   echo $"Error: cannot read input file $INPUT_FILE; aborting." >&2
   abort
}

. $INPUT_FILE

# now remove sensible information from input file
sed -i "/^INSTALL_ROOT_PASSWORD=.*/d" $INPUT_FILE
sed -i "/^INSTALL_USER_PASSWORD=.*/d" $INPUT_FILE

# restore original kde config location
[ -L /opt/kde/share/config ] && {
   rm -f /opt/kde/share/config
   mkdir -p /opt/kde/share/config
   mv /etc/opt/kde/* /opt/kde/share/config/
   rmdir /etc/opt/kde
   ln -s ../../opt/kde/share/config /etc/opt/kde
}

dcop_write $"Configuring users and host..." 50
echo $"Configuring users and host" | tee -a $LOG_FILE
# configure user
[ "$INSTALL_USER" ] && {
   userdel -rf liveuser
   useradd $INSTALL_USER \
          -c "$INSTALL_USER_FULLNAME" \
          -G audio,filesharing,sysadmin,packager,lpadmin,video,nopermfs || {
      echo $"Error: could not create user; skipping." | tee -a $LOG_FILE
   }
   sh -c "echo $INSTALL_USER_PASSWORD | passwd $INSTALL_USER --stdin" || {
      echo $"Error: could not set user password; aborting." | tee -a $LOG_FILE
      abort
   }

   # configure user for autologin
   if [ "$INSTALL_USER_AUTOLOGIN" = "1" ]; then
      echo $"Info: enabling user autologin" | tee -a $LOG_FILE
      [ -e /opt/kde/share/config/kdm/kdmrc ] && {
         sed -i "s|AutoLoginEnable=.*|AutoLoginEnable=true|" \
            /opt/kde/share/config/kdm/kdmrc
         sed -i "s|AutoLoginUser=.*|AutoLoginUser=$INSTALL_USER|" \
            /opt/kde/share/config/kdm/kdmrc      
      }
      [ -e /etc/lxdm/lxdm.conf ] && {
         sed -i "s|[#[:space:]]*autologin=.*|autologin=$INSTALL_USER|" \
            /etc/lxdm/lxdm.conf
      }
      [ -e /etc/gdm/custom.conf ] && {
         sed -i "s|[#[:space:]]*AutomaticLogin=.*|AutomaticLogin=$INSTALL_USER|" \
            /etc/gdm/custom.conf
         sed -i "s|AutomaticLoginEnable=.*|AutomaticLoginEnable=true|" \
            /etc/gdm/custom.conf
      }
   else
      [ -e /opt/kde/share/config/kdm/kdmrc ] &&
         sed -i "s|AutoLoginEnable=.*|AutoLoginEnable=false|" \
            /opt/kde/share/config/kdm/kdmrc
      [ -e /etc/lxdm/lxdm.conf ] &&
         sed -i "s|[#[:space:]]*\(autologin.*\)|#\1|" \
            /etc/lxdm/lxdm.conf
      [ -e /etc/gdm/custom.conf ] &&
         sed -i "s|AutomaticLoginEnable=.*|AutomaticLoginEnable=false|" \
            /etc/gdm/custom.conf
   fi

   # pre-create user homedir (currently just needed to put TODO txt files on
   # desktop)   
   cp -a /etc/skel /home/$INSTALL_USER
   #mkdir /home/$INSTALL_USER/Desktop
   chmod 711 /home/$INSTALL_USER
   #cp /usr/share/openmamba/installer/*.txt /home/$INSTALL_USER/Desktop/

   cp /usr/share/openmamba/mambabase/mambabase-autostart.desktop /etc/xdg/autostart/mambabase.desktop
   chown $INSTALL_USER /etc/xdg/autostart/mambabase.desktop
   chmod 644 /etc/xdg/autostart/mambabase.desktop

   # create home kde configuration directory
   mkdir -p /home/$INSTALL_USER/.kde4/share/config
   # copy global settings for localizations
   [ -e /root/.kde4/share/config/kdeglobals ] && \
      cp /root/.kde4/share/config/kdeglobals /home/$INSTALL_USER/.kde4/share/config/
   [ -e /root/.kde4/share/config/kxkbrc ] && \
      cp /root/.kde4/share/config/kxkbrc /home/$INSTALL_USER/.kde4/share/config/
   # disable ktip on startup
   cat > /home/$INSTALL_USER/.kde4/share/config/ktiprc << _EOF
[TipOfDay]
RunOnStart=false
_EOF
   # set user permissions
   chown "$INSTALL_USER".users /home/$INSTALL_USER -R
}

# set root password
sh -c "echo $INSTALL_ROOT_PASSWORD | passwd --stdin" || {
      echo $"Error: could not set user password; aborting." | tee -a $LOG_FILE
      abort
   }

# set hostname and domain
[[ "$INSTALL_HOSTNAME" ]] && {
   hostnamectl set-hostname $INSTALL_HOSTNAME
   sed -i "s|HOSTNAME=.*|HOSTNAME=$INSTALL_HOSTNAME|" \
   /etc/sysconfig/network

   [[ "$INSTALL_DOMAIN" ]] || INSTALL_DOMAIN="localdomain"
   sed -i "s|\(127\.0\.0\.1\W*\)\(.*\)|\1 $INSTALL_HOSTNAME.$INSTALL_DOMAIN $INSTALL_HOSTNAME localhost.localdomain localhost|" \
      /etc/hosts
}

# set samba configuration file
[[ "$INSTALL_WORKGROUP" ]] && {
   sed -i "s|workgroup =.*|workgroup = $INSTALL_WORKGROUP|" /etc/samba/smb.conf
   sed -i "s|netbios name =.*|netbios name = $INSTALL_HOSTNAME|" /etc/samba/smb.conf
}

# configure suspend (from suspend rpm post script) and recreate mkinitramfs
grep path_to_resume_device_file /etc/suspend.conf && {
   SWAPLINE=`/sbin/swapon -s |grep -m1 ^/dev/`
   SWAPDEV=${SWAPLINE/ */}
   [ $SWAPDEV ] && sed -i "s|<path_to_resume_device_file>|$SWAPDEV|" /etc/suspend.conf
}
KERNEL_RELEASE=`uname -r`
#mkinitramfs /boot/initramfs-$KERNEL_RELEASE.img $KERNEL_RELEASE -f

dcop_write $"Installation completed." 100
[ "$DCOPID" ] && $DCOP $DCOPID KommanderIf setEnabled next true
echo $"Installation done." | tee -a $LOG_FILE

exit 0
