#!/usr/bin/env bash
#
# IEs 4 Linux
# Developed by: Sergio Luis Lopes Junior <slopes at gmail dot com>
# Project site: http://tatanka.com.br/ies4linux
#
# Released under the GNU GPL. See LICENSE for more information

# Discover ies4linux installation folder
# Check current system
OSNAME=`uname`
export OSNAME

IES4LINUX=`dirname "$0"`
SOURCE_PATH=`pwd`
cd "$IES4LINUX"
export IES4LINUX=`pwd`

# NEW
# Initialize $WINEPREFIX
if [ -z "$WINEPREFIX" ] ; then WINEPREFIX="$HOME"/.wine ; fi
export WINEPREFIX

#test, if script is launched not in $WINEPREFIX/dosdevices/c: directory
if ! echo "$IES4LINUX" | grep "$WINEPREFIX/dosdevices/c:" >/dev/null 2>/dev/null; then {
    echo -e "Please use 'ieinstall' command for install IE."
    read answer
    exit
}
fi
# NEW #

# Create log file
# mkdir -p "$HOME"/.ies4linux
# touch "$HOME"/.ies4linux/log
# LOG="$HOME/.ies4linux/log"

# Instantiate variables
INSTALLIE6=1

# Default options.
# Corresponding config variables are named as below minus the trailing _DEFAULT
INSTALLIE55_DEFAULT=0
INSTALLIE5_DEFAULT=0
INSTALLFLASH_DEFAULT=0
CREATE_ICON_DEFAULT=1
#BASEDIR_DEFAULT="$HOME/.ies4linux"
BASEDIR_DEFAULT="$WINEPREFIX"
BASEDIR="$WINEPREFIX"
#BINDIR_DEFAULT="$HOME/bin"
BINDIR_DEFAULT="$BASEDIR/dosdevices/c:/windows/profiles/All Users/Application Data"
WGETFLAGS_DEFAULT="--continue"
DOWNLOADDIR_SUFFIX="dosdevices/c:/windows/downloads"
DOWNLOADDIR_DEFAULT="$BASEDIR_DEFAULT/$DOWNLOADDIR_SUFFIX"

#IE6_LOCALE_DEFAULT="EN-US"
IE6_LOCALE_DEFAULT="RU"
IE6_LOCALES="EN-US PT-BR DE FR ES IT NL SV JA KO NO DA CN TW FI PL HU AR HE CS PT RU EL TR"

# Easter eggs
INSTALLIE1=0
INSTALLIE15=0
INSTALLIE2=0

#SCR56EN or SCR56RU
SCR56='00000'

# Basic imports
. lib/functions.sh
. lib/terminal.sh
. lib/messages.sh

# Get some command-line options.
# If an option is specified on the command line, the user won't be asked for it later on.
while [ $# -gt 0 ]; do
    case "$1" in
#      --install-ie55)     INSTALLIE55=1;   shift ;;
#      --no-install-ie55)  INSTALLIE55=0;   shift ;;
#      --install-ie5)      INSTALLIE5=1;    shift ;;
#      --no-install-ie5)   INSTALLIE5=0;    shift ;;
#      --install-ie1)      INSTALLIE1=1;    shift ;;
#      --install-ie15)     INSTALLIE15=1;    shift ;;
#      --install-ie2)      INSTALLIE2=1;    shift ;;
      --install-flash)    INSTALLFLASH=1;  shift ;;
      --no-install-flash) INSTALLFLASH=0;  shift ;;
      --install-icon)     CREATE_ICON=1;   shift ;;
      --no-install-icon)  CREATE_ICON=0;   shift ;;
#      --basedir)           BASEDIR=$2;     shift 2 ;;
#      --bindir)            BINDIR=$2;      shift 2 ;;
#      --downloaddir)	   DOWNLOADDIR=$2; shift 2 ;;
	--source)    	PATH_TO_SOURCE="$2";  shift 2 ;;
      --wget-flags)        WGETFLAGS=$2;   shift 2 ;;
      --help | -h)
         show_help
         exit 0
         ;;
      --locale)
         IE6_LOCALE=$(echo "$2" | tr a-z A-Z)
	 USING_GUESSED_IE6_LOCALE=false
         shift 2
         ;;
      --list-locales)
	 echo $IE6_LOCALES | fmt -w 40
         exit 0
         ;;
      *)
         echo "Error: unknown option \"$1\""
         if echo "$1" | grep '=' >/dev/null; then
            echo "Options are not GNU-style"
            echo "  i.e. don't use: --option=value"
            echo "     use instead: --option value"
         fi
         echo "run \"./ies4linux --help\" for more info"
         exit 1
         ;;
    esac
done

# Check for dependencies
#wine --version &> /dev/null || error $MSG_ERROR_INSTALL_WINE
CABEXTRACT="/usr/bin/cabextract"
[ ! -f "$BASEDIR/.etersoft-release" ] && {
    $CABEXTRACT --version >/dev/null 2>/dev/null || error $MSG_ERROR_INSTALL_CABEXTRACT
    $CABEXTRACT --version | grep "1." >/dev/null 2>/dev/null || error $MSG_ERROR_UPDATE_CABEXTRACT
}
#wget --version >/dev/null 2>/dev/null || error $MSG_ERROR_INSTALL_WGET
#unzip -version >/dev/null 2>/dev/null || error $MSG_ERROR_INSTALL_UNZIP
#wine --version 2>&1  | grep "0.9." &> /dev/null || warning $MSG_WARNING_OLDWINE
#wine --version 2>&1  | grep "WINE@Etersoft 1.0" &> /dev/null || error $MSG_ERROR_INSTALL_WINE
[ "$(whoami)" = "root" ] && warning $MSG_WARNING_ROOT

# Asks what user wants to install
initAndConfigure

# Prepare folders
#mkdir -p "$BINDIR"
##mkdir -p "$BASEDIR/tmp/"
#mkdir -p "$BASEDIR/$DRIVEC/$WINDOWS/temp/"
##rm -rf "$BASEDIR"/tmp/*
#rm -rf "$BASEDIR/$DRIVEC/$WINDOWS"/temp/*
if [ ! -e "$DOWNLOADDIR" ]; then mkdir -p "$DOWNLOADDIR" ; fi
# NEW
mkdir -p "$BINDIR/ie6"

# Copy svg icon
cp "$IES4LINUX/lib/ies4linux.svg" "$BINDIR/ie6/"

# Load modules
. "$IES4LINUX/lib/download.sh"
. "$IES4LINUX/lib/install.sh"

# Last messages
section $MSG_INSTALLATIONS_FINISHED
echo

run_ies
echo

ask_for_translation
