#!/bin/sh

PREREQ=""
DESCRIPTION="Adding live session user..."

prereqs()
{
       echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
       prereqs
       exit 0
       ;;
esac

. /scripts/casper-functions
load_confmodule

log_begin_msg "$DESCRIPTION"

# U6aMy0wojraho is just a blank password
db_set passwd/root-password-crypted '*'
db_set passwd/user-password-crypted U6aMy0wojraho
db_set passwd/user-fullname "$USERFULLNAME"
db_set passwd/username "$USERNAME"
db_set passwd/user-uid 999

chroot /root /usr/lib/user-setup/user-setup-apply > /dev/null

# Clear out debconf database again to avoid confusing ubiquity later.
db_set passwd/root-password-crypted
db_set passwd/user-password-crypted
db_set passwd/user-fullname
db_set passwd/username
db_set passwd/user-uid

if [ -f /root/etc/sudoers ]; then
    if [ "${BUILD_SYSTEM}" = "Ubuntu" ]; then
	if grep -q '^%admin' /root/etc/sudoers; then
	    sed -i -e '/^%admin/s/(ALL) ALL$/(ALL) NOPASSWD: ALL/' /root/etc/sudoers
	else
	    echo '%admin  ALL=(ALL) NOPASSWD: ALL' >> /root/etc/sudoers
	fi
    elif [ "${BUILD_SYSTEM}" = "Debian" ]; then
		echo "${USERNAME}  ALL=(ALL) NOPASSWD: ALL" >> /root/etc/sudoers
    fi
fi

# XXX - awful hack to stop xscreensaver locking the screen (#7150)
echo 'RUNNING_UNDER_GDM="yes"' >> /root/etc/environment

LTS="$(cut -d' ' -f3 /root/cdrom/.disk/info 2>/dev/null)" || LTS=
RELEASE="$(cut -d' ' -f1-2 /root/cdrom/.disk/info 2>/dev/null)" || RELEASE=
if [ "$LTS" = "LTS" ] && [ -n "$RELEASE" ]; then
	RELEASE="$RELEASE LTS"
fi
for file in /usr/share/applications/ubiquity-gtkui.desktop /usr/share/applications/kde4/ubiquity-kdeui.desktop; do
    if [ -f "/root/$file" ]; then
        sed -i "s/RELEASE/$RELEASE/" "/root$file"
        chroot /root install -d -o $USERNAME -g $USERNAME /home/$USERNAME/Desktop
        chroot /root install -D -o $USERNAME -g $USERNAME $file /home/$USERNAME/Desktop/$(basename "$file")
        break
    fi
done

if [ -L /root/home/$USERNAME/Examples ]; then
    chroot /root install -o $USERNAME -g $USERNAME -d /home/$USERNAME/Desktop/
    mv /root/home/$USERNAME/Examples /root/home/$USERNAME/Desktop/
fi

if [ -f /root/home/$USERNAME/examples.desktop ]; then
    chroot /root install -o $USERNAME -g $USERNAME -d /home/$USERNAME/Desktop/
    mv /root/home/$USERNAME/examples.desktop /root/home/$USERNAME/Desktop/
fi

#Kubuntu
if [ -f "/root/usr/bin/plasma" ]; then
    if [ -f "/root/usr/share/kde4/apps/khelpcenter/plugins/kubuntu/about-kubuntu.desktop" ]; then
      chroot /root install -d -o $USERNAME -g $USERNAME /home/$USERNAME/Desktop
      chroot /root install -D -o $USERNAME -g $USERNAME /usr/share/kde4/apps/khelpcenter/plugins/kubuntu/about-kubuntu.desktop /home/$USERNAME/Desktop/about-kubuntu.desktop
    fi
fi

#Ubuntu MID wants to be installed with --automatic
if [ -f "/root/usr/share/ubuntu-mid-default-settings/mid-gui-start" ]; then
  if [ -f "/root/usr/share/applications/ubiquity-gtkui.desktop" ]; then
    sed -i 's/--desktop/--automatic --desktop/' "/root/usr/share/applications/ubiquity-gtkui.desktop"
  fi
fi

#Ubuntu MID uses kourou, and the ubiquity icon should be easy to find
if [ -f "/root/usr/lib/hildon-desktop/kourou.py" ]; then
  if [ -f "/root/etc/xdg/menus/home.menu" ]; then
    sed -i '/<\/Include>/i\		<Filename>ubiquity-gtkui.desktop</Filename>' "/root/etc/xdg/menus/home.menu"
  fi
fi

log_end_msg
