#!/bin/sh

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


CASPERNFSDEBUG=""
CASPERNFSAUTOLOGIN=""
for x in $(cat /proc/cmdline) ; do
 case $x in
   caspernfs-debug)
    CASPERNFSDEBUG="yes"
  ;;
   caspernfs-autologin=*)
    CASPERNFSAUTOLOGIN="${x#caspernfs-autologin=}"
  ;;
 esac
done
if [ -n "$CASPERNFSAUTOLOGIN" ]; then
USERNAME=$CASPERNFSAUTOLOGIN
USERFULLNAME="Mint User"


if [ "$CASPERNFSDEBUG" = "yes" ]; then
  if [ -x /sbin/usplash_write ]; then
          /sbin/usplash_write "QUIT"
  fi
  chvt 1
  if [ -x /bin/plymouth ] && plymouth --ping; then
          /bin/plymouth hide-splash > /dev/null 2>&1
  fi
fi

. /scripts/casper-functions

prereqs()
{
       echo "$PREREQ"
}

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

if [ "$CASPERNFSDEBUG" = "yes" ]; then
  /bin/sh
fi

. /root/usr/share/debconf/confmodule.caspernfs

log_begin_msg "$DESCRIPTION"


# U6aMy0wojraho is just a blank password
echo "$USERNAME:x:999:999:$USERFULLNAME:/home/$USERNAME:/bin/bash" >> /root/etc/passwd
echo "$USERNAME:U6aMy0wojraho:15455:0:99999:7:::" >> /root/etc/shadow
echo "$USERNAME:x:999:" >> /root/etc/group
if [ ! -d /root/home/$USERNAME ]; then
  cp -rp /root/etc/skel /root/home/$USERNAME
fi
chroot /root chown -R 999 /home/$USERNAME


if [ -f /root/etc/sudoers ]; then
    echo "${USERNAME}  ALL=(ALL) NOPASSWD: ALL" >> /root/etc/sudoers
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
fi

