#!/bin/sh

set -e

CHROOT=/debian
TSTFILE=/usr/bin/hilda
if [ ! -d "$CHROOT" ] ; then
    mkdir /debian
# else
#    if [ -f "$CHROOT$TSTFILE" ] || [ ! "x`/sbin/debian echo hello | grep hello`" = "x" ] ; then
#       closechroot
#       export YESCHROOT='yes'
#    fi
fi

# I sincerely apologise for the following ugly kludge that
# restores all previous settings.

if  [ -f "/home/user/.chroot.orig" ] ; then
  cp /home/user/.chroot.orig /home/user/.chroot
fi

# if  [ -f "/home/user/.Xmodmap-keymap.orig" ] ; then
#   mv /home/user/.Xmodmap-keymap.orig /home/user/.Xmodmap-keymap
#   export UPGRD="upgrade"
# fi

if  [ -f "/home/user/.xbindkeysrc.scm.orig" ] ; then
  mv /home/user/.xbindkeysrc.scm.orig /home/user/.xbindkeysrc.scm
fi

# if  [ -f "/home/user/.gimp-2.4/gimprc.orig" ] ; then
#  mv /home/user/.gimp-2.4/gimprc.orig /home/user/.gimp-2.4/gimprc
# fi

# if  [ -f "/home/user/.gimp-2.4/sessionrc.orig" ] ; then
#  mv /home/user/.gimp-2.4/sessionrc.orig /home/user/.gimp-2.4/sessionrc
# fi

# if  [ -f "/home/user/.config/lxde/config.orig" ] ; then
#  mv /home/user/.config/lxde/config.orig /home/user/.config/lxde/config
# fi

# if  [ -f "/home/user/.config/lxpanel/LXDE/config.orig" ] ; then
#   mv /home/user/.config/lxpanel/LXDE/config.orig /home/user/.config/lxpanel/LXDE/config
# fi

# if  [ -f "/home/user/.config/lxpanel/LXDE/panels/panel.orig" ] ; then
#  mv /home/user/.config/lxpanel/LXDE/panels/panel.orig /home/user/.config/lxpanel/LXDE/panels/panel
# fi

# if  [ -f "/home/user/.config/gtk-2.0/gtkfilechooser.ini.orig" ] ; then
#  mv /home/user/.config/gtk-2.0/gtkfilechooser.ini.orig /home/user/.config/gtk-2.0/gtkfilechooser.ini
# fi

# Fix permissions; another kludge.

chown -R user:users /home/user/img-install
chown -R user:users /home/user/.chroot
# chown -R user:users /home/user/.config
# chown -R user:users /home/user/apps
# chown -R user:users /home/user/.matchbox
# chown user:users /home/user/.Xmodmap-keymap
# chown user:users /home/user/.xbindkeysrc.scm

# yet another kludge; detect an upgrade

if [ "x$UPGRD" = "x" ] ; then
   touch /home/user/.synchroot
   chown -R user:users /home/user/.synchroot
fi

# icon kludges

if  [ ! -e "/usr/share/icons/hicolor/scalable/hildon/abiword.png" ] ; then 
   cp /usr/share/icons/hicolor/scalable/hildon/abiword-debian.png /usr/share/icons/hicolor/scalable/hildon/abiword.png
fi

if  [ ! -e "/usr/share/icons/hicolor/scalable/hildon/deblet-easydeb.png" ] ; then 
   cp /usr/share/icons/hicolor/scalable/hildon/deblet-easydeb.png /usr/share/icons/hicolor/scalable/hildon/deblet.png
fi

# this takes a while; that's why it's at the end...
gtk-update-icon-cache -f /usr/share/icons/hicolor

if [ "x$UPGRD" != "x" ] ; then
  export MSG1=`printf "\
This update points to the new qole.org domain and the updated\n\
image file that has the NEW, UPDATED Debian Stable release.\n\n\
If you want a fully updated Debian (with OpenOffice.org 3.0,\n\
Gimp 2.6, and the Epiphany browser), make sure you \n\
close your chroot, delete the current image if you need \n\
the space, then use the installer to download \n\
the new image file. \n\n\
Note: You don't need to replace your current image file."`
else
export MSG1=`printf "\
In order to use this package, you must download and\n\
install a very large image file. If you are unsure about\n\
how to do this, and / or you just want the current set of\n\
Debian applications (OpenOffice, GIMP & Firefox 3  w/ java),\n\
use the Debian Image Installer in the Extras menu.\n\
This program will download and install the image file to\n\
one of your SD cards. You will need at least 1.5 GB free on that\n\
card. After you download the image, you can move it elsewhere.\n\
After moving the file, you may need to edit the .chroot file \n\
as explained on ITt.\n\
If you have run any Debian apps, be sure to issue\n\
sudo closechroot\n\
in the terminal (or just reboot) before moving the image file.\n"`
fi
gxmessage -center -alignbuttons center -buttons OK:0 -geometry 690x380 -title "NOTICE" "$MSG1"

exit 0