#!/bin/sh -e

RELEASE_flags="-DNDEBUG -O2 -g0 -s"
DEBUG_flags="-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined"

LIN="uxncli $HOME/roms/uxnlin.rom"
ASM="uxncli $HOME/roms/drifblim.rom"

if [[ "$*" == *"--lint"* ]]
then
	clang-format -i ulzdec.c
	clang-format -i ulzenc.c
	$LIN ulzdec.tal
	$LIN ulzenc.tal
fi

# Building

cc ulzdec.c $DEBUG_flags -o ulzdec
cc ulzenc.c $DEBUG_flags -o ulzenc
$ASM ulzdec.tal ulzdec.rom
$ASM ulzenc.tal ulzenc.rom

if [[ "$*" == *"--install"* ]]
then
	cp ulzenc ~/bin
	cp ulzdec ~/bin
	cp ulzdec.rom ~/roms
	cp ulzenc.rom ~/roms
fi

# Make compressed file

echo ""
echo "Encoder(C89):"
./ulzenc example.txt a.ulz
echo "Decoder(C89):"
./ulzdec a.ulz b.bin
echo "Result(C89):"
cat b.bin
echo ""

rm ./a.ulz
rm ./b.bin

echo ""
echo "Encoder(Uxn):"
uxncli ulzenc.rom example.txt a.ulz
echo "Decoder(Uxn):"
uxncli ulzdec.rom a.ulz b.bin
echo "Result(Uxn):"
cat b.bin
echo ""

echo ""
echo "Image Encoding"
./ulzenc ako10x10.chr ako10x10.chr.ulz
uxncli ulzenc.rom ako10x10.chr ako10x10.chr.ulz

rm ./ulzdec
rm ./ulzenc
rm ./a.ulz
rm ./b.bin
rm ./ulzdec.rom
rm ./ulzenc.rom
rm ./ako10x10.chr.ulz
rm *.sym