#!/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:
EXPORTLINUX="/a/www/wowpreklad/Modules/Export/files/"
MPQARCHLINUX="${EXPORTLINUX}${MPQFILENAME}"
ICOLINUX="${EXPORTLINUX}WoW.ico"
LUAFILENAME="WowLua.exe"
VERSION=$( php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php version=$1 2> /dev/null)
FONTLINUX="${EXPORTLINUX}Fonts"
LUALINUX="${EXPORTLINUX}${VERSION}/${LUAFILENAME}"
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 "==================================="
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 $LUALINUX WowLua.exe
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 "Dbc soubory:"
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php dbc=$EXPORTID
echo "Addon:"
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php addon=$EXPORTID

echo "==================================="
echo "Generation Readme file:"
echo "==================================="
php /a/www/wowpreklad/Modules/Export/cmdmpqexport.php ExportId=$EXPORTID > CzechWoW.htm 2> /dev/null
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

for f in lua/Global*.lua      
do
                                                
  wine cmd /C "$MPQAPP ${MPQFILENAME} Interface\\FrameXML\\$(basename ${f}) ${f}";
done
for f in lua/Glue*.lua
do
  wine cmd /C "$MPQAPP ${MPQFILENAME} Interface\\GlueXML\\$(basename ${f}) ${f}";
done
echo "==================================="
echo "Creating Instalation file:"
echo "==================================="

makensis Instalace_CzechWoW.nsi

echo "HOTOVO!" 
 
