#!/bin/sh

set -e

# Create GTK/Gnome places bookmark for easy access.
add_gtk3_place() {
	SERVER="$1"
	USER="$2"
	GROUP="$3"
	TITLE="$4"
	SMBPATH="$5"
	if [ ! -e /home/$USER/.config/gtk-3.0/bookmarks ] || ! grep -q "$SMBPATH $TITLE" "/home/$USER/.config/gtk-3.0/bookmarks"; then
		su - "$USER" -c "mkdir -p  \"/home/$USER/.config/gtk-3.0\""
		echo "$SMBPATH $TITLE" >> "/home/$USER/.config/gtk-3.0/bookmarks"
		chown $USER:$GROUP "/home/$USER/.config/gtk-3.0/bookmarks"
	fi
}

# Create GTK-2.0 places bookmark for easy access.
add_gtk2_place() {
	SERVER="$1"
	USER="$2"
	GROUP="$3"
	TITLE="$4"
	SMBPATH="$5"
	if [ ! -e /home/$USER/.gtk-bookmarks ] || ! grep -q "$SMBPATH $TITLE" "/home/$USER/.gtk-bookmarks"; then
		echo "$SMBPATH $TITLE" >> /home/$USER/.gtk-bookmarks
		chown $USER:$GROUP /home/$USER/.gtk-bookmarks
	fi
}

# Create XBEL/KDE/Dolphin places bookmark for easy access
# See http://en.wikipedia.org/wiki/XBEL
create_xbel_place() {
	SERVER="$1"
	USER="$2"
	GROUP="$3"
	TITLE="$4"
	SMBPATH="$5"
	mkdir -p /home/$USER/.local/share
	cat > /home/$USER/.local/share/user-places.xbel <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel>
<xbel xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks" xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info" xmlns:kdepriv="http://www.kde.org/kdepriv">
 <bookmark href="$SMBPATH">
  <title>$TITLE</title>
  <info>
   <metadata owner="http://freedesktop.org">
    <bookmark:icon name="folder-remote"/>
   </metadata>
   <metadata owner="http://www.kde.org">
    <ID>1276594357/0</ID>
   </metadata>
  </info>
 </bookmark>
</xbel>
EOF
	chown -R $USER:$GROUP /home/$USER/.local
}

# FIXME Would be nice if the same bookmark file could be used for both
# KDE and Gnome
case "$ORIGHOMEDIR" in
	/*/*/*)
		homepath="$(ldapsearch -LLL -x "(&(uid=$USER)(sambaHomePath=*))" sambaHomePath | awk '/sambaHomePath: / { print $2 }')"
		if [ "$homepath" ] ; then

			sambaSID="$(ldapsearch -LLL -x "(&(uid=$USER)(sambaSID=*))" sambaSID | awk '/sambaSID: / { print $2 }')"
			if [ "$sambaSID" ]; then
				sambaDomainSID=$(echo $sambaSID | cut -d"-" -f1-7)
				SMBDOMAIN="$(ldapsearch -LLL -x "(&(sambaDomainName=*)(sambaSID=$sambaSID))" sambaDomainName | awk '/sambaDomainName: / { print $2 }');"
			fi

			SMBPATH=$(echo "smb:$homepath" | sed -e "s|\\\\|//|" -e "s|\\\\|/|" | sed -e "s|smb://|smb://$SMBDOMAIN$USER@|")
			SERVER="$(echo $SMBPATH | cut -d "@" -f2 | cut -d/ -f1)"

		else
			# Extract FQDN from home directory path
			SERVER="$(getent hosts $(echo $ORIGHOMEDIR | cut -d/ -f3) | awk '{print $2}')"

			# get the SMBDOMAIN fallback from smb.conf first, but try LDAP later on
			if [ -e /etc/samba/smb.conf ] && grep -q -i "\s*workgroup\s*=\s*" /etc/samba/smb.conf; then
				SMBDOMAIN="$(grep -i -E '\s*workgroup\s*=' /etc/samba/smb.conf | sed -re 's/\s*workgroup\s*=\s*(.*)\s*$/\1/' -e "s/\s+//g");"
			fi

			SMBPATH="smb://$SMBDOMAIN$USER@$SERVER/$USER"
		fi

		GROUP="$(id -ng $USER)"
		TITLE="$USER on $SERVER via SMB"
		add_gtk3_place "$SERVER" "$USER" "$GROUP" "$TITLE" "$SMBPATH"
		add_gtk2_place "$SERVER" "$USER" "$GROUP" "$TITLE" "$SMBPATH"
		create_xbel_place "$SERVER" "$USER" "$GROUP" "$TITLE" "$SMBPATH"

	;;
esac

# Make sure local user have privileges through PolicyKit and can sudo
#adduser $USER sudo
