#!/bin/sh
if [ "$(id -u)" = "0" ]; then
 echo "Installing pulseaudio - best restart system / dm to activate..."
 apt-get update
 apt-get install --yes pulseaudio-module-x11 pulseaudio-module-bluetooth libasound2-plugins pavucontrol vlc-plugin-pulse
 [ "$(uname -m)" = "x86_64" ] && apt-get install --yes libasound2-plugins:i386
 if ! [ -f /usr/share/fonts/X11/100dpi/helvR14-ISO8859-1.pcf.gz ]; then
  echo "Installing font - requires dm restart..."
  apt-get install --yes xfonts-100dpi
 fi
 echo "Installing depends and steam deb only - run script as user again..."
 DEPENDS="gtk2-engines libasound2 libasyncns0 libatk1.0-0 libattr1 libavahi-client3 libavahi-common3 libc6 libcairo2 libcap2 libcomerr2 libcups2 libcurl3-gnutls libdbus-1-3 libexpat1 libffi5 libflac8 libfontconfig1 libfreetype6 libgcc1 libgcrypt11 libgdk-pixbuf2.0-0 libgl1-mesa-dri libgl1-mesa-glx libglib2.0-0 libgnutls26 libgpg-error0 libgssapi-krb5-2 libgtk2.0-0 libice6 libjson0 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libnspr4 libnss3 libogg0 libopenal1 libp11-kit0 libpango1.0-0 libpci3 libpcre3 libpixman-1-0 libpng12-0 libpulse0 libsdl1.2debian libselinux1 libsm6 libsndfile1 libstdc++6 libtasn1-3 libtxc-dxtn-s2tc0 libudev0 libuuid1 libvorbis0a libvorbisenc2 libvorbisfile3 libwrap0 libx11-6 libx11-xcb1 libxau6 libxcb1 libxcb-render0 libxcb-shm0 libxcomposite1 libxcursor1 libxdamage1 libxdmcp6 libxext6 libxfixes3 libxi6 libxinerama1 libxmu6 libxrandr2 libxrender1 libxtst6 zlib1g"
 if [ "$(uname -m)" = "x86_64" ]; then
  apt-get install --yes $(echo $DEPENDS|tr ' ' '\n'|sed 's/$/:i386/')
 else
  apt-get install --yes $DEPENDS
 fi
 if ! [ -x /usr/bin/gksu ]; then
  echo "Install gksu for updateing (however steam has gksudo hardcoded)..."
  apt-get install --yes gksu
 fi
 if ! [ -x /usr/bin/gnome-terminal -o -x /usr/bin/xterm ]; then
  echo "Install xterm for updateing..."
  apt-get install --yes xterm
 fi
  if ! [ -x /usr/bin/zenity ]; then
  echo "Install zenity for updateing..."
  apt-get install --yes zenity
 fi
 TMP=$(mktemp -d /tmp/steam.XXXXXXX)
 wget -NP $TMP http://media.steampowered.com/client/installer/steam.deb
 dpkg-deb -x $TMP/steam.deb $TMP/steam-debian
 dpkg-deb -e $TMP/steam.deb $TMP/steam-debian/DEBIAN
 sed -i 's/2.15/2.13/g;s/-turbo//;s/1:0.99.1/2.0/;s/jockey-common,//' $TMP/steam-debian/DEBIAN/control
 dpkg-deb -b $TMP/steam-debian
 dpkg --purge steam steam64 steam-launcher 2>/dev/null
 dpkg -i $TMP/steam-debian.deb
 apt-get install --yes -f
 rm -rf $TMP
 rm -f /etc/apt/sources.list.d/steam.list*
 rm -f /usr/bin/steamdeps
else
 VDPAU_HACK=1
 [ -r $HOME/.flash ] && . $HOME/.flash
 while getopts vh opt; do
  case $opt in
   v) VDPAU_HACK=0 ;;
   \?|h) echo "Usage $0 [options] " >&2
         echo "" >&2
	 echo "optional options are:" >&2
	 echo " -v      Allow VDPAU" >&2
	 echo " -h      Show this help" >&2
	 exit 1
	 ;;
  esac
 done
 #STEAM=~/Steam
 STEAM=~/.local/share/Steam
 if dpkg --compare-versions $(dpkg -l|awk '/libc6/{print $3}'|head -1) ge 2.15; then
  if [ -d $STEAM/deb ]; then
   echo "Removing old libc6 hack now!"
   cd $STEAM
   for x in $(for y in deb/*.deb; do dpkg-deb -c $y;done|awk '{if($6 ~ /\.\/lib\/.*-linux-gnu/){print $6}}');do
    rm -vf ubuntu12_*/$(basename $x)
   done
   rm -rf $STEAM/deb
   cd - >/dev/null
  fi
 else
  if ! [ -f $STEAM/ubuntu12_32/libc-2.15.so ]; then
   mkdir -p $STEAM/ubuntu12_32
   mkdir -p $STEAM/deb
   wget -NP $STEAM/deb http://security.ubuntu.com/ubuntu/pool/main/e/eglibc/libc6_2.15-0ubuntu20.2_i386.deb
   TMP=$(mktemp -d /tmp/steam.XXXXXXX)
   dpkg-deb -x $STEAM/deb/libc6_2.15-0ubuntu20.2_i386.deb $TMP/libc6
   mv $TMP/libc6/lib/i386-linux-gnu/* $STEAM/ubuntu12_32
   rm -rf $TMP
  fi
  if [ "$(uname -m)" = "x86_64" ] && ! [ -f $STEAM/ubuntu12_64/libc-2.15.so ]; then
   mkdir -p $STEAM/ubuntu12_64
   mkdir -p $STEAM/deb
   wget -NP $STEAM/deb http://security.ubuntu.com/ubuntu/pool/main/e/eglibc/libc6_2.15-0ubuntu20.2_amd64.deb
   TMP=$(mktemp -d /tmp/steam.XXXXXXX)
   dpkg-deb -x $STEAM/deb/libc6_2.15-0ubuntu20.2_amd64.deb $TMP/libc6
   mv $TMP/libc6/lib/x86_64-linux-gnu/* $STEAM/ubuntu12_64
   rm -rf $TMP
  fi
 fi
 #URL=$(wget -qO- https://get.adobe.com/de/flashplayer/completion/?installer=Flash_Player_11.2_for_other_Linux_%28.tar.gz%29_32-bit|awk -F\' '/location.href/{print $2}'|sed s/http:/https:/)
 URL=https://fpdownload.macromedia.com/get/flashplayer/pdc/$(wget -qO- https://www.adobe.com/software/flash/about|awk -F'[<>]' '/11\.2\./{print $3}'|head -1)/install_flash_player_11_linux.i386.tar.gz
 URL_OLD=
 [ -r $STEAM/ubuntu12_32/plugins/libflashplayer.url ] && URL_OLD=$(cat $STEAM/ubuntu12_32/plugins/libflashplayer.url)
 if [ "$URL_OLD" != "$URL $VDPAU_HACK" ] || ! [ -r $STEAM/ubuntu12_32/plugins/libflashplayer.so ]; then
  echo "Installing flash..."
  mkdir -p $STEAM/ubuntu12_32/plugins
  wget $URL -qO-|tar zxvC $STEAM/ubuntu12_32/plugins libflashplayer.so
  echo $URL $VDPAU_HACK>  $STEAM/ubuntu12_32/plugins/libflashplayer.url
  [ "$VDPAU_HACK" = "1" ] && sed -i s/libvdpau/libvdpax/g $STEAM/ubuntu12_32/plugins/libflashplayer.so
 fi
 echo "run now: steam" 
 echo "hint: install world of goo demo to try"
 echo "try too: steam steam://install/440"
fi
