#!/bin/sh
if [ $(id -u) != 0 ]; then
 echo Error: You must be root to run this script!
 exit 1
fi

X_CONF=XF86Config-4
if which Xorg >/dev/null; then
 [ -e /etc/X11/xorg.conf ] && X_CONF=xorg.conf
fi

ENABLE="s|ServerCmd=.*|ServerCmd=/usr/bin/Xgl :0 -fp /usr/share/fonts/X11/misc -fullscreen -ac -accel glx:pbuffer -accel xv:fbo:0 -br|"
grep -q fglrx /etc/X11/$X_CONF && ENABLE="s|ServerCmd=.*|ServerCmd=/usr/bin/Xgl :1 -fp /usr/share/fonts/X11/misc -fullscreen -ac -accel glx:pbuffer -accel xv:pbuffer -br|"
DISABLE="s|ServerCmd=.*|ServerCmd=/usr/bin/X -br|"


[ -d /KNOPPIX -o -d /KANOTIX ] || INSTALLED="yes"
if [ -n "$INSTALLED" ]; then
 if test -n "$DISPLAY"; then
  echo Error: Can\`t be run within X! Use a text console \(ALT-CTRL-F1\)
  exit 2
 fi
else
 if grep -q unionfs /proc/mounts; then
  echo Unionfs found.
 else
  echo Activating Unionfs now.
  mkdir -p /tmp/union
  UNION=/KNOPPIX
  [ -d /KANOTIX ] && UNION=/KANOTIX
  mount -t unionfs -o dirs=/tmp/union/:$UNION /none $UNION/ 2>&1 >/dev/null
 fi
 echo "Enable KDM AutoLogin..."
 USER="$(getent passwd 1000|cut -d: -f1)"
 perl -pi -e "s/^.*(AutoLoginUser)=.*/\1=$USER/;s/^.*(AutoLoginEnable)=.*/\1=true/;s/^.*(AutoLoginAgain)=.*/\1=true/" /etc/kde3/kdm/kdmrc
 # Remove live initscript
 rm -f /etc/X11/Xsession.d/45xsession
 # Be sure that xorg.conf exists
 [ -e /etc/X11/xorg.conf ] || cp /etc/X11/$X_CONF /etc/X11/xorg.conf
fi

# Enable Win key, use 24 bit, disable sw_cursor
for xconf in /etc/X11/xorg.conf /etc/X11/xorg.conf.1st /etc/X11/XF86Config-4 /etc/X11/XF86Config-4.1st; do
 if [ -e $xconf ]; then
  perl -pi -e 's/^.*XkbOptions.*\n//' $xconf
  perl -pi -e 's/^(\s*Option\s+)("XkbRules".*)$/\1\2\n\1"XkbOptions" "altwin:super_win"/' $xconf
  perl -pi -e 's/DefaultColorDepth.*/DefaultColorDepth 24/' $xconf
  perl -pi -e 's/^[\s]*Option\s*"sw_cursor"/\t#Option     "sw_cursor"/' $xconf
 fi
done

# Fix sources.list if needed
perl -pi -e 's|\n?$|\n|' /etc/apt/sources.list

UNINSTALL="xgl cgwd-themes gset-compiz beryl beryl-manager beryl-plugins beryl-settings emerald emerald-themes compiz compiz-core compiz-gnome compiz-gtk compiz-plugins"
COMPIZ="xgl compiz cgwd-themes gset-compiz"
CAIRO="libpng3 libsvg-cairo1 libsvg1 libsvg1 libsvg-cairo1 libglitz-glx1"

rm -f /home/*/.kde/Autostart/compiz.desktop
rm -f /home/*/.kde/Autostart/beryl.desktop

if [ "$1" = "-u" ]; then
 COMPIZ_REMOVE="libglitz1 libgnome-desktop-2 libgnome-media0 libgnome-menu2
                libgnome-window-settings1 libgtkhtml2-0 libgtksourceview-common
		libgtksourceview1.0-0 libmetacity0 libnautilus-burn3
		libpanel-applet2-0 librsvg2-2 libtotem-plparser1 libwnck-common
		libwnck18 libxres1 metacity-common python-gnome2-desktop
		python-gnome2-extras python-pyorbit
		xgl compiz cgwd-themes gset-compiz"

 perl -pi -e 's/.*www5.autistici.org.*\n//;s/.*cairographics.org.*\n//' /etc/apt/sources.list
 perl -pi -e 's|.*deb http://snapshot.debian.net/archive pool dbus.*\n||' /etc/apt/sources.list
 apt-get update
 apt-get remove --yes --purge $COMPIZ_REMOVE
fi

rm -f /usr/share/autostart/compiz.desktop
rm -f /usr/share/autostart/beryl.desktop

if [ "$1" = "-d" -o "$1" = "-u" ]; then
 echo "Disable Compiz..."
 perl -pi -e "$DISABLE" /etc/kde3/kdm/kdmrc
fi

if [ -z "$1" ]; then
 [ "$(uname -m)" = "x86_64" ] && CPU=amd64 || CPU=i386
 grep -q www5.autistici.org /etc/apt/sources.list || echo "deb http://www5.autistici.org/debian-xgl/debian/ binary-$CPU/" >> /etc/apt/sources.list
 #grep -q  cairographics.org /etc/apt/sources.list || echo "deb http://cairographics.org/packages/debian/ unstable/" >> /etc/apt/sources.list
 grep -q "deb http://snapshot.debian.net/archive pool dbus" /etc/apt/sources.list || echo "deb http://snapshot.debian.net/archive pool dbus" >> /etc/apt/sources.list
 gpg --keyserver wwwkeys.eu.pgp.net --recv-keys 11F6E468
 gpg --armor --export 11F6E468 | apt-key add -
 apt-get update
 apt-get --purge --yes remove $UNINSTALL
 apt-get --allow-unauthenticated --yes install $COMPIZ 
fi

if [ "$1" = "-e" -o -z "$1" ]; then
 if ! [ -x /usr/bin/Xgl ]; then
  echo "Error: No Xgl installed, install it first (script without option)!" >&2
  exit 3
 fi
 echo "Enable Compiz..."
 echo "Hint: When you experience problems with some apps use:"
 echo "export XLIB_SKIP_ARGB_VISUALS=1"
 perl -pi -e "$ENABLE" /etc/kde3/kdm/kdmrc
 echo "[Desktop Entry]
Encoding=UTF-8
Exec=xmodmap -e 'keycode 113 = Mode_switch' -e 'keycode 22 = BackSpace';compiz-start.py
GenericName[de_DE]=
StartupNotify=false
Terminal=false
TerminalOptions=
Type=Application
X-KDE-autostart-after=kdesktop" > /usr/share/autostart/compiz.desktop
fi

# fglrx hack
if grep -q fglrx /etc/X11/$X_CONF; then
 LIBGL_DRIVERS_PATH=/usr/lib/dri
 if [ "$(uname -m)" = "x86_64" ]; then
  [ -d /usr/lib32/dri ] && LIBGL_DRIVERS_PATH=${LIBGL_DRIVERS_PATH}:/usr/lib32/dri
 fi
 perl -pi -e "s/^.*LIBGL_DRIVERS_PATH.*\n//" /etc/init.d/kdm
 [ "$1" = "-e" -o -z "$1" ] && perl -pi -e "s|^(set -e)\n|\1\nexport LIBGL_DRIVERS_PATH=$LIBGL_DRIVERS_PATH\n|" /etc/init.d/kdm
fi

if [ "$INSTALLED" == "yes" ]; then
 DM=`cat /etc/X11/default-display-manager 2>/dev/null`
 DM=`basename $DM 2>/dev/null`
 if [ -z "$DM" ]; then
  [ -e /etc/init.d/kdm ] && /etc/init.d/kdm stop
 else
  [ -e /etc/init.d/$DM ] && /etc/init.d/$DM stop
 fi
 killall -9 Xgl >/dev/null 2>&1
 rm -f /etc/X11/default-display-manager
 echo /usr/bin/kdm > /etc/X11/default-display-manager
 sleep 1
 /etc/init.d/kdm start
else
 nohup sh -c "/etc/init.d/xsession stop;
	      DM=`cat /etc/X11/default-display-manager 2>/dev/null`;
	      DM=`basename $DM 2>/dev/null`;
	      if [ -z "$DM" ]; then
               [ -e /etc/init.d/kdm ] && /etc/init.d/kdm stop
	      else
	       [ -e /etc/init.d/$DM ] && /etc/init.d/$DM stop
	      fi;
	      killall -9 Xgl >/dev/null 2>&1
	      rm -f /etc/X11/default-display-manager;
	      echo /usr/bin/kdm > /etc/X11/default-display-manager;
	      sleep 5;
	      /etc/init.d/kdm start"
fi
