# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../iexplorer/iexplorer.conf
# Copyright (C) 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

autoextract=0
makeopt=
makeinstopt=

datadir=$datadir/$pkg
tmpdir=$builddir/tmp

 WINEPREFIX=$datadir/ie$ver
SYSTEMDRIVE=$datadir/ie$ver/drive_c
  SYSTEMDIR=$datadir/ie$ver/drive_c/windows/system

export WINEPREFIX SYSTEMDRIVE SYSTEMDIR

PROGRAMFILES="Program Files"	# detected after wineprefixcreate anyway


hook_add premake 5 'do_basic_installation'
do_basic_installation() {
	echo "Creating Wine Prefix"
	mkdir -p "$WINEPREFIX"
	xvfb-run -- strace -vvfF -o $WINEPREFIX/winreprefixcreate.strace wineprefixcreate

	touch $WINEPREFIX/.timestamp-base
	sleep 2

	PROGRAMFILES=$( ls -1 $SYSTEMDRIVE/ | grep -v windows | head -n 1 )

	# Install riched.dll
	echo "Installing RICHED20"
	cabextract -d "$tmpdir/" -F "ver1200.exe" `match_source_file -p 249973USA8`
	cabextract -d "$tmpdir/" -F "riched20.120" "$tmpdir/ver1200.exe"
	mv -v "$tmpdir/riched20.120" "$SYSTEMDIR/riched20.dll"

	# Install DCOM98
	echo "Installing DCOM98"
	cabextract -d "$SYSTEMDIR" `match_source_file -p DCOM98`
	mv -v "$SYSTEMDIR/dcom98.inf" "$SYSTEMDIR/../inf/"

	# Install mfc40
	echo "Installing ActiveX MFC40"
	cabextract -d "$tmpdir/" `match_source_file -p mfc40`
	cabextract -d "$tmpdir/" -F "mfc40.dll" "mfc40.exe"
	mv -v $tmpdir/mfc40.dll $SYSTEMDIR/

	# last things
	echo "And the icon"
	cp -v "$confdir/ie_wine.svg" "$docdir/ie_wine.svg"

	rm -rvf "$tmpdir/*"
}

hook_add postmake 5 'install_ie6'
install_ie6() {
	echo "Extracting downloaded exe file"
	cd $tmpdir
	wine `match_source_file -p ie60`
	cd IE*

	mkdir -vp "$SYSTEMDRIVE/$PROGRAMFILES/Internet Explorer"

	echo "Extracting CAB files"
	cabextract IE_S*CAB
	cabextract -L -d $SYSTEMDIR/ IE_1.CAB IEDOM.CAB
	mv -v $SYSTEMDIR/{sch128c,schannel}.dll
	mv -v $SYSTEMDIR/iexplore.exe "$SYSTEMDRIVE/$PROGRAMFILES/Internet Explorer/IEXPLORE.EXE"

	cabextract -L -d $SYSTEMDIR SCR56EN.CAB
	cabextract -L -F "pngfilt.dll" ADVAUTH.CAB
	cabextract -L -F "msvcrt.dll" SETUPW95.CAB
	mv -v pngfilt.dll msvcrt.dll $SYSTEMDIR

	echo "Installing TTF Fonts"
	cabextract -L -F "*TTF" FONT*CAB
	mv -v *ttf $SYSTEMDIR/../fonts/

	echo "Configuring ie6"
	gunzip -c $confdir/winereg.system.reg.gz > $WINEPREFIX/system.reg
	gunzip -c $confdir/winereg.user.reg > $WINEPREFIX/user.reg

	cat <<-EOT > $bindir/ie6
	#!/bin/bash
	cd \$HOME
	export WINEPREFIX="$WINEPREFIX"
	exec wine "$SYSTEMDRIVE/$PROGRAMFILES/Internet Explorer/IEXPLORE.EXE" \$@
	EOT
	chmod +x $bindir/ie6

	cat <<-EOT > $docdir/ie6.desktop
	Exec "$bindir/ie6"
	Icon "$docdir/ie_wine.svg"
	Name "Internet Explorer $ver"
	GenericName "Microsoft Windows Aplication"
	Comment "Microsoft Internet Explorer on Linux"
	EOT

        rm -rvf "$tmpdir/*"
}

hook_add postmake 7 'install_flash'
install_flash() {
	local OCXFILE=Flash8a.ocx

	echo "Preparing Flash Player 8"

	# Copy Flash files
	cabextract -d "$tmpdir/" `match_source_file -p swflash`

	# Create add.reg
	sed -n -e 's/^\[/;\[/g;/^;\[SW.AddReg\]/,/^;\[/p;' $tmpdir/swflash.inf > $tmpdir/add.reg
	sed -i '/^;/ d;/^\s/ d' add.reg

	strings=` sed -n -e 's/\s//g;s/^\[/;\[/g;/^;\[strings\]/,/^;\[/p;/^;/d;/^\s/d' swflash.inf `
	for string in $strings; do
        	key=` echo $string | awk -F '=' '/^[^;]/ {print $1}' `
		value=` echo $string | awk -F '"' '/^[^;]/ {print $2}' | sed -e 's/\//BARRA/g '`

	        sed -i -e "s/%${key}%/${value}/g;s/BARRA/\//g" add.reg
	done

	echo "Installing Flash Player 8"

        mkdir -p "$SYSTEMDIR/Macromed/Flash/"
        cp -v "$tmpdir/GetFlash.exe" "$tmpdir/$OCXFILE" "$SYSTEMDIR/Macromed/Flash/"

        wine regedit "$tmpdir/add.reg"
        wine regsvr32 'C:\Windows\System\Macromed\Flash\'$OCXFILE
}