#!/usr/bin/env bash
#prepare ground

    #get us here
#    if test $(echo $0 | grep "Resources" &>/dev/null); then
        IES4OSX_DIRNAME="$(dirname "$0")"
#    else
#        IES4OSX_DIRNAME="$PWD"
#    fi
#    echo $IES4OSX_DIRNAME
    cd "$IES4OSX_DIRNAME"

    export IES4OSX=1

    #is wine in $PATH?
    wine --version &> /dev/null || {
        #is wine in /Applications/Darwine?
        if test $(find -f /Applications/Darwine/Wine.bundle/Contents/bin/wine); then
            export PATH=$PATH:/Applications/Darwine/Wine.bundle/Contents/bin
        else
            #is wine in ~/Applications/Darwine?
            if test $(find -f ~/Applications/Darwine/Wine.bundle/Contents/bin/wine); then
                export PATH=$PATH:~/Applications/Darwine/Wine.bundle/Contents/bin
            else
                #is wine in locate db?
                if test $(locate wine | grep bin/wineserver); then
                    export PATH=$PATH:"$(dirname $(locate wine | grep bin/wineserver))"
                fi
            fi
        fi
    }
    wine --version &> /dev/null || {
        echo "Darwine not found. Terminating."
        exit 1;
    }


    #is cabextract in $PATH? Else use our own
    cabextract --version &> /dev/null || export PATH=$PATH:"$IES4OSX_DIRNAME/cabextract"

    #prepare X11
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$IES4OSX_DIRNAME/ies4osx.app/Contents/Resources/libfreetype/"
    export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:"$IES4OSX_DIRNAME/ies4osx.app/Contents/Resources/libfreetype/"

    if test $(echo $OSTYPE | grep darwin8); then
        # set Display properties and start X11 for Tiger
        export DISPLAY=:0.0
        /usr/bin/open-x11 wine
    else
        # set Display properties for Leopard
        export DISPLAY="$(find -f /tmp/launch-* -name :0)"
    fi

    # we want a .app package
    CREATE_APP=1

    #remove old ies4linux files
    #    rm -rf "/tmp/ies4linux"

    #prepare arguments
#    shift
#    cd ies4linux
#     ./ies4linux --no-gui --no-desktop-icon --no-menu-icon $*





### functions.sh
# DEBUG MODULE ################################################################

# Print a message if debug is on
# $* Message to be printed
function debug { 
	[ "$DEBUG" = "true"  ] && echo "DEBUG: $*"
}

# Pipe to read messages and print them if debug is on
function debugPipe {
	while read line; do
		debug $line
	done
}

function clean_tmp {
    rm -rf "$BASEDIR"/tmp/*
}

# Download file
# $1 URL
# $2 Destination
function download {
    echo -n "   downloading $(basename $1)..."
    if [ ! -f $2 ]; then
        curl -s -o $2 $1 && echo " OK"
    else
        echo " alredy downloaded"
    fi
}

# Extract a cab file in quiet mode
# (also lower case every file)
function extractCABs {
    cabextract -qL $* &> /dev/null
}

# create a .app bundle
# $1 ie7
# $2 7.0
function create_app {
    kill_wineserver
    rm -rf ~/Desktop/Internet\ Explorer\ $2.app >/dev/null
    cp -R "$IES4LINUX/ies4osx.app/" ~/Desktop/Internet\ Explorer\ $2.app/
    cp -PR $BASEDIR/$1/ ~/Desktop/Internet\ Explorer\ $2.app/Contents/Resources/$1/
    sed 's/WHICHINTERNETEXPLORER/Internet Explorer '$2'/' "$IES4LINUX/ies4osx.app/Contents/Info.plist" > ~/Desktop/Internet\ Explorer\ $2.app/Contents/Info.plist
    sed 's/WHICHINTERNETEXPLORER/'$1'/' "$IES4LINUX/ies4osx.app/Contents/Resources/start.sh" > ~/Desktop/Internet\ Explorer\ $2.app/Contents/Resources/start.sh
    [ "$INSTALLFONTANTIALIASING" = "0" ] && {
        sed 's/export LD_LIBRARY_PATH/#export LD_LIBRARY_PATH/' ~/Desktop/Internet\ Explorer\ $2.app/Contents/Resources/start.sh > ~/Desktop/Internet\ Explorer\ $2.app/Contents/Resources/start.sh2
        mv -f ~/Desktop/Internet\ Explorer\ $2.app/Contents/Resources/start.sh2 ~/Desktop/Internet\ Explorer\ $2.app/Contents/Resources/start.sh
    }
}

#shutdown wineserver
function kill_wineserver {
    wineserver -k || {
        killall wine &> /dev/null
        killall wineserver &> /dev/null
    }
}



# WINE MODULE #################################################################
# Call wineprefixcreate
function create_wine_prefix {
    if which wineprefixcreate &> /dev/null; then
        ( wineprefixcreate -w 2>&1 ) | debugPipe
    else
        echo "Could not create wineprefix"
    fi
}

# Register a dll
# $1 dll to be registered
function register_dll {
   debug Registering DLL: $1
   (WINEDLLOVERRIDES="regsvr32.exe=b" wine regsvr32 /i "$1" 2>&1) | debugPipe
}

# Add a registry file
# $1 reg file to be registered
function add_registry {
    debug Add $1 to registry
    (wine regedit "$1" 2>&1) | debugPipe
}

# Process an inf file
# $1 Inf file to process
function run_inf_file {
    debug Process INF $1
    ( wine rundll32 setupapi.dll,InstallHinfSection DefaultInstall 128 "$1" 2>&1) | debugPipe
}

# Generate reg and install it
# $1 ie version
function install_home_page {
    local temp="$BASEDIR/tmp/homepage.reg"

    get_start_page $1
    cat <<END > "$temp"
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]
"First Home Page"="${START_PAGE}"
"Start Page"="${START_PAGE}"

[HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main]
"Default_Page_URL"="${START_PAGE}"
"Default_Search_URL"="http://www.google.com"
"Search Page"="http://www.google.com"
"Start Page"="${START_PAGE}"
END
    add_registry "$temp"
    rm "$temp"
}

# $1 ie version
# $2 (optional) firstrun
function get_start_page {
    local url="http://www.tatanka.com.br/ies4linux/startpage?lang=$TRANSLATION_LOCALE&ieversion=$1"
    if [ "$2" = "firstrun" ]; then
        url="$url&firstrun=true"
    fi
    export START_PAGE="$url"
}





###ies4linux
# Discover ies4linux installation folder
IES4LINUX=`dirname "$0"`
cd "$IES4LINUX"
export IES4LINUX=`pwd`

# Instantiate variables
export INSTALLIE7=0
export INSTALLIE6=1
export INSTALLIE55=0
export INSTALLIE5=0
export INSTALLIE2=0
export INSTALLIE15=0
export INSTALLIE1=0
export INSTALLFLASH=1
export INSTALLROYALTHEME=0
#export BASEDIR="$HOME/.ies4linux"
export BASEDIR="/private/tmp/ies4linux"
export DOWNLOADDIR_SUFFIX="downloads"
export DOWNLOADDIR="$BASEDIR/$DOWNLOADDIR_SUFFIX"
export IE6_LOCALE="EN-US"
export INSTALLFONTANTIALIASING=0
export INSTALLCOREFONTS=0

# Get command-line options
while [ $# -gt 0 ]; do
    case "$1" in
    --install-ie6 | --install-flash)   shift;;
    --install-ie55)             export INSTALLIE55=1;   shift ;;
    --install-ie5)              export INSTALLIE5=1;    shift ;;
    --install-ie2)              export INSTALLIE2=1;    shift ;;
    --install-ie15)             export INSTALLIE15=1;    shift ;;
    --install-ie1)              export INSTALLIE1=1;    shift ;;
    --install-ie7)              export INSTALLIE7=1;    shift ;;
    --hack-ie7-proxy-settings)  export HACK_IE7_PROXY=1; shift ;;
    --no-ie6)                   export INSTALLIE6=0;   shift ;;
    --no-flash)                 export INSTALLFLASH=0;  shift ;;
    --locale)                   export IE6_LOCALE=$(echo "$2" | tr a-z A-Z); shift 2 ;;
    --install-royale-theme)     export INSTALLROYALTHEME=1;    shift ;;
    --font-antialiasing)        export INSTALLFONTANTIALIASING=1;    shift ;;
    --install-corefonts)        export INSTALLCOREFONTS=1;    shift ;;
    *) echo "Error: unknown option \"$1\""; exit 1 ;;
    esac
done





##### Install.sh
# See if user chose at least one IE
if [ "$((INSTALLIE6+INSTALLIE55+INSTALLIE5+INSTALLIE7+INSTALLIE1+INSTALLIE2+INSTALLIE15))" = "0" ]; then
    exit 0
fi



# downloading module #####################################################
echo "creating folders"
mkdir -p "$BASEDIR/tmp/" || echo "   could not create '$BASEDIR/tmp/'"
mkdir -p "$DOWNLOADDIR"  || echo "   could not create '$DOWNLOADDIR'"

# Download all files first
echo "downloading files"
    URL_IE6_CABS=http://download.microsoft.com/download/ie6sp1/finrel/6_sp1/W98NT42KMeXP
    IE6_CABS="ADVAUTH CRLUPD HHUPD IEDOM IE_EXTRA IE_S1 IE_S2 IE_S5 IE_S4 IE_S3 IE_S6 SCR56EN SETUPW95 FONTCORE FONTSUP VGX"

    download http://download.microsoft.com/download/d/1/3/d13cd456-f0cf-4fb2-a17f-20afc79f8a51/DCOM98.EXE "$DOWNLOADDIR/DCOM98.EXE"
    download http://activex.microsoft.com/controls/vc/mfc42.cab "$DOWNLOADDIR/mfc42.cab"
    download http://download.microsoft.com/download/win98SE/Update/5072/W98/EN-US/249973USA8.exe "$DOWNLOADDIR/249973USA8.exe"

mkdir -p "$DOWNLOADDIR/ie6/EN-US"
mkdir -p "$DOWNLOADDIR/ie6/$IE6_LOCALE"
for cab in $IE6_CABS; do
    # SCR56EN is always downloaded from EN-US
    if [ "$cab" = "SCR56EN" ] ; then
        download "$URL_IE6_CABS/EN-US/SCR56EN.CAB" "$DOWNLOADDIR/ie6/EN-US/SCR56EN.CAB"
    else
        download "$URL_IE6_CABS/$IE6_LOCALE/$cab.CAB" "$DOWNLOADDIR/ie6/$IE6_LOCALE/$cab.CAB"
    fi
done

    [ "$INSTALLIE1"  = "1" ] && download "http://www.mirrorservice.org/sites/browsers.evolt.org/browsers/ie/32bit/1.0/Msie10.exe" "$DOWNLOADDIR/Msie10.exe"
    [ "$INSTALLIE15" = "1" ] && download "http://www.mirrorservice.org/sites/browsers.evolt.org/browsers/ie/32bit/1.5/IE15I386.EXE" "$DOWNLOADDIR/IE15I386.EXE"
    [ "$INSTALLIE2"  = "1" ] && download "http://www.mirrorservice.org/sites/browsers.evolt.org/browsers/ie/32bit/2.0/msie20.exe" "$DOWNLOADDIR/msie20.exe"
    [ "$INSTALLIE3"  = "1" ] && download "http://www.mirrorservice.org/sites/browsers.evolt.org/browsers/ie/32bit/3.02/win95typical/msie302r.exe" "$DOWNLOADDIR/msie302r.exe"
    [ "$INSTALLIE55" = "1" ] && download "http://www.mirrorservice.org/sites/browsers.evolt.org/browsers/ie/32bit/standalone/ie55sp2_9x.zip" "$DOWNLOADDIR/ie55sp2_9x.zip"
    [ "$INSTALLIE5"  = "1" ] && download "http://www.mirrorservice.org/sites/browsers.evolt.org/browsers/ie/32bit/standalone/ie501sp2_9x.zip" "$DOWNLOADDIR/ie501sp2_9x.zip"
    [ "$INSTALLIE7"  = "1" ] && download "http://download.microsoft.com/download/3/8/8/38889DC1-848C-4BF2-8335-86C573AD86D9/IE7-WindowsXP-x86-enu.exe" "$DOWNLOADDIR/IE7-WindowsXP-x86-enu.exe"
    [ "$INSTALLFLASH" = "1" ] && download "http://download.macromedia.com/get/shockwave/cabs/flash/swflash.cab" "$DOWNLOADDIR/swflash.cab"

[ "$INSTALLCOREFONTS" = "1" ] && {
    COREFONTS="andale32.exe arial32.exe arialb32.exe comic32.exe courie32.exe georgi32.exe impact32.exe times32.exe trebuc32.exe verdan32.exe wd97vwr32.exe webdin32.exe"
    mkdir "$DOWNLOADDIR/corefonts" &> /dev/null
    for font in $COREFONTS; do
        download "http://internap.dl.sourceforge.net/sourceforge/corefonts/$font" "$DOWNLOADDIR/corefonts/$font"
    done
}

mkdir "$DOWNLOADDIR/themes" &> /dev/null
#download "http://www.winvistaside.de/download/download.php?file=4&go=1" "$DOWNLOADDIR/themes/Royale_theme.zip"
#download "http://download.softpedia.com/dl/caa7f39318c4193e0253bd9ffff3fd78/472378be/100013027/software/desktop/Royale_theme.zip" "$DOWNLOADDIR/themes/Royale_theme.zip"
#http://www.istartedsomething.com/uploads/royale_noir.zip
#download "http://www.pcwelt.de/cgi-bin/download/download.pl?fileid=1369" "$DOWNLOADDIR/themes/Royale_theme.zip"
#download "http://go.microsoft.com/fwlink/?LinkID=75078" "$DOWNLOADDIR/themes/ZuneDesktopTheme.msi"
    [ "INSTALLROYALTHEME" = "1" ] && download "ftp://ftp.crystalxp.net/crystalxp.net/12345/media-centre-edition-crystalxp.net-en-318.zip" "$DOWNLOADDIR/themes/Royale_theme.zip"



# IE6 Installation module #####################################################
echo "installing Internet Explorer 6"
	clean_tmp
    rm -rf "$BASEDIR/ie6"
    export WINEPREFIX="$BASEDIR/ie6/"
    mkdir -p "$BASEDIR/ie6/"

echo "IE 6.0: creating winprefix"
    create_wine_prefix

    # Discover Wine folders
    DRIVEC=drive_c
    WINDOWS=Windows
    SYSTEM=system
    SYSTEM32=System32
    FONTS=Fonts
    INF=Inf
    COMMAND=Command
    if [ -d "$BASEDIR/ie6/fake_windows" ]; then DRIVEC=fake_windows; fi
    if [ -d "$BASEDIR/ie6/$DRIVEC/windows" ]; then WINDOWS=windows; fi
    if [ -d "$BASEDIR/ie6/$DRIVEC/$WINDOWS/system32" ]; then SYSTEM32=system32; fi
    if [ -d "$BASEDIR/ie6/$DRIVEC/$WINDOWS/fonts" ]; then FONTS=fonts; fi
    if [ -d "$BASEDIR/ie6/$DRIVEC/$WINDOWS/inf" ]; then INF=inf;fi
    if [ -d "$BASEDIR/ie6/$DRIVEC/$WINDOWS/command" ]; then COMMAND=command;fi 
    export DRIVEC WINDOWS SYSTEM FONTS INF COMMAND

    # symlinking system to system32
    if [ -d "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM32" ]; then 
        rm -rf "$BASEDIR/ie6/$DRIVEC/$WINDOWS/"{S,s}ystem
        cd "$BASEDIR/ie6/$DRIVEC/$WINDOWS/"
        ln -s "$SYSTEM32" "system"
    fi

echo "IE 6.0: extracting CABs"
	clean_tmp
    cd "$BASEDIR/tmp"
    extractCABs "$DOWNLOADDIR/ie6/$IE6_LOCALE"/{ADVAUTH,CRLUPD,HHUPD,IEDOM,IE_EXTRA,IE_S*,SETUPW95,VGX}.CAB
    extractCABs "$DOWNLOADDIR/ie6/EN-US/SCR56EN.CAB"
    extractCABs ie_1.cab
    rm -f *cab regsvr32.exe setup*

echo "IE 6.0: installing IE6"
    mv cscript.exe "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$COMMAND/"
    mv wscript.exe "$BASEDIR/ie6/$DRIVEC/$WINDOWS/"
    mv sch128c.dll  "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/schannel.dll"
    mkdir -p "$BASEDIR/ie6/$DRIVEC/Program Files/Internet Explorer"
    mv iexplore.exe "$BASEDIR/ie6/$DRIVEC/Program Files/Internet Explorer/iexplore.exe"
    mkdir -p "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/sfp/ie/"
    mv vgx.cat "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/sfp/ie/"
    mv -f * "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/"

echo "IE 6.0: installing DCOM98"
    clean_tmp
    extractCABs -d "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/" "$DOWNLOADDIR/DCOM98.EXE"
    mv "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/rpcltscm.dll" "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/rpcltspx.dll"
    mv "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/dcom98.inf" "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$INF/"

echo "IE 6.0: installing Fonts"
    clean_tmp
    cd "$BASEDIR/tmp"
    extractCABs -F "*TTF" "$DOWNLOADDIR/ie6/$IE6_LOCALE/"/FONT*CAB
[ "$INSTALLCOREFONTS" = "1" ] && {
    extractCABs -F "*TTF" "$DOWNLOADDIR/corefonts/"*
    extractCABs -F "*Viewer1.cab" "$DOWNLOADDIR/corefonts/"*
    extractCABs -F "*TTF" "$BASEDIR/tmp/Viewer1.cab"
    chmod u+w "tahoma.ttf"
}
    mv *ttf "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$FONTS/"

echo "IE 6.0: installing ActiveX MFC42"
    clean_tmp
    extractCABs "$DOWNLOADDIR/mfc42.cab"
    extractCABs mfc42.exe
    mv *.inf "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$INF/"
    mv {olepro32,msvcrt,mfc42}.dll "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/"
    register_dll "C:\\Windows\\System\\olepro32.dll"
    register_dll "C:\\Windows\\System\\mfc42.dll"

echo "IE 6.0: installing RICHED20"
    clean_tmp
    extractCABs -F ver1200.exe "$DOWNLOADDIR/249973USA8.exe"
    extractCABs "$BASEDIR/tmp/ver1200.exe"
    mv riched20.120 "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/riched20.dll"
    mv riched32.dll usp10.dll "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$SYSTEM/"

echo "IE 6.0: installing registry keys"
    clean_tmp
    add_registry "$IES4LINUX"/winereg/ie6.reg
    install_home_page ie6

echo "IE 6.0: finalizing"
    wineboot
    chmod -R u+rwx "$BASEDIR/ie6"



# Flash Installation module ###################################################
[ "$INSTALLFLASH" = "1" ] && {
    echo "installing Flash Player 9"
        clean_tmp
        cd "$BASEDIR/tmp/"

    echo "Flash: extracting files"
        extractCABs "$DOWNLOADDIR/swflash.cab"
        FLASHOCX=$(echo $BASEDIR/tmp/*.ocx | sed -e "s/.*\///")

    echo "Flash: installing Flash to IE6"
        cp swflash.inf "$BASEDIR/ie6/$DRIVEC/$WINDOWS/$INF/"
        run_inf_file ./swflash.inf
        register_dll "C:\\Windows\\System\\Macromed\\Flash\\$FLASHOCX"

    echo "Flash: finalizing"
        wineboot
}



# Royal Theme module ###################################################
[ "$INSTALLROYALTHEME" = "1" ] && {
    echo "installing Royale Theme"
        clean_tmp
        cd "$BASEDIR/tmp/"

    echo "Royale Theme: extracting files"
        unzip "$DOWNLOADDIR/themes/Royale_theme.zip" -d "$BASEDIR/tmp/" &>/dev/null
        cabextract -q "Royale Theme for Win XP.exe" -d "$BASEDIR/ie6/$DRIVEC/windows"


    echo "Royale Theme: installing registry keys"
        clean_tmp
        add_registry "$IES4LINUX"/winereg/royal.reg

    echo "Royale Theme: finalizing"
        wineboot
}



    [ "$INSTALLIE6"   = "1" ] &&  create_app ie6 6.0



# IE1.0 Installation module ###################################################
[ "$INSTALLIE1"   = "1" ] &&  {
    echo "installing Internet Explorer 1.0"
        kill_wineserver
        rm -rf "$BASEDIR/ie1"
        mkdir -p "$BASEDIR/ie1/$DRIVEC/Program Files/Internet Explorer/History"
        mkdir -p "$BASEDIR/ie1/$DRIVEC/Program Files/Internet Explorer/dcache"

    echo "IE 1.0: creating wineprefix"
        export WINEPREFIX="$BASEDIR/ie1/"
        create_wine_prefix
        clean_tmp

    echo "IE 1.0: extracting CABs"
        cd "$BASEDIR/tmp"
        extractCABs "$DOWNLOADDIR"/Msie10.exe
        cabextract -q iexplore.cab -d "$BASEDIR/ie1/$DRIVEC/Program Files/Internet Explorer/" &>/dev/null
        
    echo "IE 1.0: installing registry keys"
        add_registry "$IES4LINUX"/winereg/.ie1.reg
        install_home_page ie1

    echo "IE 1.0: finalizing"
        chmod -R u+rwx "$BASEDIR/ie1"
        create_app ie1 1.0
}



# IE1.5 Installation module ###################################################
[ "$INSTALLIE15"   = "1" ] &&  {
    echo "installing Internet Explorer 1.5"
        kill_wineserver
        rm -rf "$BASEDIR/ie15"
        mkdir -p "$BASEDIR/ie15/$DRIVEC/Program Files/Internet Explorer/History"

    echo "IE 1.5: creating wineprefix"
        export WINEPREFIX="$BASEDIR/ie15/"
        create_wine_prefix
        clean_tmp

    echo "IE 1.5: extracting CABs"
        cd "$BASEDIR/tmp"
        cabextract -q "$DOWNLOADDIR"/IE15I386.EXE -d "$BASEDIR/ie15/$DRIVEC/Program Files/Internet Explorer/" &>/dev/null

    echo "IE 1.5: installing registry keys"
        add_registry "$IES4LINUX"/winereg/.ie1.reg
        install_home_page ie15

    echo "IE 1.5: finalizing"
        chmod -R u+rwx "$BASEDIR/ie15"
        create_app ie15 1.5
}



# IE2.0 Installation module ###################################################
[ "$INSTALLIE2"   = "1" ] &&  {
    echo "installing Internet Explorer 2.0"
        kill_wineserver
        rm -rf "$BASEDIR/ie2"
        mkdir -p "$BASEDIR/ie2/$DRIVEC/Program Files/Internet Explorer/History"

    echo "IE 2.0: creating wineprefix"
        export WINEPREFIX="$BASEDIR/ie2/"
        create_wine_prefix
        clean_tmp

    echo "IE 2.0: extracting CABs"
        cd "$BASEDIR/tmp"
        extractCABs "$DOWNLOADDIR"/msie20.exe
        cabextract -q iexplore.cab-d "$BASEDIR/ie2/$DRIVEC/Program Files/Internet Explorer/" &>/dev/null
        
    echo "IE 2.0: installing registry keys"
        add_registry "$IES4LINUX"/winereg/.ie1.reg
        install_home_page ie2
        
    echo "IE 2.0: finalizing"
        chmod -R u+rwx "$BASEDIR/ie2"
        create_app ie2 2.0
}



# IE5.0 Installation module ###################################################

[ "$INSTALLIE5"   = "1" ] &&  {
    echo "installing Internet Explorer 5.0"
        kill_wineserver
        export WINEPREFIX="$BASEDIR/ie5/"

    echo "IE 5.0: copying IE6"
        clean_tmp
        rm -rf "$BASEDIR/ie5"
        cp -PR "$BASEDIR"/ie6 "$BASEDIR"/ie5
        DIR="$BASEDIR/ie5/$DRIVEC/$WINDOWS/$SYSTEM"
        rm "$DIR"/{browseui,dispex,dxtmsft,dxtrans,inetcpl,inetcplc,jscript,mshtml,mshtmled,mshtmler,shdocvw,urlmon}.*
    
    echo "IE 5.0: extracting files"
        cd "$BASEDIR/tmp/"
        unzip -Lqq "$DOWNLOADDIR/ie501sp2_9x.zip"
        mv ie501sp2_9x/*{dll,tlb,cpl} "$BASEDIR/ie5/$DRIVEC/$WINDOWS/$SYSTEM/"
        mv ie501sp2_9x/iexplore.exe "$BASEDIR/ie5/$DRIVEC/Program Files/Internet Explorer/iexplore.exe"
    
    echo "IE 5.0: installing registry keys"
        add_registry "$IES4LINUX"/winereg/ie5.reg
        install_home_page ie5
    
    echo "IE 5.0: finalizing"
        chmod -R u+rwx "$BASEDIR/ie5"
        create_app ie5 5.0
}



# IE5.5 Installation module ###################################################

[ "$INSTALLIE55"   = "1" ] &&  {
    echo "installing Internet Explorer 5.5"
        kill_wineserver
        export WINEPREFIX="$BASEDIR/ie55/"

    echo "IE 5.5: copying IE6"
        clean_tmp
        rm -rf "$BASEDIR/ie55"
        cp -PR "$BASEDIR"/ie6 "$BASEDIR"/ie55
        DIR="$BASEDIR/ie55/$DRIVEC/$WINDOWS/$SYSTEM"
        rm "$DIR"/{browseui,dispex,dxtmsft,dxtrans,inetcpl,inetcplc,jscript,mshtml,mshtmled,mshtmler,shdocvw,urlmon}.*

    echo "IE 5.5: extracting files"
        cd "$BASEDIR/tmp/"
        unzip -Lqq "$DOWNLOADDIR"/ie55sp2_9x.zip
        mv ie55sp2_9x/*{dll,tlb,cpl} "$BASEDIR/ie55/$DRIVEC/$WINDOWS/$SYSTEM/"
        mv ie55sp2_9x/iexplore.exe "$BASEDIR/ie55/$DRIVEC/Program Files/Internet Explorer/iexplore.exe"

    echo "IE 5.5: installing registry keys"
        add_registry "$IES4LINUX"/winereg/ie55.reg
        install_home_page ie55

    echo "IE 5.5: finalizing"
        chmod -R u+rwx "$BASEDIR/ie55"
        create_app ie55 5.5
}



# IE7.0 Installation module ###################################################

# ATTENTION: IES4LINUX IE7 SUPPORT IS PRE-PRE-ALPHA!
# USE ONLY TO HELP ME TESTING THIS FEATURE
[ "$INSTALLIE7"   = "1" ] &&  {
    echo "installing Internet Explorer 7.0 (BETA)"
        kill_wineserver
        export WINEPREFIX="$BASEDIR/ie7/"

    echo "IE 7.0: copying IE6"
        clean_tmp
        rm -rf "$BASEDIR/ie7"
        cp -PR "$BASEDIR"/ie6 "$BASEDIR"/ie7

    echo "IE 7.0: extracting files"
        cd "$BASEDIR/tmp/"

#       this is to msvcrt
#       extractCABs "$DOWNLOADDIR"/Q305601_WxP_SP1_x86_ENU.exe
#       cp msvcrt.dll "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"
#       clean_tmp

        extractCABs "$DOWNLOADDIR"/IE7-WindowsXP-x86-enu.exe

        cp wininet.dll iertutil.dll shlwapi.dll urlmon.dll jscript.dll vbscript.dll advpack.dll "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"
        cp mshtml.dll mshtml.tlb mshtmled.dll mshtmler.dll inetcpl.cpl "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"
        cp ieapfltr.dll mstime.dll ieencode.dll "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"

        cp dxtmsft.dll dxtrans.dll pngfilt.dll  ieframe.dll "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"
        cp ieproxy.dll ieui.dll jsproxy.dll vgx.dll imgutil.dll "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"

        register_dll "C:\\Windows\\System\\mshtml.dll"
        register_dll "C:\\Windows\\System\\mshtmled.dll"
        register_dll "C:\\Windows\\System\\mshtmler.dll"
        register_dll "C:\\Windows\\System\\dxtmsft.dll"
        register_dll "C:\\Windows\\System\\dxtrans.dll"
        register_dll "C:\\Windows\\System\\pngfilt.dll"

        # we can't register these dlls
        #register_dll "C:\\Windows\\System\\msvcrt.dll"
        #register_dll "C:\\Windows\\System\\imgutil.dll"
        #register_dll "C:\\Windows\\System\\jscript.dll"
        #register_dll "C:\\Windows\\System\\vbscript.dll"

        # don't use ie7 exe
        #cp iexplore.exe "$BASEDIR/ie7/$DRIVEC/Program Files/Internet Explorer/iexplore.exe"

        cd update
        extractCABs idndl.exe
        cp normaliz.dll idndl.dll "$BASEDIR/ie7/$DRIVEC/$WINDOWS/$SYSTEM"

        register_dll "C:\\Windows\\System\\normaliz.dll"
        register_dll "C:\\Windows\\System\\idndl.dll"

    echo "IE 7.0: installing registry keys"
        add_registry "$IES4LINUX"/winereg/ie7.reg
        install_home_page ie7

    echo "IE 7.0: finalizing"
        wineboot
        chmod -R u+rwx "$BASEDIR/ie7"
        create_app ie7 "7.0"
}

# Remove tmp files
rm -rf "$BASEDIR/"/ie* &> /dev/null