#!/bin/bash
cd "$(dirname "$0")"
mkdir -p build
cd build
download_if_needed() {
  local file=$(basename $1)
  if [[ ! -f "$file" || $(stat -c%s "$file") -lt 1500 ]]; then
    curl -LO $1
  fi
}

download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/windows.inc"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/kernel32.inc"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/winextra.inc"
uasm -coff ../WriteFile.asm
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/Irvine32.inc"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/SmallWin.inc"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/VirtualKeys.inc"
uasm -coff ../PZ25_6.asm
uasm -coff ../ReadFile.asm
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/Kernel32.Lib"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/Irvine32.lib"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/User32.Lib"
download_if_needed "https://github.com/therealdreg/dregs-masm32-wine/raw/refs/heads/main/libs/msvcrt.lib"
link.exe /SUBSYSTEM:CONSOLE kernel32.Lib WriteFile.o 2>/dev/null
link.exe /SUBSYSTEM:CONSOLE kernel32.Lib User32.Lib Irvine32.lib ReadFile.o 2>/dev/null
link.exe /SUBSYSTEM:CONSOLE kernel32.Lib User32.Lib Irvine32.lib msvcrt.lib PZ25_6.o 2>/dev/null
