:: This batch file update the xepersian and bidi packages in texlive 2017 onward
:: It assumes texlive rootfolder is d:\texlive\2017 and the local folder is
:: d:\texlive\texmf-local\tex. If not correct
::
:: Mohammad Rahmani
:: Chemical Engineering Department
:: Amirkabir University of Technology
:: m.rahmani@aut.ac.ir
:: rev 0.1, Aug 10, 2017
::
:: ---------------------------------------------------------------------
:: Change the root folder to meet your texlive installtion 
:: in your computer
   SET root=d:\texlive\texmf-local\tex
:: ---------------------------------------------------------------------



Title Update the xepersian and bidi packages in texlive distribution
Echo off
cls

:: Check the root folder
IF NOT EXIST %root% (
	ECHO . Your local folder for texlive
	ECHO .
	ECHO .    %root% 
	ECHO .
	ECHO . is not correct....
	ECHO . Open this batch file and correct the path and run it again
	Exit/b
) ELSE (
	ECHO The local folder %root% is available
	ECHO Job started ........
)


Echo update the xepersian and bidi packages in texlive distribution
cd /d "%~dp0"


:: Delete previous folders
IF EXIST "%~dp0xepersian" (
	del/s /f /q xepersian
)
IF EXIST "%~dp0bidi" (
	del/s /f /q bidi
)	


:: Unzip files	
for %%a in (*.zip) do (
	PowerShell Expand-Archive -Force -Path %%a '%~dp0'
)


:: Extract the package files from dts archive
ECHO Expand the .dtx files
cd %~dp0xepersian
latex xepersian.ins
cd ..
cd %~dp0bidi
latex bidi.ins
cd ..


:: Copy file to destination folder
ECHO Copy files to local folder in TeX Database Structure
SET xepersianroot=%root%\xelatex\bidi
SET bidiroot=%root%\xelatex\bidi

Echo Copy the xepersian and bidi files into the local TeX folder
IF NOT EXIST  %root% (
 Echo The root folder for texlive is not exist. Check the batch file
 Exit/b
 )
 
 IF EXIST %xepersianroot% (
	del /s/f/q %xepersianroot%\*.*
 ) ELSE (
	mkdir %root%\xelatex\xepersian
 )
 xcopy "%~dp0xepersian" %root%\xelatex\xepersian /f /s /y

 

 IF EXIST %bidiroot% (
	del /s/f/q %bidiroot%\*.*
 ) ELSE (
	mkdir %root%\xelatex\bidi
 )
 xcopy "%~dp0bidi" %root%\xelatex\bidi /f /s /y

 
 
 :: Update the TeX Database Structure
 ECHO Update the TDS
 texhash
 