@echo off
:: Tested on XP Home SP3
:: Also tested previously (3.03) on WinME ... and Vista (3.04)  ...
:: ... with "HKLM\System\CurrentControlSet\Control\Wow\DpmiLimit" = 128 MB
::
:: Tuesday, October 19, 2010  7:10pm
::
:: rugxulo at gmail dot com
:: http://sites.google.com/site/rugxulo
::
:: Ad Majorem Gloriam Dei, Christus Rex
::
:: -----------------------------------------------------------
:: *** Requires *** :
::
:: 1).  /BETA/djdev204.zip  (2.03p2's DJTAR won't work on .tar.bz2
::                          else you need TAR112AB.ZIP + BZIP106B.ZIP
::                          as well as some version of DJDEV*.ZIP
::                          but I've only tested 2.04 and 2.03p2)
:: 2).  gcc444b.zip         (G++ always needs GCC and UCL=C, UPX=C++)
:: 3).  gpp444b.zip         (previously tried 3.4.4, 4.2.3, 4.3.2, 4.4.2)
:: 4).  bnu219b.zip         (AS, LD, COLLECT2, AR)
:: 5).  /BETA/mak381b.zip   (earlier versions supposedly WON'T work)
:: 6).  fil41b.zip          (for RM and TOUCH)
:: 7).  bsh205bbr3.zip      (yes, it needs SH, haven't tried 2.04)
:: 8).  sed421b.zip         (even SED is needed)
:: 9).  zlib125b.zip        (somehow needed)
::
:: ...  and of course ...
::
:: 10). ucl-1.03.tar.gz   lzma443.tar.bz2   upx-3.07-src.tar.bz2
::
:: They say LZMA 4.49 *may* work but wasn't tested nearly as much.
::
:: http://www.delorie.com/djgpp/
:: http://djgpp.cybermirror.org/
::
:: http://upx.sf.net
:: http://www.oberhumer.com/opensource/ucl/
:: http://www.sourceforge.net/projects/sevenzip
:: -----------------------------------------------------------

if "%UCL%"=="" set UCL=103
if "%UCLDOT%"=="" set UCLDOT=1.03
if "%LZMA%"=="" set LZMA=443
if "%LZMADOT%"=="" set LZMADOT=4.43
if "%UPXVER1%"=="" set UPXVER1=3.07
if "%UPXVER2%"=="" set UPXVER2=307
if "%MTUNE%"=="" set MTUNE=i686
for %%a in (3 4 5 6) do if "%MTUNE%"=="%%a86" set MTUNE=i%%a86

if "%PREFIX%"=="" set PREFIX=jkw\rugxulo\tmp\moo\booger
if not exist \%PREFIX%\nul set PREFIX=.

:: Basically, this .BAT should be in and run from within "..\%PREFIX%",
:: so put the .tbz files in %PREFIX%\UPX first.

if not exist \%PREFIX%\upx\nul echo \%PREFIX%\UPX dir not found!
if not exist \%PREFIX%\upx\nul echo Set %%PREFIX%% and try again.
if not exist \%PREFIX%\upx\nul goto end

if not "%PREFIX%"=="" cd \%PREFIX%\upx

if exist ucl-%UCLDOT%.tar.gz ren ucl-%UCLDOT%.tar.gz ucl%UCL%.tgz
if exist lzma%LZMA%.tar.bz2 ren lzma%LZMA%.tar.bz2 lzma%LZMA%.tbz
if exist upx-%UPXVER1%-src.tar.bz2 ren upx-%UPXVER1%-src.tar.bz2 upx%UPXVER2%s.tbz

for %%a in (lzma%LZMA% ucl%UCL%) do if not exist %%a.t?z echo %%a not found!
if not exist upx%UPXVER2%s.t?z echo %%a not found!
for %%a in (lzma%LZMA% ucl%UCL% upx%UPXVER2%s) do if not exist %%a.t?z goto end

if exist lzma-%LZMADOT%\nul goto build

md lzma-%LZMADOT%
cd lzma-%LZMADOT%
djtar -x ..\lzma%LZMA%.t?z
cd ..
djtar -x ucl%UCL%.t?z
djtar -x upx%UPXVER2%s.t?z

if exist ucl-%UCLDOT%\readme goto fixup

set P1=bzip2
:untar
tar --use-compress-program=%P1% -xf ucl%UCL%.t?z
tar --use-compress-program=%P1% -xf upx%UPXVER2%s.t?z
cd lzma-%LZMADOT%
tar --use-compress-program=%P1% -xf ..\lzma%LZMA%.t?z
cd ..
if exist ucl-%UCLDOT%\readme goto fixup
if "%P1%"=="gzip" goto end
set P1=gzip
goto untar

:fixup
:: seems we need to fix a small 2.03p2 error in PEFILE.CPP
echo #if DJGPP_MINOR != 4 >> upx-%UPXVER1%-src\src\snprintf.h
echo #define snprintf upx_snprintf >> upx-%UPXVER1%-src\src\snprintf.h
echo #endif >> upx-%UPXVER1%-src\src\snprintf.h

:: WinME doesn't like /Q in DEL or multiple params or 2>&1
set KLEEN=ucl-%UCLDOT%\b\clean.bat
echo for %%%%a in (def err exp map) do if exist *.%%%%a del *.%%%%a >%KLEEN%
echo for %%%%a in (o obj res tds) do if exist *.%%%%a del *.%%%%a >>%KLEEN%
echo for %%%%a in (libucl.a ucl.a) do if exist %%%%a del %%%%a >>%KLEEN%
echo for %%%%a in (ucl.dll ucl.lib) do if exist %%%%a del %%%%a >>%KLEEN%
echo if exist ucl_*.lib del ucl_*.lib >>%KLEEN%
echo for %%%%a in (a.exe a.out) do if exist %%%%a del %%%%a >>%KLEEN%
echo if exist simple.exe del simple.exe >>%KLEEN%
echo if exist uclpack.exe del uclpack.exe >>%KLEEN%
set KLEEN=

:: ... not really needed but just for license clarity ...
if "%OS%"=="Windows_NT" if exist ucl-%UCLDOT%\nul del /s ucl-%UCLDOT%\*.o*
if "%OS%"=="Windows_NT" goto build

if exist ucl-%UCLDOT%\nul dir /s/a/b ucl-%UCLDOT%\*.o* | sed "s/^/del /w blah.bat"
if exist blah.bat call blah.bat
if exist blah.bat del blah.bat

:::checkit
::cls
::echo Are there any *.O* files listed below??
::dir /s/a/b ucl-%UCLDOT%\*.o*
::pause

:build
cls
cd ucl-%UCLDOT%

if not "%DJGPP%"=="" goto gobuild
echo DJGPP not setup!
cd ..
goto end

:gobuild
echo echo on >dj.bat
echo call b\dos32\dj2.bat >>dj.bat
echo echo off >>dj.bat
sed "/set CF=/s/$/ -mtune=%MTUNE%/" b\dos32\dj2.bat > dj2.new
if exist dj2.new copy /b dj2.new b\dos32\dj2.bat
if exist dj.bat call dj.bat
if exist dj.bat del dj.bat

cd..
cd upx-%UPXVER1%-src\src

set CXX=gpp
set CXXFLAGS=-O2 -fomit-frame-pointer -mtune=%MTUNE% %1 %2 %3
set UPX_UCLDIR=/%PREFIX%/upx/ucl-%UCLDOT%
set UPX_LZMADIR=/%PREFIX%/upx/lzma-%LZMADOT%

make

if not exist upx.exe goto bye
cls
if exist upx.exe ren upx.exe upx-ucl.exe

set UFLAGS=--best --lzma
for %%m in (386 486) do if "%MTUNE%"=="%%m" set UFLAGS=--best
if exist upx-ucl.exe upx-ucl %UFLAGS% upx-ucl.exe
set UFLAGS=

if exist upx-ucl.exe move upx-ucl.exe ..\..
cd ..\..
dir upx-ucl.exe

:end
for %%a in (P1 PREFIX CXX UPX_UCLDIR UPX_LZMADIR UPXVER1) do set %%a=
for %%a in (UPXVER2 LZMADOT LZMA UCL UCLDOT MTUNE) do set %%a=

:bye
