#!/bin/sh

closechroot

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
  export UPGRD="upgrade"
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.orig" ] ; then
  rm /home/user/.xbindkeysrc.orig
#  mv /home/user/.xbindkeysrc.orig /home/user/.xbindkeysrc
fi

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

if  [ -f "/home/user/.gimp-2.6/sessionrc.orig" ] ; then
  mv /home/user/.gimp-2.6/sessionrc.orig /home/user/.gimp-2.6/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/lxpanel/LXDE/panels/top.orig" ] ; then
  rm /home/user/.config/lxpanel/LXDE/panels/top.orig
#   mv /home/user/.config/lxpanel/LXDE/panels/top.orig /home/user/.config/lxpanel/LXDE/panels/top
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/.gimp-2.6
chown user:users /home/user/.gtkrc-2.0

# 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

# 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 "\
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\
MyDocs or a microSD card. You will need at least 2.5 GB free\n\
there. 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 talk.maemo.org.\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