#!/bin/bash
LEVELS="cycle1 cycle2 cycle3 prof direction"
declare -A SKELS
SKELS[cycle1]="01-mini"
SKELS[cycle2]="02-super"
SKELS[cycle3]="03-maxi"
SKELS[prof]="prof"
SKELS[direction]="direction"

declare -A GRPS
GRPS[cycle1]="Cycle1"
GRPS[cycle2]="Cycle2"
GRPS[cycle3]="Cycle3"
GRPS[prof]="Profs"
GRPS[direction]="Administratifs"

user="$1"
level="$2"
TEMPF="$$_temp"
DIR=`dirname $0`
DIST="$DIR/dist"
abort(){
    rm $TEMPF*
    exit 1
}
usage(){
    echo "Usage $0 username level"
    echo "Transform an existing LDAP user into a Primtux LDAP user"
    echo "Options"
    echo "Level :  {${LEVELS// /|}}"
}

if [ -z "$2" ]
then
    usage
    abort
fi
if [ "`echo $LEVELS | tr ' ' '\n' | grep $level`" != "$level" ]
then
    echo "Level should be one of $LEVELS"
    abort
fi
. /etc/slis/slis.conf
# set -x
# Check for LDAP group Primtux
for f in dist/*.ldiff
do
    echo $f
    group=`head -n 1 $f | sed 's/^[^ ]* \([^,]*\),.*$/\1/'`
    if [ -z "`ldapsearch -x $group | grep numEntries`" ]
    then
        echo "adding group"
        # insert primtux group
        sed -e "s/BASE/$LDAP_BASE_DN/" $f > $TEMPF.ldiff
        ldapadd -x -D "cn=$LDAP_ADMIN_RDN,$LDAP_BASE_DN" -w $LDAP_ADMIN_PW -f $TEMPF.ldiff
    fi
done
ldapsearch -x -LLL uid=$user > $TEMPF.ldiff
if [ ! -s $TEMPF.ldiff ]
then
    echo "Unknown user $user"
    abort
fi
# Check for UID => 1000 and update if required
uid=`grep uidNumber $TEMPF.ldiff | cut -d ' ' -f 2`
if [ $uid -eq 1000 ]
then
    # Change UID
    newuid=$(( `slapcat | grep "uidNumber: 10.." | cut -d ' ' -f 2 | sort -nu | tail -n 1`+1))
    # prepare ldiff file
    grep "^dn" $TEMPF.ldiff > $TEMPF.1.ldiff
    echo -e "changetype: modify\nreplace: uidNumber\nuidNumber: $newuid" >> $TEMPF.1.ldiff
    # run ldapmodify
    ldapmodify -x -D "cn=$LDAP_ADMIN_RDN,$LDAP_BASE_DN" -w $LDAP_ADMIN_PW -f $TEMPF.1.ldiff
    uid=$newuid
fi
# Add Primtux to user group
echo -e "dn: cn=Primtux,ou=Groups,$LDAP_BASE_DN\nchangetype: modify\nadd: memberUid\nmemberUid: $user" > $TEMPF.1.ldiff
ldapmodify -x -D "cn=$LDAP_ADMIN_RDN,$LDAP_BASE_DN" -w $LDAP_ADMIN_PW -f $TEMPF.1.ldiff
echo -e "dn: cn=${GRPS[$level]},ou=Groups,$LDAP_BASE_DN\nchangetype: modify\nadd: memberUid\nmemberUid: $user" > $TEMPF.1.ldiff
ldapmodify -x -D "cn=$LDAP_ADMIN_RDN,$LDAP_BASE_DN" -w $LDAP_ADMIN_PW -f $TEMPF.1.ldiff
# Retrieve files from skels
skel="$DIR/skels/${SKELS[$level]}"
home=`grep home $TEMPF.ldiff | cut -d ' ' -f 2`
mv $home $home.bak
cp -r $skel $home
cp -r $home.bak/* $home/
rm -rf $home.bak
# fix links
find $home/.wine/drive_c/ -type l ! -exec test -e {} \; -print | while read f
do
    ln -sf "$home/`basename \"$f\" | sed 's/.* \(.*\)/\u\1/'`" "$f"
done
sed -i "s@${SKELS[$level]}@$user@g" $home/.config/xfce4/panel/launcher-4/14504679941.desktop
sed -i "s@/home/${SKELS[$level]}/Documents@/home/$user/Documents @g" $home/.handymenu.conf
sed -i "s@${SKELS[$level]}@$user@g" $home/.handymenu.conf
# Add bookmarks files that are no in skel
mkdir -p $home/.config/gtk-3.0
cp dist/bookmarks/$level $home/.config/gtk-3.0/bookmarks
# Fix proxy issue
sed -i '/^export =/d' /home/$user/.profile
for PROTO in HTTP HTTPS FTP
do
    echo "export ${PROTO}_PROXY=slis:3128" >> /home/$user/.profile
done
# Fix evince as pdf reader
echo '[Default Applications]' >> /home/$user/.local/share/applications/defaults.list
echo 'application/pdf=evince.desktop' >> /home/$user/.local/share/applications/defaults.list
# fix samba passwd
# echo "You will  be prompted twice for samba password, please enter the same as for LDAP"
# smbpasswd -U $user
# fix Rights
chown -R $uid:lcs-users $home
rm $TEMPF*
echo "Correctly set up $user for $level"
