#!/bin/sh
#
# Name:
#    install -  script file for invoking the Multi-platform installer on Linux.
#
#    Usage: install [-h|help]
#                              [-javadir <directory>] | [-root <directory>] |
#                              [-test] | [-v|-verbose]
#                              -h|-help        - Display arguments.
#                              -test           - Don't actually run the java
#                                                command (only useful with -v).
#                              -v|-verbose     - Display settings.
#                              -javadir <directory>  - Override default java root directory.
#                              -root <directory>     - Override default DVD root directory.
#                              -tmpdir <directory>   - Override default directory for temporary files.
#
#              The default settings when no override inputs are supplied are:
#              -root = <directory containing this script>
#              -javadir = <root>/sys/java/jre/$ARCH/jre
#              -tmpdir = /tmp
#
#
# Copyright 2009-2014 The MathWorks, Inc.
#__________________________________________________________________________
#
    arg0_=$0
#
#
    trap "exit 1" 1 2 3 15
#
#========================= java_launcher (start) ============================
#
#========================= archlist.sh (start) ============================
#
# usage:        archlist.sh
#
# abstract:     This Bourne Shell script creates the variable ARCH_LIST.
#
# note(s):      1. This file is always imbedded in another script
#
# Copyright 1997-2013 The MathWorks, Inc.
#----------------------------------------------------------------------------
#
    ARCH_LIST='glnxa64 maci64'
#=======================================================================
# Functions:
#   check_archlist ()
#=======================================================================
    check_archlist () { # Sets ARCH. If first argument contains a valid
			# arch then ARCH is set to that value else
		        # an empty string. If there is a second argument
			# do not output any warning message. The most
			# common forms of the first argument are:
			#
			#     ARCH=arch
			#     MATLAB_ARCH=arch
			#     argument=-arch
			#
                        # Always returns a 0 status.
                        #
                        # usage: check_archlist arch=[-]value [noprint]
                        #
	if [ $# -gt 0 ]; then
	    arch_in=`expr "$1" : '.*=\(.*\)'`
	    if [ "$arch_in" != "" ]; then
	        ARCH=`echo "$ARCH_LIST EOF $arch_in" | awk '
#-----------------------------------------------------------------------
	{ for (i = 1; i <= NF; i = i + 1)
	      if ($i == "EOF")
		  narch = i - 1
	  for (i = 1; i <= narch; i = i + 1)
		if ($i == $NF || "-" $i == $NF) {
		    print $i
		    exit
		}
	}'`
#-----------------------------------------------------------------------
	       if [ "$ARCH" = "" -a $# -eq 1 ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo ' '
echo "    Warning: $1 does not specify a valid architecture - ignored . . ."
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	       fi
	    else
		ARCH=""
	    fi
	else
	    ARCH=""
	fi
#
	return 0
    }
#=======================================================================
#========================= archlist.sh (end) ==============================
ARCH=
#========================= arch.sh (start) ============================
#!/bin/sh
#
# usage:        arch.sh
#
# abstract:     This Bourne Shell script determines the architecture
#               of the the current machine.
#
#               ARCH      - Machine architecture
#
#               IMPORTANT: The shell function 'check_archlist' is used
#                          by this routine and MUST be loaded first.
#                          This can be done by sourcing the file,
#
#                              archlist.sh
#
#                          before using this routine.
#
# note(s):      1. This routine must be called using a . (period)
#
#               2. Also returns ARCH_MSG which may contain additional
#                  information when ARCH returns 'unknown'.
#
# Copyright 1986-2011 The MathWorks, Inc.
#----------------------------------------------------------------------------
#
#=======================================================================
# Functions:
#   realfilepath ()
#   matlab_arch ()
#=======================================================================
    realfilepath () { # Returns the actual path in the file system
                      # of a file. It follows links. It returns an
                      # empty path if an error occurs.
                      #
                      # Returns a 1 status if the file does not exist
                      # or appears to be a circular link. Otherwise,
                      # a 0 status is returned.
                      #
                      # usage: realfilepath filepath
                      #
    filename=$1
#
# Now it is either a file or a link to a file.
#
    cpath=`pwd`

#
# Follow up to 8 links before giving up. Same as BSD 4.3
#
      n=1
      maxlinks=8
      while [ $n -le $maxlinks ]
      do
#
# Get directory correctly!
#
	newdir=`echo "$filename" | awk '
                        { tail = $0
                          np = index (tail, "/")
                          while ( np != 0 ) {
                             tail = substr (tail, np + 1, length (tail) - np)
                             if (tail == "" ) break
                             np = index (tail, "/")
                          }
                          head = substr ($0, 1, length ($0) - length (tail))
                          if ( tail == "." || tail == "..")
                             print $0
                          else
                             print head
                        }'`
	if [ ! "$newdir" ]; then
	    newdir="."
	fi
	(cd "$newdir") > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    return 1
	fi
	cd "$newdir"
#
# Need the function pwd - not the built in one
#
	newdir=`/bin/pwd`
#
	newbase=`expr //"$filename" : '.*/\(.*\)' \| "$filename"`
        lscmd=`ls -ld "$newbase" 2>/dev/null`
	if [ ! "$lscmd" ]; then
	    return 1
	fi
#
# Check for link portably
#
	if [ `expr "$lscmd" : '.*->.*'` -ne 0 ]; then
	    filename=`echo "$lscmd" | awk '{ print $NF }'`
	else
#
# It's a file
#
	    dir="$newdir"
	    command="$newbase"
#
	    cd "$dir"
#
# On Mac OS X, the -P option to pwd causes it to return a resolved path, but
# on 10.5, -P is no longer the default, so we are now passing -P explicitly
#
            if [ "$ARCH" = 'mac' -o "$ARCH" = 'maci' -o "$ARCH" = 'maci64' ]; then
                echo `/bin/pwd -P`/$command
#
# The Linux version of pwd returns a resolved path by default, and there is
# no -P option
#
            else
                echo `/bin/pwd`/$command
            fi
	    break
	fi
	n=`expr $n + 1`
      done
      if [ $n -gt $maxlinks ]; then
	return 1
      fi

    cd "$cpath"
    }
#
#=======================================================================
    set_mac_arch() {
        # First check to see if maci64 is even possible on this hardware
        if [ "`/usr/sbin/sysctl -n hw.cpu64bit_capable`" = "0" ]; then
            # maci64 is not possible. So set the arch to maci.
            ARCH="maci"
            return
        fi

        # Now check to see if maci64 is asked for
        if [ "$MACI64" = "0" ]; then
            # only maci is wanted, so arch is maci.
            ARCH="maci"
            return
        fi

        # If we get to this point, maci64 is available and desired. So, check to
        # see if 64 bit binaries are available. First, if $MATLABROOT is NOT
        # set, we can't really check for anything else.
        if [ "$MATLABROOT" = "" ]; then
            ARCH="maci64";
            return
        fi


        # if we get to this point, we need to check the binaries that we have to
        # find out if we have maci64 binaries
        if [ -d "$MATLABROOT/bin/maci64" ]; then
            ARCH="maci64"
            return
        fi

        # if we get to this point, even though maci64 is possible and desired,
        # the maci64 binaries aren't available, so fall back to maci
        ARCH="maci"
    }

#
#=======================================================================
    matlab_arch () {  # Determine the architecture for MATLAB
                      # It returns the value in the ARCH variable.
                      # If 'unknown' is returned then sometimes a
                      # diagnostic message is returned in ARCH_MSG.
                      #
                      # Always returns a 0 status.
                      #
                      # usage: matlab_arch
                      #
        ARCH="unknown"
#
        if [ -f /bin/uname ]; then
            case "`/bin/uname`" in
                SunOS)                                  # Solaris
                    case "`/bin/uname -p`" in
                        sparc)
                            ARCH="sol64"
                            ;;
                        i386)
                            ARCH="sola64"
                            ;;
                    esac
                    ;;
                Linux)
                    case "`/bin/uname -m`" in
                        i*86)
                            ARCH="glnx86"
                            ;;
                        x86_64)
                            ARCH="glnxa64"
                            ;;
                    esac
                    ;;
# Usually uname lives in /usr/bin on the Mac, but sometimes people
# have links in /bin that link uname to /usr/bin.  Because of this
# Mac needs to be listed in the checks for both /bin/uname and /usr/bin/uname
                Darwin)                                 # Mac OS X
                    case "`/bin/uname -p`" in
                        i386)
                            set_mac_arch
                            ;;
                        esac
                    ;;
                *)
                    :
                    ;;
            esac
        elif [ -f /usr/bin/uname ]; then
            case "`/usr/bin/uname`" in
                Darwin)                                 # Mac OS X
                    case "`/usr/bin/uname -p`" in
                        i386)
                            set_mac_arch
                            ;;
                        esac
                    ;;
            esac
        fi
        return 0
    }
#=======================================================================
#
# The local shell function check_archlist is assumed to be loaded before this
# function is sourced.
#
    ARCH_MSG=''
    check_archlist ARCH=$ARCH
    if [ "$ARCH" = "" ]; then
        if [ "$MATLAB_ARCH" != "" ]; then
            check_archlist MATLAB_ARCH=$MATLAB_ARCH
        fi
        if [ "$ARCH" = "" ]; then
            matlab_arch
        fi
    fi
    Arch=$ARCH
#========================= arch.sh (end) ==============================
    setMac () {
        if [ "$ARCH" = "maci" -o "$ARCH" = "maci64" ]; then
            ISMAC=1
        else
            ISMAC=0
        fi
        export ISMAC
         
    }
#=======================================================================
    setVMenvironment () {
    #   Set up some variables for the VM environment

    # Augment with AWT Motif default locale resource files
    XFILESEARCHPATH="$JRE_LOC/lib/locale/%L/%T/%N%S:$XFILESEARCHPATH"
    export XFILESEARCHPATH

    # Determine <final_load_library_path> for each platform
    #
    libraryPathsToAdd=$1
    
    case "$ARCH" in
        glnx*)
            LD_LIBRARY_PATH="`eval echo $LD_LIBRARY_PATH`"
            if [ "$LD_LIBRARY_PATH" != "" ]; then
                LD_LIBRARY_PATH="$libraryPathsToAdd":$LD_LIBRARY_PATH
            else
                LD_LIBRARY_PATH="$libraryPathsToAdd"
            fi
            export LD_LIBRARY_PATH
            ;;
        mac*)
            DYLD_LIBRARY_PATH="`eval echo $DYLD_LIBRARY_PATH`"
            if [ "$DYLD_LIBRARY_PATH" != "" ]; then
                DYLD_LIBRARY_PATH="$libraryPathsToAdd":$DYLD_LIBRARY_PATH
            else
                DYLD_LIBRARY_PATH="$libraryPathsToAdd"
            fi
            export DYLD_LIBRARY_PATH
            ;;
        *)
            LD_LIBRARY_PATH="`eval echo $LD_LIBRARY_PATH`"
            if [ "$LD_LIBRARY_PATH" != "" ]; then
                LD_LIBRARY_PATH=$LD_LIBRARY_PATH
            else
                LD_LIBRARY_PATH=
            fi
            export LD_LIBRARY_PATH
            ;;
    esac

    }
#=======================================================================
    setVMpath () {
    #
    # Determine the java vm path for each platform.
    #
    javaRoot=$1
    DEFAULT_JRE_LOC=$javaRoot/jre/$ARCH/jre

    if [ "$JRE_LOC" = "" ]; then
        JRE_LOC=$DEFAULT_JRE_LOC
    fi
#
# Look for JRE
#
    if [ ! -d "$JRE_LOC" ]; then
        echo "---------------------------------------------------------------------------"
        echo "Error: Cannot locate Java Runtime Environment (JRE)."
        echo "The directory $JRE_LOC does not exist."
        echo "---------------------------------------------------------------------------"
        exit 1
    fi
    }
#=======================================================================
    getJarPaths () {
    # Get the full search path for our jar files
    # Echos the search path to stdout.

    JAVA_DIR="$1/java"
    JAR_LOC="$JAVA_DIR/jar"
    JAREXT_LOC="$JAVA_DIR/jarext"
    JAR_SEARCH_PATHtmp=$JRE_LOC/lib/ext

    # Get list of directories in JAR_LOC
    theDirList=`find "$JAR_LOC" -type d`
    # Add all subdirectories of JAR_LOC (including recursive search)
    for aDir in $theDirList
    do
        JAR_SEARCH_PATHtmp=$JAR_SEARCH_PATHtmp:$aDir
    done

    # Get list of directories in JAREXT_LOC
    theDirList=`find "$JAREXT_LOC" -type d`
    # Add all subdirectories of JAREXT_LOC (including recursive search)
    for aDir in $theDirList
    do
        # Do not add connector to our path since it brings in older
        # versions of some other third party classes.
        if [ "$aDir" != "$JAREXT_LOC/connector" ]; then
            JAR_SEARCH_PATHtmp=$JAR_SEARCH_PATHtmp:$aDir
        fi
    done

    # If MW_LIB_EXT_DIRS env var set, append value to existing search path
    if [ "$MW_LIB_EXT_DIRS" != "" ]; then
        JAR_SEARCH_PATHtmp=$JAR_SEARCH_PATHtmp:$MW_LIB_EXT_DIRS
    fi

    echo $JAR_SEARCH_PATHtmp
    }

#=======================================================================
    copyDirWritable() { 
    # Copy an entire directory.  This will create the destination 
    # directory, do a recursive copy, and then make all the files and 
    # directories writable.
    # usage: copyDirWritable src dest
    srcDir=$1
    destDir=$2

    mkdir -p "$destDir"
    cp -RLf "$srcDir"/* "$destDir"
    chmod -R +w "$destDir" 2>/dev/null

    }
#========================= java_launcher (end) ==============================
    copyJREAndJarsToTmp () {
    #
    # Copy JRE and Jars to tmp
    #
    echo "Preparing installation files ..."

    NEW_JRE_LOC="$TEMP_DIR/sys/java/jre/$ARCH/jre"
    copyDirWritable "$JRE_LOC" "$NEW_JRE_LOC"
    JRE_LOC=$NEW_JRE_LOC

    JAVA_DIR="$TEMP_DIR/java"
    JAR_LOC="$JAVA_DIR/jar"
    JAREXT_LOC="$JAVA_DIR/jarext"
    copyDirWritable "$ROOT/java" "$JAVA_DIR"
    }
#=======================================================================
    doLinuxCheck () {
        if [ "$ARCH" = 'glnxa64' ]; then
            # If this is Red Hat, check if this is version 5
            if [ -f /etc/redhat-release ]; then
                RHOUT=`grep -i "release 5" /etc/redhat-release`
                if [ $? -eq 0 ]; then
                    echo "This system appears to be running Red Hat Enterprise Linux 5."
                    echo "R2013a is the last release to support this distribution."
                    echo "See http://www.mathworks.com/support/sysreq/roadmap.html for more roadmap information."
                    exit 1;
                fi
            fi
        fi
    }
#=======================================================================
    getJavaLaunchTarget () {
    #   Find jar file to execute.
    # TODO: Consolidate this code with getClassName() from activation and 
    #       move to java_launcher_template

    MAIN_CLASS="com/mathworks/professionalinstaller/Launcher"

    BOOTSTRAP_PROPERTIES=$ROOT/java/bootstrap.properties

    if [ -f "$BOOTSTRAP_PROPERTIES" ]; then
        MAIN_CLASS_OVERRIDE=`grep -i mainclass "$BOOTSTRAP_PROPERTIES" | awk -F= '{ print $2 }' | sed 's/ //g'`
        if [ "$MAIN_CLASS_OVERRIDE" != "" ]; then
            MAIN_CLASS=$MAIN_CLASS_OVERRIDE
        fi
    fi

    #   By default, set launch target to main class
    JAVA_LAUNCH_TARGETtmp=$MAIN_CLASS

    #   If MW_MAIN_CLASS env var set, set launch target to "$MW_MAIN_CLASS"
    if [ "$MW_MAIN_CLASS" != "" ]; then
        JAVA_LAUNCH_TARGETtmp=$MW_MAIN_CLASS
    fi

    echo $JAVA_LAUNCH_TARGETtmp
    }
#=======================================================================
#
# Parse the arguments
#
    stat="OK"
    JAR_LOC=
    JAREXT_LOC=
    JAR_SEARCH_PATH=
    VERBOSE=0
    TEMP_DIR="/tmp/mathworks_$$"
    JRE_LOC=
    XTRAFLAGS=
    ROOT=
    TESTONLY=0
    arglist=
    SHOW_SPLASH=1
    INSTALLBUNDLE=0
    BOOTSTRAP_PATH=
    TEMP_DIR_SET=0

#   Set default values for some variables if they are not set yet.
    if [ "$ROOT" = "" ]; then
        SETUP_FILE=`realfilepath "$0"`
        ROOT=`dirname "$SETUP_FILE"`
    fi

    while [ "$stat" = "OK" -a  $# -gt 0 ]; do
        case "$1" in
            -h|-help)
                stat=""
                ;;
            -v|-verbose)
                VERBOSE=1;
                ;;
            -javadir)
                if [ $# -eq 1 ]; then
                    echo "A JRE directory must be specified with -javadir."
                    stat=""
                else
                    shift
                    JRE_LOC=$1;

                    if [ ! -d "$JRE_LOC" ]; then
                        echo "The directory $JRE_LOC does not exist."
                        stat=""
                    fi
                    if [ ! -d "$JRE_LOC/lib" ]; then
                        echo "$JRE_LOC does not appear to be a JRE directory."
                        stat=""
                    fi
                fi
                ;;
            -root)
                if [ $# -eq 1 ]; then
                    echo "A DVD directory must be specified with -root."
                    stat=""
                else
                    shift
                    ROOT=$1;
                    if [ ! -d "$ROOT" ]; then
                        echo "The directory $ROOT does not exist."
                        stat=""
                    fi
                fi
                ;;
            -tmpdir)
                if [ $# -eq 1 ]; then
                    echo "A directory for temporary files must be specified with -tmpdir."
                    stat=""
                else
                    shift
                    TEMP_DIR="$1/mathworks_$$"
                    TEMP_DIR_SET=1
                fi
                ;;
            -test)
                # Just run through script without actually running the java
                # command.  Only useful with -v.
                TESTONLY=1
                ;;
            -psn*)
                # Apple passes this in with things that are double clicked. Ignore.
                ;;
            -*)
                OPTION=$1
                OPTION="$(echo ${OPTION} | tr '[:upper:]' '[:lower:]')"
                if [ $OPTION = "-mode" ]; then
                    SHOW_SPLASH=0
                fi
                if [ $OPTION = "-inputfile" ]; then
                    SHOW_SPLASH=0
                fi
				if [ $OPTION = "-installbundle" ]; then
                    SHOW_SPLASH=0
                    arglist="$arglist \"$1\""
                    shift
                    BOOTSTRAP_PATH=`dirname "$1"`                    
                    INSTALLBUNDLE=1
                fi
                found=0
                # Check for -arch
                arch=$ARCH
                check_archlist argument=$1 noprint
                if [ "$ARCH" != "" ]; then
                    found=1
                else
                    ARCH=$arch
                fi
                if [ "$found" = "0" ]; then
                   arglist="$arglist \"$1\""
                fi
                ;;
            *)
                   arglist="$arglist \"$1\""
                ;;
        esac
        shift
    done
    setMac
#
# Check for errors
#
    if [ "$stat" != "OK" ]; then        # An error occurred.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo ""
    echo "   Usage: install [-h|help]"
    echo "                             [-javadir <directory>] | [-root <directory>] |"
    echo "                             [-test] | [-v|-verbose]"
    echo ""
    echo "    -h|-help        - Display arguments."
    echo "    -test           - Don't actually run the java command (only useful with -v)."
    echo "    -v|-verbose     - Display settings."
    echo "    -javadir <directory>       - Override default java root directory."
    echo "    -root <directory>          - Override default DVD root directory."
    echo "    -tmpdir <directory>        - Override default directory for temporary files."
    echo ""
    echo "    The default settings when no override inputs are supplied are: "
    echo "    -root = <directory containing this script>"
    echo "    -javadir = <root>/sys/java/jre/$ARCH/jre"
    echo "    -tmpdir = /tmp"
    echo ""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        exit 1
    fi

#   Check OS level to warn about future change
    doLinuxCheck

#   Determine the java vm path for each platform.
    setVMpath "$ROOT/sys/java"
    copyJREAndJarsToTmp

#   Set up some variables for the VM environment.
    copyDirWritable "$ROOT/bin/$ARCH" "$TEMP_DIR/bin/$ARCH"
    setVMenvironment "$TEMP_DIR/bin/$ARCH"

#   Get the jar directories
    JAR_SEARCH_PATH=`getJarPaths "$TEMP_DIR"`

#   Get the java launch target
    JAVA_LAUNCH_TARGET=`getJavaLaunchTarget`

#   Set up splash option
    if [ $SHOW_SPLASH -eq 1 ]; then
        SPLASH_OPTION="-splash:\"$ROOT/java/splash.png\""
    else
        SPLASH_OPTION=""
    fi

#   Set up java command to run.                  
    if [ $ISMAC -eq 1 ]; then
        if [ $INSTALLBUNDLE -eq 1 ]; then        
            XTRAFLAGS="-d64 -Xdock:name=\"Install\" -Xdock:icon=\"$BOOTSTRAP_PATH/installer.icns\""             
        else 
            XTRAFLAGS="-d64 -Xdock:name=\"Install MATLAB\" -Xdock:icon=\"$ROOT/InstallForMacOSX.app/Contents/Resources/membrane.icns\""
        fi
    fi

    if [ $TEMP_DIR_SET -eq 1 ]; then
        TMPDIRFLAGS="-Djava.io.tmpdir=$TEMP_DIR"
    else
        TMPDIRFLAGS=""
    fi
    java_exe=$JRE_LOC/bin/java
    java_cmd="$java_exe -Xmx512m $XTRAFLAGS $TMPDIRFLAGS $SPLASH_OPTION -Djava.ext.dirs=$JAR_SEARCH_PATH $JAVA_LAUNCH_TARGET -root \"$ROOT\" -tmpdir \"$TEMP_DIR\" $arglist"

#
    if [ "$VERBOSE" = "1" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "->  DVD                 = $ROOT"
    echo "->  ARCH                = $ARCH"
    echo "->  DISPLAY             = $DISPLAY"
    echo "->  TESTONLY            = $TESTONLY"
    echo "->  JRE_LOC             = $JRE_LOC"
#
        case "$ARCH" in
            mac*)
    echo "->  DYLD_LIBRARY_PATH   = $DYLD_LIBRARY_PATH"
                ;;
            *)
    echo "->  LD_LIBRARY_PATH     = $LD_LIBRARY_PATH"
                ;;
        esac

    echo " "
    echo "Command to run:"
    echo "$java_cmd"
    echo " "
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fi
#
#
#   Run the java command to start the install app
    if [ "$TESTONLY" != "1" ]; then
        echo "Installing ..."
        eval $java_cmd

#   Clean up
        rm -rf $TEMP_DIR
    fi

    echo "Finished"