#!/bin/bash
# estudio_tools.sh version 1.0.1 (03.07.2006)
# Authors: Bernd Schoeller, Andreas Leitner, Patrick Ruckstuhl
# Most recent version: se.ethz.ch/people/leitner/estudio_tools.sh
#
# This script provides two shell functions:
#
# install_estudio [version]
#     
#      Installs the given version of Eiffel Studio. It first
#      downloads the version and then unpacks it into ${LOCAL_ROOT} 
#      (see configuration section below). When no version is given the
#      function lists the installable versions. When the version "default"
#      is given it installs the latest version. Note that this function
#      does not set any environment variables neccessary to run 
#      EiffelStudio. To accomplish this have a look at the function
#      `activate_estudio'.
#      
# activate_estudio [version]
#
#      Activates the given version of Eiffel Studio. It sets/changes
#      the environment variables ${ISE_EIFFEL}, ${ISE_LIBRARY}, and 
#      ${PATH}. When no version is given the function lists the installed
#      version of EiffelStudio. When the version "default" is given the latest
#      version will be activated.
#
# Configuration:
#
# Platform your system is running on
export ISE_PLATFORM=${ISE_PLATFORM:-linux-x86}
#
# Directory where the different versions of EiffelStudio should be installed to
ESTT_LOCAL_ROOT=${ESTT_LOCAL_ROOT:-${HOME}/estudio}
#
# Web page where Eiffel Studio can be downloaded from
ESTT_REMOTE_ROOT=${ESTT_REMOTE_ROOT:-http://eiffelsoftware.origo.ethz.ch/downloads}
#
# Installation:
#
# Save this script in a file (for example named eiffel_scripts.sh) and then
# include (via the "source" command) it in your .bashrc (for 
# example "source eiffel_scripts.sh").
#
# Requirements:
# 
# * bash
# * links
# * grep
# * fgrep
# * tar
# * mkdir
# * rm
# 
# Implementation: (Do not modify code below this line)
# ------------------------------------------------------------------------------

# Remove the argument from the PATH
function remove_path ()
{
    local tmp
    if (echo :$PATH: | fgrep :$1: > /dev/null)
        then
        tmp=${PATH/$1:}
        PATH=${tmp/:$1}
    fi
}

# Append the argument to the PATH, remove it from the path before
function append_path ()
{
    remove_path $1
    PATH=${PATH}:$1
}

function activate_estudio ()
{
    local VERSION
    local TMPIFS
    local i
    VERSION=$1
    if [ "x${VERSION}" = "x" ]
	 then
	 ls -d ${ESTT_LOCAL_ROOT}/Eiffel* 2>/dev/null| sed "s+${ESTT_LOCAL_ROOT}/Eiffel++g"
    else
	 if [ "x$VERSION" = "xdefault" ]
	     then
	     for i in $(ls -d ${ESTT_LOCAL_ROOT}/Eiffel* | sed "s+${ESTT_LOCAL_ROOT}/Eiffel++g")
		do
		VERSION=${i}
	     done
	 fi
	 if [ ! -d "${ESTT_LOCAL_ROOT}/Eiffel${VERSION}" ]
	     then
	     echo "Requested version (${VERSION}) of EiffelStudio is not installed."
	 else
	     remove_path "$ISE_EIFFEL/studio/spec/$ISE_PLATFORM/bin"
	     remove_path "$ISE_EIFFEL/build/spec/$ISE_PLATFORM/bin"
	     export ISE_EIFFEL=${ESTT_LOCAL_ROOT}/Eiffel${VERSION}
	     append_path "$ISE_EIFFEL/studio/spec/$ISE_PLATFORM/bin"
	     append_path "$ISE_EIFFEL/build/spec/$ISE_PLATFORM/bin"
	 fi
    fi
}


function install_estudio ()
{
    local VERSION
    local FILE_NAMES
    local REMOTE_VERSIONS
    local LOCAL_FILE
    local FILE_NAME
    VERSION=${1}
    FILE_NAMES=$(links -dump http://eiffelsoftware.origo.ethz.ch/download | grep "Eiffel.*-${ISE_PLATFORM}.tar.bz2" | cut '-d ' -f 6 -s)
    REMOTE_VERSIONS=$(echo $FILE_NAMES | sed "s+Eiffel++g" | sed "s+-${ISE_PLATFORM}.tar.bz2++g")
    if [ "x$VERSION" = "x" ]
        then
	 echo -e $REMOTE_VERSIONS
    else
	 if [ "x$VERSION" = "xdefault" ] 
	     then
	     VERSION=$(echo -e $REMOTE_VERSIONS | tail  -n 1)
	 fi
	 FILE_NAME=$(echo -e $FILE_NAMES | fgrep ${VERSION})
	 if [ "x${FILE_NAME}" = "x" ]
	     then
	     echo "Requested version '${VERSION}' of EiffelStudio is not available."
	 else
	     if [ -d ${ESTT_LOCAL_ROOT}/Eiffel${VERSION} ]
		  then
		  echo "EiffelStudio version '${VERSION}' is already installed. Use \"activate_estudio ${VERSION}\" to activate it."
	     else
		 if [ ! -d "${ESTT_LOCAL_ROOT}" ]
		     then
		     mkdir ${ESTT_LOCAL_ROOT}
		 fi
		 echo "Downloading EiffelStudio version ${VERSION} ..."
		 REMOTE_FILE=${ESTT_REMOTE_ROOT}/${FILE_NAME}
		 LOCAL_FILE=${ESTT_LOCAL_ROOT}/${FILE_NAME}
		 wget ${REMOTE_FILE} -q -O ${LOCAL_FILE}
		 echo "Finished download."
		 echo "Unpacking EiffelStudio version ${VERSION} ..."
		 mkdir ${ESTT_LOCAL_ROOT}/tmp
		 tar xfj ${LOCAL_FILE} -C ${ESTT_LOCAL_ROOT}/tmp/
		 rm ${LOCAL_FILE}
		 mv ${ESTT_LOCAL_ROOT}/tmp/Eiffel* ${ESTT_LOCAL_ROOT}/Eiffel${VERSION}
		 rmdir ${ESTT_LOCAL_ROOT}/tmp/
		 echo "Finished unpacking."
	     fi
	 fi
    fi
}


# Programmable completion for the ISE Eiffel estudio and ec command under bash.

shopt -s extglob

_ec()
{
        local cur prev fcmds cmds 

        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        prev=${COMP_WORDS[COMP_CWORD-1]}

        # possible expansions
        fcmds='-help -version -melt -quick_melt -freeze -finalize -precompile -c_compile -config -ace -project -target'

        # handle some special cases
        case $prev in
        -ace)
                # followed by a filename
                cmds=""
                ;;
        -config)
                # followed by a filename
                cmds=""
                ;;
        -project)
                # followed by a filename
                cmds=""
                ;;
        *)
                cmds=$fcmds
                ;;
        esac

        # can't use an expansion more than once 
        for (( i=1; i<=$COMP_CWORD-1; ++i )) ; do
                opt=${COMP_WORDS[$i]}

                cmds=${cmds/${opt}/ }
        done

        COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )

        return 0
}
complete -F _ec -o default ec

_estudio ()
{
        # estudio doesn't have any arguments
        COMPREPLY=()

        return 0
}
complete -F _estudio -o default estudio
