#!/bin/bash

EXPORTID=$1

#wine like patch:
MPQAPP="Z:\\a\\www\\wowpreklad\\Modules\\Export\\mpq.exe"
TMP="Z:\\a\\www\\wowpreklad\\tmp\\Export\\${EXPORTID}\\"
MPQFILENAME="patch-5.MPQ"
MPQARCH="${TMP}${MPQFILENAME}"
#linux patch:
VERSION=$( php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php version=$1 2> /dev/null)
EXPORTLINUX="/a/www/wowpreklad/Modules/Export/files/"
MPQARCHLINUX="${EXPORTLINUX}${VERSION}/${MPQFILENAME}"
ICOLINUX="${EXPORTLINUX}WoW.ico"
EXEFILENAME="WowCz.exe"
FONTLINUX="${EXPORTLINUX}Fonts"
EXELINUX="${EXPORTLINUX}${VERSION}/*.exe"
ADDONLINUX="${EXPORTLINUX}${VERSION}/CzWoW"
NSILINUX="${EXPORTLINUX}${VERSION}/Instalace_CzechWoW.nsi"

TMPDIR="/a/www/wowpreklad/tmp/"
WORKDIR="${TMPDIR}Export/${EXPORTID}"
           
echo "==================================="
echo "Workdir: $WORKDIR"
echo "==================================="
mkdir $WORKDIR -p
cd $WORKDIR
echo "10" > progress

echo "==================================="
echo "Delete old/Copy files to tmp:"
echo "==================================="
mkdir CzWoW -p
mkdir dbc -p
mkdir lua -p
mkdir Fonts -p

rm dbc/* 
rm lua/* 
rm CzWoW/* 

cp $MPQARCHLINUX $MPQFILENAME 
cp $NSILINUX Instalace_CzechWoW.nsi
cp $ICOLINUX WoW.ico
cp $EXELINUX $EXEFILENAME
cp ${ADDONLINUX}/* CzWoW/ -R
cp ${FONTLINUX}/* Fonts/ -R


#cp ${TMPDIR}Export/${EXPORTID}/lua/* lua/ -R 
#cp ${TMPDIR}Export/${EXPORTID}/dbc/* dbc/ -R 
#cp ${TMPDIR}Export/${EXPORTID}/CzWoW/* CzWoW/ -R 
echo "hotovo"

echo "==================================="
echo "Export files from database:"
echo "==================================="

echo "Lua scripty:"
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php lua=$EXPORTID
echo "20" > progress

echo "Dbc soubory:"
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php dbc=$EXPORTID
echo "60" > progress
echo "Addon:"
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php addon=$EXPORTID
echo "66" > progress

echo "==================================="
echo "Generation Readme file:"
echo "==================================="
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php ExportId=$EXPORTID > CzechWoW.htm 2> /dev/null
echo "70" > progress
echo "Hotovo"

echo "==================================="
echo "Archive files to $MPQFILENAME :" 
echo "==================================="
for f in dbc/*.dbc
do
  wine cmd /C "$MPQAPP ${MPQFILENAME} DBFilesClient\\$(basename ${f}) ${f}";
done
echo "76" > progress
for f in lua/Global*.lua      
do
                                                
  wine cmd /C "$MPQAPP ${MPQFILENAME} Interface\\FrameXML\\$(basename ${f}) ${f}";
done
echo "80" > progress
for f in lua/Glue*.lua
do
  wine cmd /C "$MPQAPP ${MPQFILENAME} Interface\\GlueXML\\$(basename ${f}) ${f}";
done
echo "90" > progress
echo "==================================="
echo "Creating Instalation file:"
echo "==================================="

makensis Instalace_CzechWoW.nsi
echo "100" > progress
echo "HOTOVO!" 
 
