#!/bin/sh

# Fix 1C Education Server 3.0 files

PATCHFILE=$(winepath "C:\\windows\downloads\fix_1c_edu.patch")
TARGET_1SDIR=$(winepath "C:\\Program Files\\1C Education\\1CE3")

test -n "$TARGET_1SDIR" || exit 1
cd "$TARGET_1SDIR" || exit 1

mkdir -p ./Etersoft
cd ./Etersoft

cat <<EOF > start_1c_server.sh
#!/bin/sh

wine "C:\Program Files\1C Education\1CE3\common\jre\bin\java.exe" -jar -Duser.dir="C:\Program Files\1C Education\1CE3\common\tomcat" "C:\Program Files\1C Education\1CE3\common\tomcat\bin\bootstrap.jar" start
EOF



cat <<EOF > start_firebird.sh
#!/bin/sh

wine "C:\Program Files\Common Files\1C Education Shared\fb\bin\ibserver.exe" -a -p 65535
EOF

cd -

function do_patch(){

cd ./common/tomcat/webapps/1CEduWeb
patch -p0 < $PATCHFILE
cd -

}


function change_to_firefox(){

#TARGETDIR=../common/tomcat/webapps/1CEduWeb//config-client/1c/js

cd ./common/tomcat/webapps/1CEduWeb//config-client/1c/js

#    (      )

    echo Create original files...
    for TOFILE in *.js; do
		echo $TOFILE.orig
		if  [ -f "./$TOFILE.orig" ];  then
			echo  already  
			continue
		fi
		
		echo moving....
		mv  $TOFILE $TOFILE.orig || fatal "Can't copy "
    done

#         (    )
	for FILE in *.js.orig ; do

		TOFILE=`basename $FILE .orig`
		if  [ -f ./$TOFILE ]; then
			echo $TOFILE  already exist  
			continue
		fi

		echo $TOFILE

		cat $FILE 		| sed -r  's/\.all\(/.getElementById(/g'  \
					| sed -r 's/\.all \(/.getElementById(/g' \
					| sed -r 's/\.all\.([a-zA-Z0-9_]+|)\./.getElementById("\1")./g' \
					| sed -r 's/\.all\.([a-zA-Z0-9_]+)/.getElementById("\1")/g'\
					| sed -r 's/\.all\.([a-zA-Z0-9_]+)/.getElementById("\1")/g'\
					| sed -r 's/\.all[ ]*\[([a-zA-Z0-9_]+)\]/.getElementById(\1)/g' \
					| sed  's/document.getElementById("questionForm")/document.all.questionForm/g' \
					| sed  's/getElementById("item")/all.item/g' \
					| sed  's/getElementById("dummy")/all("dummy")/g' \
					 > ./$TOFILE
					 
					 
	done

	cd -

} # end change_to_firefox()

do_patch

change_to_firefox





