#!/bin/sh
#
# Copyright(C) 2002-2009 Adobe Macromedia Software LLC.  All rights reserved.
#
# Adobe Flash Player Installer (Standalone)
#

PRODUCT="Adobe Flash Player"
VERSION="9"
PLATFORM="Linux"
TYPE="(Standalone)"

# Environment variables
PATH=.:/bin:/usr/bin:/usr/local/bin:/sbin:$PATH
export PATH

# Get the path of this script
cwd=`dirname $0`

# Minimum glibc
MIN_GLIBCMAJOR=2
MIN_GLIBCMINOR=3


##############################
# Subroutines
##############################

# the os is not supported
exit_os () {
  echo ""
  echo "ERROR: Your operating system is not supported by the"
  echo "       $PRODUCT installer."
  echo ""
  exit 1
}

# the architecture is not supported
exit_cpu () {
  echo ""
  echo "ERROR: Your architecture, \'$1\', is not supported by the"
  echo "       $PRODUCT installer."
  echo ""
  exit 1
}

# glibc is older than supported
exit_glibc () {
  echo ""
  echo "ERROR: Your glibc library is older than $MIN_GLIBCMAJOR.$MIN_GLIBCMINOR."
  echo "       Please update your glibc library."
  echo ""
  exit 1
}

# check glibc
check_glibc () {
  ICONV=`iconv --version | sed -e '2,$d'`
  if [ $? -ne 0 ]; then
    echo "no-iconv"
  else
    ICONVVER=`echo "$ICONV" | awk '{print $4}'`
    GLIBCMAJOR=`echo $ICONVVER | cut -d'.' -f1`
    GLIBCMINOR=`echo $ICONVVER | cut -d'.' -f2`
    if [ \( $GLIBCMAJOR -ge $MIN_GLIBCMAJOR \) -a \( $GLIBCMINOR -ge $MIN_GLIBCMINOR \) ]; then
      echo "valid-glibc"
    else
      echo "invalid-glibc"
    fi
  fi
}

# check entered directory
check_installdir () {
  CHECKDIR="$1"
  # blank?
  if [ -z "$CHECKDIR" ]; then
    echo "blank"
    return
  fi
  # dir exists and is writable?
  if [ -d "$CHECKDIR" ]; then
    if [ -w "$CHECKDIR" ]; then
      echo "valid"
      return
    else
      echo "invalid-not-writable"
      return
    fi
  elif [ -f "$CHECKDIR" ]; then
    echo "invalid-file"
    return
  elif [ ! -d "$CHECKDIR" ]; then
    echo "not_exist"
    return
  fi
  echo "invalid"
}

# fix dir if necessary
fix_dir () {
  FIXDIR="$1"
  FIRSTCHAR=`expr "$FIXDIR" : '\(.\).*'`
  if [ "$FIRSTCHAR" != '/' ]; then
    currentdir=`pwd`
    echo "$currentdir/$FIXDIR"
  else
    echo "$1"
  fi
}


##############################
# Main Section
##############################

ROOTINSTALL=0

# check user
USERID=`id | sed -e 's/).*//; s/^.*(//;'`
if [ "X$USERID" = "Xroot" ]; then
  ROOTINSTALL=1
fi

# check OS
os=`uname -s`
if [ "X$os" != "XLinux" ]; then
  exit_os
fi

# check architecture
TEMPARCH=`uname -m`
case $TEMPARCH in
  i[3456]86)
    ARCH=i386
    ;;
  *)
    exit_cpu $TEMPARCH
    ;;
esac

# check for iconv and version of glibc
GLIBCSTATUS=`check_glibc`
case $GLIBCSTATUS in
  invalid-glibc)
    exit_glibc
    ;;
esac

##################
# Welcome user
##################
echo ""
echo "Copyright(C) 2002-2009 Adobe Macromedia Software LLC.  All rights reserved."
echo ""
echo "$PRODUCT $VERSION for $PLATFORM $TYPE"
echo ""
echo "$PRODUCT $VERSION $TYPE will be installed on this machine."
echo ""
if [ $ROOTINSTALL -eq 1 ]; then
echo "You are running the $PRODUCT $VERSION $TYPE installer as"
echo "the \"root\" user. $PRODUCT $VERSION $TYPE will be"
echo "installed system-wide."
else
echo "You are running the $PRODUCT $VERSION $TYPE installer as"
echo "a non-root user. $PRODUCT $VERSION $TYPE will be installed in"
echo "your home directory."
fi
echo ""
echo "Support is available at http://www.adobe.com/support/flashplayer/"
echo ""
echo "To install $PRODUCT $VERSION $TYPE now, press ENTER."
echo ""
echo "To cancel the installation at any time, press Control-C."
echo ""
read cont < /dev/tty

echo ""
echo "NOTE: $PRODUCT requires two font packages"
if [ -e /etc/debian_version ]; then
echo "      to be installed, gsfonts and gsfonts-x11."
else
echo "      to be installed, ttfonts and urw-fonts."
fi
echo ""
echo "Press ENTER to continue..."
echo ""
read cont < /dev/tty

# Loop until user is comfortable with their choices
okToProceed=0
while [ $okToProceed -eq 0 ]; do

  # default variables
  DIRSTATUS=""
  HOMEDIR=""
  createdir=0
  installicon=0

  ############################
  # Get destination directory
  ############################
  get_installdir () {
    HOMEDIR=`(cd ; pwd)`
    if [ $ROOTINSTALL -eq 1 ]; then
      INSTALLDIR="/usr/bin"
    else
      INSTALLDIR="$HOMEDIR/bin"
    fi

    echo ""
    echo "$PRODUCT $VERSION $TYPE will be installed to the default"
    echo "destination directory."
    echo ""
    echo "To install to the default directory, press the 'Enter' key."
    echo "To install to a different destination directory, type in the full path of the"
    echo "directory, then press the 'Enter' key."
    echo ""
    printf "Default destination directory [$INSTALLDIR]: "
    read dir

    if [ ! -z "$dir" ]; then
      INSTALLDIR="$dir"

      # fix the entered dir if necessary
      FIXED_DIR=`fix_dir "$INSTALLDIR"`
      INSTALLDIR="$FIXED_DIR"
    fi


    # check given installdir if valid
    DIRSTATUS=`check_installdir "$INSTALLDIR"`

    case $DIRSTATUS in
      blank)
        echo ""
        echo "WARNING: Please do not enter a blank destination directory."
        echo ""
        get_installdir
        ;;
      invalid-file)
        echo ""
        echo "WARNING: You have entered the path to a file."
        echo "         Please enter a valid destination directory."
        echo ""
        get_installdir
        ;;
      invalid)
        echo ""
        echo "WARNING: Please enter a valid destination directory."
        echo ""
        get_installdir
        ;;
      invalid-not-writable)
        echo ""
        echo "WARNING: $INSTALLDIR is not writable."
        echo ""
        get_installdir
        ;;
      not_exist)
        get_answer () {
          echo ""
          echo "WARNING: $INSTALLDIR does not exist."
          echo ""
          printf "Create the directory? (y/n): "
          read yn
          case $yn in
            y | Y)
              # test dir
              mkdir -p $INSTALLDIR >/dev/null 2>&1
              # success
              if [ $? -eq 0 ]; then
                rmdir -p $INSTALLDIR >/dev/null 2>&1
                createdir=1
                return
              else
                echo ""
                echo "ERROR: The installer cannot create $INSTALLDIR."
                echo "       Please enter a valid destination directory."
                echo ""
                get_installdir
              fi
              ;;
            n | N)
              get_installdir
              ;;
            *)
              echo ""
              echo "Please enter 'y' or 'n'."
              get_answer
              ;;
          esac
        }
        get_answer
        ;;
      valid)
        ;;
    esac
  }
  get_installdir

  #################################
  # Ask to install icon on desktop
  #################################
  ask_to_install_icon () {
    echo ""
    echo "Do you want to install the $PRODUCT $VERSION $TYPE icon on your"
    printf "desktop? (y/n): "
    read yn
    case $yn in
      y | Y)
        installicon=1
        ;;
      n | N)
        return
        ;;
      *)
        echo ""
        echo "Please enter 'y' or 'n'."
        ask_to_install_icon
        ;;
    esac
  }
  ask_to_install_icon


  ##########
  # Summary
  ##########
  echo ""
  echo ""
  echo "----------- Install Action Summary -----------"
  echo ""
  echo "$PRODUCT $VERSION $TYPE will be installed"
  echo "to the following destination directory:"
  echo ""
  echo "Destination directory   = $INSTALLDIR"
  if [ $installicon -eq 1 ]; then
  echo "Install icon to desktop = Yes"
  else
  echo "Install icon to desktop = No"
  fi
  echo ""

  # okay to continue?
  get_installagreement () {
    printf 'Proceed with the installation? (y/n/q): '
    read yn

    case $yn in
      y | Y)
        okToProceed=1
        ;;
      n | N)
        continue
        ;;
      q | Q)
        exit 1
        ;;
      *)
        echo ""
        echo "Please enter 'y', 'n', or 'q'."
        get_installagreement
        ;;
    esac
  }
  get_installagreement

done


#######################
# Perform installation
#######################

# create desktop directories if necessary
if [ $ROOTINSTALL -eq 0 ]; then
  # Gnome
  if [ ! -d "$HOMEDIR/.gnome" ]; then
    mkdir "$HOMEDIR/.gnome"
  fi
  # KDE
  if [ ! -d "$HOMEDIR/.kde" ]; then
    mkdir "$HOMEDIR/.kde"
  fi
fi

#------------------------
# copy standalone player
#------------------------
if [ $createdir -eq 1 ]; then
  mkdir -p "$INSTALLDIR"
fi
cp -f "$cwd/flashplayer" "$INSTALLDIR"

#----------------------
# copy MIME type files
#----------------------
if [ $ROOTINSTALL -eq 1 ]; then
  # system-wide
  # Gnome
  cp -rf "$cwd/usr/share/mime-info" /usr/share
  # KDE
  cp -rf "$cwd/usr/share/mimelnk" /usr/share
else
  # local
  # Gnome
  cp -rf "$cwd/usr/share/mime-info" "$HOMEDIR/.gnome"
  # KDE
  cp -rf "$cwd/usr/share/mimelnk" "$HOMEDIR/.kde/share"
fi

#-----------------
# copy icon files
#-----------------
if [ $ROOTINSTALL -eq 1 ]; then
  # system-wide
  # Gnome
  if [ ! -d "/usr/share/pixmaps" ]; then
    mkdir -p "/usr/share/pixmaps"
  fi
  cp -f "$cwd/usr/share/icons/hicolor/48x48/apps/flash.png" /usr/share/pixmaps
  cp -f "$cwd/usr/share/icons/hicolor/48x48/mimetypes/swf.png" /usr/share/pixmaps
  # KDE
  cp -rf "$cwd/usr/share/icons" /usr/share
else
  # local
  # Gnome
  if [ ! -d "$HOMEDIR/.icons" ]; then
    mkdir -p "$HOMEDIR/.icons"
  fi
  cp -f "$cwd/usr/share/icons/hicolor/48x48/apps/flash.png" "$HOMEDIR/.icons"
  cp -f "$cwd/usr/share/icons/hicolor/48x48/mimetypes/swf.png" "$HOMEDIR/.icons"
  cp -rf "$cwd/usr/share/icons/gflashplayer128.xpm" "$HOMEDIR/.icons/gflashplayer128.xpm"
  # KDE
  cp -rf "$cwd/usr/share/icons" "$HOMEDIR/.kde/share"
fi

#-----------------------
# copy file association
#-----------------------
if [ $ROOTINSTALL -eq 1 ]; then
  # system-wide
  # Gnome
  cp -rf "$cwd/usr/share/application-registry" /usr/share
  # for Red Hat 8.0 or greater
  if [ -d "/usr/share/applications" ]; then
    cp -f "$cwd/usr/share/applnk/Internet/flashplayer.desktop" /usr/share/applications
  fi
  # KDE
  cp -rf "$cwd/usr/share/applnk" /usr/share
else
  # local
  # Gnome
  if [ ! -d "$HOMEDIR/.gnome/application-info" ]; then
    mkdir -p "$HOMEDIR/.gnome/application-info"
  fi
  cp -f "$cwd/usr/share/application-registry/flashplayer.applications" "$HOMEDIR/.gnome/application-info"
  # KDE
  cp -rf "$cwd/usr/share/applnk" "$HOMEDIR/.kde/share"
  if [ ! -d "$HOMEDIR/.kde/share/applnk-redhat" ]; then
    mkdir -p "$HOMEDIR/.kde/share/applnk-redhat"
  fi
  cp -f "$cwd/usr/share/applnk/Internet/flashplayer.desktop" "$HOMEDIR/.kde/share/applnk-redhat"
fi

#-----------------------------
# modify flashplayer.desktop
#-----------------------------
if [ $ROOTINSTALL -eq 0 ]; then
  # KDE changes
  cat "$HOMEDIR/.kde/share/applnk/Internet/flashplayer.desktop" | sed -e "s%Exec=flashplayer%Exec=$INSTALLDIR/flashplayer%g" > "$HOMEDIR/.kde/share/applnk/Internet/flashplayer.desktop.temp"
  mv -f "$HOMEDIR/.kde/share/applnk/Internet/flashplayer.desktop.temp" "$HOMEDIR/.kde/share/applnk/Internet/flashplayer.desktop"
fi

#-----------------
# modify swf.keys
#-----------------
if [ $ROOTINSTALL -eq 0 ]; then
  cat "$HOMEDIR/.gnome/mime-info/swf.keys" | sed -e "s%icon_filename=%icon_filename=$HOMEDIR/.icons/%g" > "$HOMEDIR/.gnome/mime-info/swf.keys.temp"
  mv -f "$HOMEDIR/.gnome/mime-info/swf.keys.temp" "$HOMEDIR/.gnome/mime-info/swf.keys"
fi

#----------------------
# copy icon to desktop
#----------------------
if [ $installicon -eq 1 ]; then
  if [ ! -d "$HOMEDIR/Desktop" ]; then
    mkdir -p "$HOMEDIR/Desktop"
  fi
  if [ ! -d "$HOMEDIR/.gnome-desktop" ]; then
    mkdir -p "$HOMEDIR/.gnome-desktop"
  fi
  if [ $ROOTINSTALL -eq 1 ]; then
    cp -f /usr/share/applnk/Internet/flashplayer.desktop "$HOMEDIR/Desktop/Adobe Flash Player"
    cp -f /usr/share/applnk/Internet/flashplayer.desktop "$HOMEDIR/.gnome-desktop"
  else
    cp -f "$HOMEDIR/.kde/share/applnk/Internet/flashplayer.desktop" "$HOMEDIR/Desktop/Adobe Flash Player"
    cp -f "$HOMEDIR/.kde/share/applnk/Internet/flashplayer.desktop" "$HOMEDIR/.gnome-desktop"
  fi
fi

#-----------------
# set permissions
#-----------------
chmod 755 "$INSTALLDIR/flashplayer"

# Final messages.

echo ""
echo ""
echo "Please log out of this session and log in for the changes to take effect."
echo ""

echo ""
echo "The $PRODUCT installation is complete."
echo ""
