#!/bin/sh

# as vdpau accelleration is not fully stable currently, it is disabled now
# override possible in ~/.flash or with "-v" option to the script
# to try a new flash version use "-n" option, if there is one...
# to use hw accelleration this is also required as root:
# mkdir -p /etc/adobe;echo EnableLinuxHWVideoDecode=1 > /etc/adobe/mms.cfg

if [ -z "$TERM" ]; then
 grep -q noflash /proc/cmdline && exit 0
fi

if [ "$(id -u)" = "0" ]; then
 echo "Error: Do NOT run this script as root!" >&2
 exit 1
fi

[ -x /usr/bin/curl ] && LOAD="/usr/bin/curl -s"
[ -x /usr/bin/wget ] && LOAD="/usr/bin/wget -qO-"

if [ -z "$LOAD" ]; then
 echo "Error: No curl or wget found!"
 exit 2
fi

#VDPAU_HACK=
#TRY_NEXT=
#PING_CHECK=
WRITE_CONFIG=

[ -r $HOME/.flash ] && . $HOME/.flash

while getopts vdncpwh opt; do
 case $opt in
  v) VDPAU_HACK=0 ;;
  d) VDPAU_HACK=1 ;;
  n) TRY_NEXT=1 ;;
  c) TRY_NEXT=0 ;;
  p) PING_CHECK=0 ;;
  w) WRITE_CONFIG=1 ;;
  \?|h) echo "Usage $0 [options] " >&2
        echo "" >&2
        echo "optional options are:" >&2
        echo " -v      Allow VDPAU (needs override too)" >&2
        echo " -d      Disable VDPAU (ignore config override)" >&2
        echo " -n      Try next version (if possible)" >&2
        echo " -c      Use current version (ignore config override)" >&2
        echo " -p      Disable ping check" >&2
        echo " -w      Write config file" >&2
        echo " -h      Show this help" >&2
        echo "" >&2
        echo "Global VDPAU override:" >&2
        echo "mkdir -p /etc/adobe;echo EnableLinuxHWVideoDecode=1 > /etc/adobe/mms.cfg" >&2
        echo "" >&2
        echo "Overrides possible in env or ~/.flash" >&2
        echo "VDPAU_HACK=0" >&2
        echo "TRY_NEXT=1" >&2
        echo "PING_CHECK=0" >&2
	exit 3
        ;;
 esac
done

if [ -z "$TERM" ]; then
 grep -q vdpau /proc/cmdline && VDPAU_HACK=0
 grep -q trynext /proc/cmdline && TRY_NEXT=1
 grep -q noping /proc/cmdline && PING_CHECK=0
fi

case $VDPAU_HACK in
 0) echo "Allowing VDPAU, see -h for more info..." >&2 ;;
 *) VDPAU_HACK=1 ;;
esac

case $TRY_NEXT in
 1) echo "Trying next version..." >&2 ;;
 *) TRY_NEXT=0 ;;
esac

case $PING_CHECK in
 0) echo "Don't ping before download..." >&2 ;;
 *) PING_CHECK=1
esac

if [ "$WRITE_CONFIG" = "1" ]; then
 echo "Writing default config file..." >&2
 rm -f ~/.flash
 cat > ~/.flash <<EOT
VDPAU_HACK=$VDPAU_HACK
TRY_NEXT=$TRY_NEXT
PING_CHECK=$PING_CHECK
EOT
fi

if [ "$PING_CHECK" = "1" ]; then
 FOUND=0
 TRY=1
 while :; do
  if ping -c1 download.macromedia.com >/dev/null 2>&1; then
   FOUND=1
   break
  else
   [ $TRY -eq 1 ] && echo -n "Waiting for internet connection (up to 600 s, check every 10 s): " >&2
   TRY=$((TRY+1))
   echo -n . >&2
   sleep 10
   if [ $TRY -ge 60 ]; then
    echo
    break
   fi
  fi
 done

 if [ "$FOUND" = "0" ]; then
  echo "Error: No internert connection found until timeout!" >&2
  exit 4
 fi
fi

BIT=32
[ "$(uname -m)" = "x86_64" ] && BIT=64
URL=http://download.macromedia.com/pub/flashplayer/installers/current/9/install_flash_player_9.tar.gz
if grep -qs GLIBC_2.4 /lib/ld-linux.so.2; then
 if [ "$TRY_NEXT" = "0" ]; then
  URL=$($LOAD http://get.adobe.com/de/flashplayer/completion/?installer=Flash_Player_11_for_other_Linux_%28.tar.gz%29_$BIT-bit|awk -F\' '/location.href/{print $2}')
 else
  URL=$($LOAD http://labs.adobe.com/downloads/flashplayer11-2.html|awk -F'"' '/flashplayer/&&/_lin_/&&/'$BIT'/&&!/debug/{print $12}')
 fi
fi

if [ -z "$URL" ]; then
 echo "Download URL not found! Ask for script update!" >&2
 exit 5
fi

URL_OLD=
[ -r $HOME/.mozilla/plugins/libflashplayer.url ] && URL_OLD=$(cat $HOME/.mozilla/plugins/libflashplayer.url)
mkdir -p $HOME/.mozilla/plugins
echo $URL $VDPAU_HACK > $HOME/.mozilla/plugins/libflashplayer.url

if [ "$URL_OLD" = "$URL $VDPAU_HACK" ] && [ -r $HOME/.mozilla/plugins/libflashplayer.so ]; then
 echo "Warning: Flash plugin already installed, doing nothing!" >&2
 exit 0
fi

if ! ($LOAD $URL|tar zxvC $HOME/.mozilla/plugins libflashplayer.so); then
 rm -vf $HOME/.mozilla/plugins/libflashplayer.so
 echo "Error: Bad download, try again!" >&2
 exit 6
fi

[ "$VDPAU_HACK" = "1" ] && sed -i s/libvdpau/libvdpax/g $HOME/.mozilla/plugins/libflashplayer.so

rm -f ~/.mozilla/firefox/pluginreg.dat
[ -x /usr/bin/nspluginscan ] && /usr/bin/nspluginscan 2>/dev/null
[ "$KDE_SESSION_VERSION" = "4" -a -x /usr/bin/kbuildsycoca4 ] && /usr/bin/kbuildsycoca4 2>/dev/null
