#!/bin/sh

#
# Build release binaries for patcher9x
#
# Written by Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
#
# Builds all of Linux amd64, win32, win64 and DOS boot floppy
#

# Extract version from the first line of CHANGELOG
export VERSION=`make get-version`
echo "Building release $VERSION"

# Archive vanilla source
SRCDIR=`pwd`
make clean
tar zcf /tmp/patcher9x.tar.gz .

# Extract archive to build-djgpp and build DOS executable
rm -rf /tmp/build-djgpp && mkdir -p /tmp/build-djgpp && \
cd /tmp/build-djgpp && tar xf /tmp/patcher9x.tar.gz && \
make RELEASE=1 PROFILE=djgpp strip && \
cd $SRCDIR

# Extract archive to build-win32 and build Win32 executable
rm -rf /tmp/build-win32 && mkdir -p /tmp/build-win32 && \
cd /tmp/build-win32 && tar xf /tmp/patcher9x.tar.gz && \
make WIN32=1 PROFILE=nocrt GUEST_CC=i686-w64-mingw32-gcc GUEST_WINDRES=i686-w64-mingw32-windres RELEASE=1 strip && \
cd $SRCDIR

# Extract archive to build-win64 and build Win64 executable
rm -rf /tmp/build-win64 && mkdir -p /tmp/build-win64 && \
cd /tmp/build-win64 && tar xf /tmp/patcher9x.tar.gz && \
make WIN32=1 PROFILE=nocrt64 GUEST_CC=x86_64-w64-mingw32-gcc GUEST_WINDRES=x86_64-w64-mingw32-windres RELEASE=1 strip && \
cd $SRCDIR

# Extract archive to build-amd64 and build Linux executable
rm -rf /tmp/build-linux-amd64 && mkdir -p /tmp/build-linux-amd64 && \
cd /tmp/build-linux-amd64 && tar xf /tmp/patcher9x.tar.gz && \
make RELEASE=1 strip && \
cd $SRCDIR

# Build README.txt from patcher9x help output
make get-help > /tmp/README.txt && cd $SRCDIR

# Build win32 archive
mkdir -p /tmp/archive-win32 && \
cp /tmp/build-win32/patcher9x.exe /tmp/archive-win32 && \
cp CHANGELOG /tmp/archive-win32/CHANGELOG.txt && \
cp LICENSE /tmp/archive-win32/LICENCE.txt && \
make WIN32=1 get-help > /tmp/archive-win32/README.txt && \
unix2dos /tmp/archive-win32/CHANGELOG.txt && \
unix2dos /tmp/archive-win32/LICENCE.txt && \
unix2dos /tmp/archive-win32/README.txt && \
cd /tmp/archive-win32 && zip /tmp/patcher9x-$VERSION-win32.zip * && cd $SRCDIR

# Build win64 archive
mkdir -p /tmp/archive-win64 && \
cp /tmp/build-win64/patcher9x.exe /tmp/archive-win64 && \
cp CHANGELOG /tmp/archive-win64/CHANGELOG.txt && \
cp LICENSE /tmp/archive-win64/LICENCE.txt && \
make WIN32=1 get-help > /tmp/archive-win64/README.txt && \
unix2dos /tmp/archive-win64/CHANGELOG.txt && \
unix2dos /tmp/archive-win64/LICENCE.txt && \
unix2dos /tmp/archive-win64/README.txt && \
cd /tmp/archive-win64 && zip /tmp/patcher9x-$VERSION-win64.zip * && cd $SRCDIR

# Build Linux amd64 archive
mkdir -p /tmp/archive-linux-amd64 && \
cp /tmp/build-linux-amd64/patcher9x /tmp/archive-linux-amd64 && \
cp CHANGELOG /tmp/archive-linux-amd64/CHANGELOG && \
cp LICENSE /tmp/archive-linux-amd64/LICENCE && \
make get-help > /tmp/archive-linux-amd64/README && \
cd /tmp/archive-linux-amd64 && tar zcvf /tmp/patcher9x-$VERSION-linux-amd64.tar.gz * && cd $SRCDIR

# Mark's way to make boot floppy from official FreeDOS distrib.
## Build boot floppy: extract partition image from LiteUSB edition for extras
#unzip -d /tmp /opt/freedos/freedos-liteusb.zip FD13LITE.img && \
#SS=`file /tmp/FD13LITE.img | sed 's/.*startsector \([0-9]\{1,\}\).*/\1/g'` && \
#mkdir -p /var/lib/dosemu && dd if=/tmp/FD13LITE.img of=/var/lib/dosemu/fdimage skip=$SS
#
## Copy off boot disk files to /tmp/dosfiles
#mkdir -p /tmp/dosfiles && \
#mmount -V n: && \
#mcopy -pm n:/freedos/bin/XCOPY.EXE /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/COMMAND.COM /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/DELTREE.COM /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/FDISK.EXE /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/FDISK.INI /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/FORMAT.EXE /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/FDISKPT.INI /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/HIMEMX.EXE /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/JEMM386.EXE /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/SHSUCDX.COM /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/UDVD2.SYS /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/DEVLOAD.COM /tmp/dosfiles && \
#mcopy -pm n:/freedos/bin/EDIT.EXE /tmp/dosfiles
#
## Copy CWSDPMI files to /tmp/dosfiles
#mkdir -p /tmp/cwsdpmi && unzip -d /tmp/cwsdpmi /opt/cwsdpmi/cwsdpmi.zip bin/CWSDPMI.EXE bin/cwsdpmi.doc && \
#cp -p /tmp/cwsdpmi/bin/CWSDPMI.EXE /tmp/dosfiles && \
#cp -p /tmp/cwsdpmi/bin/cwsdpmi.doc /tmp/dosfiles/CWSDPMI.TXT
#
# Copy PATCH9X.EXE and repo boot files to /tmp/dosfiles
#cp -p /tmp/build-djgpp/patch9x.exe /tmp/dosfiles/PATCH9X.EXE && \
#cp -p boot/autoexec.bat /tmp/dosfiles/AUTOEXEC.BAT && \
#cp -p boot/cdrom.bat /tmp/dosfiles/CDROM.BAT && \
#cp -p boot/fdconfig.sys /tmp/dosfiles/FDCONFIG.SYS && \
#cp -p boot/info.bat /tmp/dosfiles/INFO.BAT && \
#cp -p boot/info.txt /tmp/dosfiles/INFO.TXT && \
#cat boot/readme.txt.template | sed "s/%VERSION%/$VERSION/g" > /tmp/dosfiles/README.TXT
#
## Finally take the boot floppy, delete the unused files and then copy over ours
#mkdir -p /tmp/floppy && unzip -d /tmp/floppy /opt/freedos/freedos-floppy.zip 144m/x86BOOT.img && \
#cp /tmp/floppy/144m/x86BOOT.img /var/lib/dosemu/fdimage
#
#mdel n:/FDAUTO.BAT && \
#mdel n:/FDCONFIG.SYS && \
#mdel n:/SETUP.BAT && \
#mdeltree n:/FREEDOS && \
#mcopy -pm /tmp/dosfiles/XCOPY.EXE n: && \
#mcopy -pm /tmp/dosfiles/COMMAND.COM n: && \
#mcopy -pm /tmp/dosfiles/DELTREE.COM n: && \
#mcopy -pm /tmp/dosfiles/FDISK.EXE n: && \
#mcopy -pm /tmp/dosfiles/FDISK.INI n: && \
#mcopy -pm /tmp/dosfiles/FORMAT.EXE n: && \
#mcopy -pm /tmp/dosfiles/FDISKPT.INI n: && \
#mcopy -pm /tmp/dosfiles/HIMEMX.EXE n: && \
#mcopy -pm /tmp/dosfiles/JEMM386.EXE n: && \
#mcopy -pm /tmp/dosfiles/SHSUCDX.COM n: && \
#mcopy -pm /tmp/dosfiles/UDVD2.SYS n: && \
#mcopy -pm /tmp/dosfiles/DEVLOAD.COM n: && \
#mcopy -pm /tmp/dosfiles/EDIT.EXE n: && \
#mcopy -pm /tmp/dosfiles/CWSDPMI.EXE n: && \
#mcopy -pm /tmp/dosfiles/CWSDPMI.TXT n: && \
#mcopy -pm /tmp/dosfiles/PATCH9X.EXE n: && \
#mcopy -pm /tmp/dosfiles/AUTOEXEC.BAT n: && \
#mcopy -pm /tmp/dosfiles/CDROM.BAT n: && \
#mcopy -pm /tmp/dosfiles/FDCONFIG.SYS n: && \
#mcopy -pm /tmp/dosfiles/INFO.BAT n: && \
#mcopy -pm /tmp/dosfiles/INFO.TXT n: && \
#mcopy -pm /tmp/dosfiles/README.TXT n:

# using floppy template + dosbox to generate bool floppy
rm -rf /tmp/dosbox && mkdir -p /tmp/dosbox/c
cp /opt/jhr/freedos-14.ima /tmp/dosbox/floppy.ima

cp -p /tmp/build-djgpp/patch9x.exe /tmp/dosbox/c/PATCH9X.EXE

# copy readme
make PROFILE=djgpp get-help > /tmp/dosbox/c/PATCH9X.TXT
unix2dos /tmp/dosbox/c/PATCH9X.TXT
cp LICENSE /tmp/dosbox/c/LICENSE.TXT
unix2dos /tmp/dosbox/c/LICENSE.TXT

# copy CWSDPMI files to /tmp/dosfiles
mkdir -p /tmp/cwsdpmi && unzip -o -d /tmp/cwsdpmi /opt/cwsdpmi/cwsdpmi.zip bin/CWSDPMI.EXE bin/cwsdpmi.doc && \
cp -p /tmp/cwsdpmi/bin/CWSDPMI.EXE /tmp/dosbox/c && \
cp -p /tmp/cwsdpmi/bin/cwsdpmi.doc /tmp/dosbox/c/CWSDPMI.TXT && cd $SRCDIR

# copy other files
cp -p boot/autoexec.bat /tmp/dosbox/c/AUTOEXEC.BAT && \
cp -p boot/cdrom.bat /tmp/dosbox/c/CDROM.BAT && \
cp -p boot/fdconfig.sys /tmp/dosbox/c/FDCONFIG.SYS && \
cp -p boot/info.bat /tmp/dosbox/c/INFO.BAT && \
cp -p boot/info.txt /tmp/dosbox/c/INFO.TXT && \
cp -p boot/extra.txt /tmp/dosbox/c/EXTRA.TXT

# readme afrer boot
sed "s/%VERSION%/$VERSION/" boot/readme.txt.template > /tmp/dosbox/c/README.TXT
dos2unix /tmp/dosbox/c/README.TXT

# copy drivers
mkdir -p /tmp/dosbox/c/EXTRA/INTELINF && unzip -o -d /tmp/dosbox/c/EXTRA/INTELINF /opt/drivers/intelinf.zip
mkdir -p /tmp/dosbox/c/EXTRA/SATA && unzip -o -d /tmp/dosbox/c/EXTRA/SATA /opt/drivers/sata.zip
mkdir -p /tmp/dosbox/c/EXTRA/AHCI && unzip -o -d /tmp/dosbox/c/EXTRA/AHCI /opt/drivers/ahci.zip

mkdir -p /tmp/cregfix && unzip -o -d /tmp/cregfix /opt/cregfix/cregfix.zip && \
cp -p /tmp/cregfix/cregfix-trunk/cregfix.com /tmp/dosbox/c/EXTRA/CREGFIX.COM && \
cat /tmp/cregfix/cregfix-trunk/README > /tmp/dosbox/c/EXTRA/CREGFIX.TXT && \
echo >> /tmp/dosbox/c/EXTRA/CREGFIX.TXT && \
cat /tmp/cregfix/cregfix-trunk/LICENSE >> /tmp/dosbox/c/EXTRA/CREGFIX.TXT

mkdir -p /tmp/simd95 && unzip -o -d /tmp/simd95 /opt/jhr/simd95.zip && \
cp -p /tmp/simd95/simd95.com /tmp/dosbox/c/EXTRA/SIMD95.COM && \
cat /tmp/simd95/readme.txt > /tmp/dosbox/c/EXTRA/SIMD95.TXT && \
echo >> /tmp/dosbox/c/EXTRA/SIMD95.TXT && \
cat /tmp/simd95/licence.txt >> /tmp/dosbox/c/EXTRA/SIMD95.TXT

# Copy SPLI8MB.EXE from original patchmem
unzip -o -d /tmp/dosbox/c/EXTRA /opt/drivers/patchmem.zip SPLIT8MB.EXE

# video is not needed
export SDL_VIDEODRIVER=dummy

# create dosbox.conf
export DBCFG=/tmp/dosbox/dosbox.conf
echo "[autoexec]" > $DBCFG
echo "mount C: c" >> $DBCFG
echo "C:" >> $DBCFG
echo "rescan" >> $DBCFG
echo "imgmount a: floppy.ima -t floppy" >> $DBCFG
echo "A:" >> $DBCFG
# use xcopy from template
echo "xcopy /E /Y C:\\ A:\\" >> $DBCFG
echo "exit" >> $DBCFG

# run dosbox
cd /tmp/dosbox && dosbox && cd $SRCDIR

# copy images
cp /tmp/dosbox/floppy.ima /tmp/patcher9x-$VERSION-boot.ima
cp /tmp/dosbox/floppy.ima /tmp/patcher9x-$VERSION-boot.img
