#!/bin/bash
#
#########################################
#
# gnome-session-wrapper - establishes
#                         customised user
#                         environment
#                         upon logging 
#                         into GNOME
#
# Written: Naaman Campbell
#          2 December 2005
#
# Updated: Naaman Campbell
#	   16 December 2005
#
#########################################

#########################################
# XSCREENSAVER
# - ensure users cannot alter
#   xscreensaver settings
#########################################

GLOBALXSS=/etc/X11/app-defaults/XScreenSaver
USERXSS=~/.xscreensaver

# check if file exists
if [ -e $USERXSS ]; then
  # check if file is a symlink
  if [ -h $USERXSS ]; then
    CONF=`ls -l $USERXSS | sed -e "s/.*\.xscreensaver -> //g"`
    # check if symlink points to correct location
    if [ $GLOBALXSS != $CONF ]; then
      rm $USERXSS
    fi
  else
    # file is not a symlink
    rm $USERXSS
  fi
fi

# check if symlink exists
if [ ! -h $USERXSS ]; then
  ln -s $GLOBALXSS $USERXSS
fi

#########################################
# END XSCREENSAVER
#########################################

#########################################
# EVOLUTION
# - migrate users from mozilla mail to 
#   evolution
#########################################
 
ERRORMSG=~/moztoevoerr.`date +%F`
ERRORRECIPIENT=sysadmin@company.com.au
ERRORSUBJECT="Non-standard Mozilla profile"
EVODIR=~/.evolution/mail/local
SKIPEVO=0

# evolution mail directory exists
if [ -d $EVODIR ]; then
  # further processing is not necessary
  SKIPEVO=1
fi

function emailerrors {

  # error message file contains data
  if [ -s $ERRORMSG ]; then
    mailx -s "$ERRORSUBJECT" $ERRORRECIPIENT < $ERRORMSG
    rm $ERRORMSG
    SKIPEVO=1
  fi

}

# check if more than one profile exists
PROFILE_COUNT=`find ~/.mozilla/* -type d | grep slt | cut -f5 -d "/" | uniq | wc -l`

if [ $PROFILE_COUNT -gt 1 ]; then
  echo "$USER has too many profiles - manual migration needed" >> $ERRORMSG
  emailerrors
fi

# obtain mozilla directory path
PROFILEDIR=`find ~/.mozilla/* -type d | grep slt | cut -f5 -d "/" | uniq`
SLTDIR=`find ~/.mozilla/* -type d | grep slt | cut -f6 -d "/" | uniq`

# check for email directory
EMAILDIR=~/.mozilla/$PROFILEDIR/$SLTDIR/Mail

if [ ! -d $EMAILDIR ]; then
  # no email directory, so skipping
  SKIPEVO=1
fi

if [ $SKIPEVO -eq 0 ]; then

# initialise evolution profile settings

ACCNAME="$USERNAME on mail"
DOMAIN=company.com.au
EMAIL="$USERNAME@$DOMAIN"
MAILSERVER=mail.$DOMAIN
OUTGOINGMAIL=mailout.$DOMAIN
PREFS=~/.mozilla/$PROFILEDIR/$SLTDIR/prefs.js

# obtain Mozilla prefs ID number
MOZID=`grep -e "identity\.id.*\.useremail\"\,\ \"$USERNAME\@$DOMAIN\"" $PREFS | sed -e 's/.*\.\(id.\)\..*/\1/g'`

# obtain user Full Name
NAME=`grep -e "identity\.$MOZID\.fullName" $PREFS | sed -e 's/.*fullName\"\,\ \"\(.*\)\".*/\1/g'`
if [ -z $NAME ]; then
  # fullName field is empty
  # obtain from GECOS field in passwd database
  NAME=`getent passwd | grep $USERNAME | cut -d: -f5`
fi

# create evolution profile
mkdir -p $EVODIR

# copy emails - maintaining folder structure
cp -pr $EMAILDIR/$MAILSERVER/* $EVODIR/.

# remove unnecessary files
find $EVODIR -name "*.msf" -exec rm {} \;
find $EVODIR -name "*.dat" -exec rm {} \;
find $EVODIR -name "*.html" -exec rm {} \;

# generate random numbers for evolution profile
UIDPT1=`echo $RANDOM$RANDOM$RANDOM | sed -e 's/^\(..........\).*/\1/g'`
UIDPT2=`echo $RANDOM$RANDOM$RANDOM | sed -e 's/^\(.....\).*/\1/g'`
PROFILEUID=$UIDPT1.$UIDPT2.1

ABOOKUIDPT1=`echo $RANDOM$RANDOM$RANDOM | sed -e 's/^\(..........\).*/\1/g'`
ABOOKUIDPT2=`echo $RANDOM$RANDOM$RANDOM | sed -e 's/^\(.....\).*/\1/g'`
ABOOKUID=$ABOOKUIDPT1.$ABOOKUIDPT2

# clear previous configuration
gconftool-2 --recursive-unset /apps/evolution

# setup mail account
gconftool-2 --type list --set /apps/evolution/mail/accounts --list-type string "[<?xml version=\"1.0\"?>\
<account name=\"$ACCNAME\" uid=\"$PROFILEUID@$HOSTNAME\" enabled=\"true\"><identity><name>$NAME</name><addr-spec>$EMAIL</addr-spec><signature uid=\"\"/></identity><source save-passwd=\"true\" keep-on-server=\"false\" auto-check=\"true\" auto-check-timeout=\"5\"><url>pop://$USERNAME@$MAILSERVER/;use_ssl=never</url></source><transport save-passwd=\"false\"><url>smtp://$USERNAME@$OUTGOINGMAIL/;use_ssl=never</url></transport><drafts-folder>mbox:$HOME/.evolution/mail/local#Drafts</drafts-folder><sent-folder>mbox:$HOME/.evolution/mail/local#Sent</sent-folder><auto-cc always=\"false\"><recipients></recipients></auto-cc><auto-bcc always=\"false\"><recipients></recipients></auto-bcc><receipt-policy policy=\"never\"/><pgp encrypt-to-self=\"false\" always-trust=\"false\" always-sign=\"false\" no-imip-sign=\"false\"/><smime sign-default=\"false\" encrypt-default=\"false\" encrypt-to-self=\"false\"/></account>]"

gconftool-2 --set /apps/evolution/mail/default_account --type string "$PROFILEUID@$HOSTNAME"

# setup address books
gconftool-2 --type list --set /apps/evolution/addressbook/sources --list-type string "[<?xml version="1.0"?>\
<group uid=\"$ABOOKUID.0@$HOSTNAME\" name=\"On This Computer\" base_uri=\"file://$HOME/.evolution/addressbook/local\" readonly=\"no\"><source uid=\"$ABOOKUID.1@$HOSTNAME\" name=\"Personal\" relative_uri=\"system\"><properties><property name=\"completion\" value=\"true\"/></properties></source></group>\
,<?xml version=\"1.0\"?>\
<group uid=\"$ABOOKUID.2@$HOSTNAME\" name=\"On LDAP Servers\" base_uri=\"ldap://\" readonly=\"no\"><source uid=\"$ABOOKUID.3@$HOSTNAME\" name=\"Company Name Address Book\" relative_uri=\"dir.$DOMAIN:389/o=People??one\"><properties><property name=\"timeout\" value=\"3\"/><property name=\"limit\" value=\"100\"/><property name=\"completion\" value=\"true\"/></properties></source></group>]"

gconftool-2 --set /apps/evolution/addressbook/display/primary_addressbook --type string "$ABOOKUID.1"

# end evolution if statement
fi

#########################################
# END EVOLUTION
#########################################

#########################################
# OPENOFFICE
# - setup users openoffice.org2
#   environment
#########################################

OOODIR=$HOME/.openoffice.org2/user/registry/data/org/openoffice
CONFDIR=/usr/share/company/ooo2

# openoffice.org2 directory does not exist
if [ ! -d $OOODIR ]; then
  mkdir -p $OOODIR
  cp -r $CONFDIR/* $OOODIR/.
fi

#########################################
# END OPENOFFICE
#########################################

#########################################
# GXINE
# - set gxine settings
#########################################

GLOBALGXINE=/usr/share/company/gxineconf

# check if file exists
if [ -e $HOME/.gxine/config ]; then
  # check if file is a symlink
  if [ -h $HOME/.gxine/config ]; then
    CONF=`ls -l $HOME/.gxine/config | sed -e "s/.*config -> //g"`
    # check if symlink points to correct location
    if [ $GLOBALGXINE != $CONF ]; then
      rm $HOME/.gxine/config
    fi
  else
    # file is not a symlink
    rm $HOME/.gxine/config
  fi
fi

# check if symlink exists
if [ ! -h $HOME/.gxine/config ]; then
  # check if directory exists
  if [ ! -d $HOME/.gxine ]; then
    mkdir $HOME/.gxine
  fi
  # create symlink
  cd $HOME/.gxine
  ln -s $GLOBALGXINE config
fi

#########################################
# END GXINE
#########################################

# pass execution of login process, along with
# arguments, to gnome-session

exec /usr/bin/gnome-session "$@"
