#!/bin/sh
# Part of spindle http://asbradbury.org/projects/spindle
#
# See LICENSE file for copyright and license details

set -ex

. ./common

CURIMG=stage4-lxde.$IMGFORMAT

install_packages() {
  # we may want to break out DEBIAN_FRONTEND=noninteractive
  ssh_in_to_qemu chroot /mnt sh -l -ex - <<EOF
apt-get update
apt-get install --no-install-recommends -y xserver-xorg-video-fbdev
apt-get install --no-install-recommends -y xserver-xorg xinit
apt-get install -y gstreamer1.0-x gstreamer1.0-omx gstreamer1.0-plugins-base \
  gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-alsa \
  gstreamer1.0-libav
apt-get install --no-install-recommends -y epiphany-browser cgroup-bin
apt-get install --no-install-recommends -y lxde lxtask menu-xdg gksu
apt-get install -y xserver-xorg-video-fbturbo
apt-get install -y xpdf gtk2-engines alsa-utils
apt-get install -y --no-install-recommends netsurf-gtk zenity xdg-utils
apt-get install -y desktop-base lxpolkit
apt-get install -y --no-install-recommends gvfs-backends gvfs-fuse
apt-get install -y wpagui
apt-get install -y git-core rpi-update
EOF
}

# make gksu default to sudo mode
configure_gksu() {
  onvm_chroot sh -l -e - <<\EOF
update-alternatives --set libgksu-gconf-defaults /usr/share/libgksu/debian/gconf-defaults.libgksu-sudo
update-gconf-defaults
EOF
}

install_wayland() {
  onvm_chroot sh -l -ex - <<\EOF
echo "deb http://raspberrypi.collabora.com wheezy rpi" > /etc/apt/sources.list.d/collabora.list
apt-key add - <<EOF1
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.13 (GNU/Linux)

mQINBFGONkIBEADC69AVM/bJ15zZftt73ZtWzqER890BLRWYHNCCqidbcs+Ww2Bi
PGIfTOnsXQIGrdDKmtgjc4kskDTeG7VWCgmhzNBzvdNrfVYxv4EpzdWUxifEOYXo
RvEynwQ0AuehplOfZeVidzvWvBwuVDt0L3XRW8etjSYfjKSYnEVCsZW3EwVUs3zp
dyjHYjQSrjwaWfFZEtU7O4U6EUiGARJnOaim9bAh6fSsXy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WFRUl6uCpLQ0tR6pJ
oC64oGPz8wMv3ZHXrb/CZM3o63hO4qv4Be9aCj+As3oXWNPOZ6Vwmx3rkiffeRig
ypY97pZhq00Kx6ycjfP0h2SUazZMuQEjawCeca8LDE+8jn/EEnnP7MgWm7EOVlkS
c/I08VWVqPOQGl1FuncbJ4KhBkrkmPoMUuz7D4Dq4fAzIS7PJkthzHdGkNoneo/X
pZRR1qfCwFBNvNzkCG3ECggevh3fTEkVvhnVTIvzq5GUi9PsjjNtAt8JPxsJgZkg
FJLPlJTgpm8KR9OiIKn1z9RodqXSUZhE7Cw/koNzzNpjfo4tB4OmNueiCXObNRrr
jIaacoqqok301Mk3SVNc3YyEa/9HN5DmETrQS+9fhukP2Frcr4wOHJCajvxa+Sl7
oryGCWXqvBpSEucw1FDkOgoe5EwHvcdu+dzVypY0JZoLHQDV69aTiHFsZhBu6Ves
hG/v4/wnBFL5f0cl9t53GmEDcjsg5VKbcTmCMG6Z6DbNIusYAz98ucAqGupzKvA0
3C/Ik0Y1LI8iEYXPiUbgSBCZ4RrJsdtbZDRyy357pSr+5eB6eXLy1eBC84jv/A6h
HXWu9eprthyEIX6pmEhqzMViFix7FVozfdn2H4U=
=zyWt
-----END PGP PUBLIC KEY BLOCK-----
EOF1
apt-get update
apt-get install -y --force-yes weston
EOF
}

install_omxplayer() {
  onvm_chroot sh -l -e - <<\EOF
apt-get update
apt-get install -y omxplayer
EOF
}

configure_lxde() {
  onvm_chroot sh -l -e <<\EOF
# first change theme to mist (seems to render noticably faster)
sed /etc/xdg/lxsession/LXDE/desktop.conf -i -e "s|sNet/ThemeName.*|sNet/ThemeName=Mist|"

# configure openbox so it doesn't ask apps to redraw while resizing
sed /etc/xdg/openbox/LXDE/rc.xml -i -e \
  "s|<drawContents>yes</drawContents>|<drawContents>no</drawContents>|"

apt-get install -y raspberrypi-artwork
# change background
update-alternatives --install /usr/share/images/desktop-base/desktop-background \
  desktop-background /usr/share/raspberrypi-artwork/raspberry-pi-logo.png 100
PCMANFMCFG=/etc/xdg/pcmanfm/LXDE/pcmanfm.conf
sed "$PCMANFMCFG" -i -e 's/^wallpaper_mode.*/wallpaper_mode=3/'
sed "$PCMANFMCFG" -i -e 's/^desktop_bg.*/desktop_bg=#ffffff/'

# while we're at it, let's not use xdg-su which Debian doesn't even provide
sed "$PCMANFMCFG" -i -e 's/^su_cmd.*/su_cmd=gksu %s/'
EOF
}

setup_automounting() {
  onvm_chroot sh -l -e <<\EOF1
apt-get install -y udisks
cat <<EOF2 > /etc/polkit-1/localauthority/50-local.d/55-storage.pkla
[Storage Permissions]
Identity=unix-group:plugdev
Action=org.freedesktop.udisks.filesystem-mount;org.freedesktop.udisks.drive-eject;org.freedesktop.udisks.drive-detach;org.freedesktop.udisks.luks-unlock;org.freedesktop.udisks.inhibit-polling;org.freedesktop.udisks.drive-set-spindown
ResultAny=yes
ResultActive=yes
ResultInactive=no
EOF2
EOF1
}

install_and_configure_lightdm() {
  onvm_chroot sh -l -e <<\EOF
apt-get install --no-install-recommends -y lightdm gnome-themes-standard-data gnome-icon-theme
apt-get install -y policykit-1
update-rc.d lightdm disable 2
sed -i /etc/lightdm/lightdm-gtk-greeter.conf -e "s/^background.*/background=#ffffff/"
# TODO: maybe change the computer icon to the Raspberry Pi logo? See 
# https://wiki.archlinux.org/index.php/LightDM#Changing_the_Icon

# Source /etc/profile and ~/.profile through XSession.d to ensure PATH is set 
# properly when lightdm is used.
cat <<\EOF1 > /etc/X11/Xsession.d/75source-profile
[ -f /etc/profile ] && . /etc/profile
[ -f "$HOME/.profile" ] && . "$HOME/.profile"
EOF1
EOF
}

install_qt5() {
  onvm_chroot sh -l -e <<\EOF
apt-get update
apt-get install --allow-unauthenticated -y qt50-snapshot qt50-quick-particle-examples
EOF
}

cd $WORKDIR
dotask branch_image ../$OUTDIR/stage3.$IMGFORMAT $CURIMG
dotask run_qemu $CURIMG
dotask mount_apt_cache
dotask disable_starting_services
dotask install_packages
dotask configure_gksu
[ -n "$RASPBIAN" ] && dotask install_omxplayer
dotask configure_lxde
dotask install_and_configure_lightdm
dotask setup_automounting
#[ -z "$RASPBIAN" ] && dotask install_qt5
[ -n "$RASPBIAN" ] && dotask install_wayland
dotask save_space_using_hardlink
dotask allow_starting_services
dotask update_issue
dotask fingerprint_debian
dotask shutdown_qemu
dotask finish_image
