#!/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 "/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
INSTALLFLASH_DEFAULT=0
CREATE_ICON_DEFAULT=1
BASEDIR_DEFAULT="$WINEPREFIX"
BASEDIR="$WINEPREFIX"
#BINDIR_DEFAULT="$HOME/bin"
BINDIR_DEFAULT="$BASEDIR/dosdevices/c:/users/Public/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-flash)    INSTALLFLASH=1;  shift ;;
      --no-install-flash) INSTALLFLASH=0;  shift ;;
      --install-icon)     CREATE_ICON=1;   shift ;;
      --no-install-icon)  CREATE_ICON=0;   shift ;;
      --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
[ "$(whoami)" = "root" ] && warning $MSG_WARNING_ROOT

# Asks what user wants to install
initAndConfigure

# Prepare folders
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
