#!/bin/bash 

#if [%1]==[justcompile] goto :justcompile

echo Generating pals
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts0.png out=work/palts0.h label=palts0 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts1.png out=work/palts1.h label=palts1 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts2.png out=work/palts2.h label=palts2 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts2a.png out=work/palts2a.h label=palts2a silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts3.png out=work/palts3.h label=palts3 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts3a.png out=work/palts3a.h label=palts3a silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts4.png out=work/palts4.h label=palts4 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts4a.png out=work/palts4a.h label=palts4a silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts5.png out=work/palts5.h label=palts5 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts5a.png out=work/palts5a.h label=palts5a silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts5b.png out=work/palts5b.h label=palts5b silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palts5c.png out=work/palts5c.h label=palts5c silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palss0.png out=work/palss0.h label=palss0 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palss0a.png out=work/palss0a.h label=palss0a silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palss1.png out=work/palss1.h label=palss1 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palss2.png out=work/palss2.h label=palss2 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palcuts0.png out=work/palcuts0.h label=palcuts0 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palcuts1.png out=work/palcuts1.h label=palcuts1 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/palcuts2.png out=work/palcuts2.h label=palcuts2 silent
../../../src/utils/mkts mode=pals pals=$(pwd)/../gfx/paltstitle.png out=work/paltstitle.h label=paltstitle silent
cat work/pal*.h > assets/palettes.h 

#rem chr-rom mapping
#rem CHRROM0: 8192 generated by import_patterns0.spt
#rem CHRROM1: 4096 generated by import_patterns1.spt + enem data levels 0, 1, 2, 3, 4.
#rem CHRROM2: 8192 generated by map exporter, continues on...
#rem CHRROM3: ?    generated by map exporter, + 2048 enem data level 5

echo Exporting chr
cd ../gfx
../../../src/utils/mkts mode=scripted in=$(pwd)/import_patterns0.spt out=../dev/tileset0.chr silent
../../../src/utils/mkts mode=scripted in=$(pwd)/import_patterns1.spt out=../dev/work/tileset1p.bin silent

echo Exporting enems
cd ../enems
../../../src/utils/eneexp3 level00.ene ../dev/work/enems00.h 00 1 gencounter bin
../../../src/utils/eneexp3 level01.ene ../dev/work/enems01.h 01 1 gencounter bin
../../../src/utils/eneexp3 level02.ene ../dev/work/enems02.h 02 1 gencounter bin

../../../src/utils/eneexp3 level10.ene ../dev/work/enems10.h 10 1 gencounter bin
../../../src/utils/eneexp3 level11.ene ../dev/work/enems11.h 11 1 gencounter bin
../../../src/utils/eneexp3 level12.ene ../dev/work/enems12.h 12 1 gencounter bin

../../../src/utils/eneexp3 level20.ene ../dev/work/enems20.h 20 1 gencounter bin
../../../src/utils/eneexp3 level21.ene ../dev/work/enems21.h 21 1 gencounter bin
../../../src/utils/eneexp3 level22.ene ../dev/work/enems22.h 22 1 gencounter bin

../../../src/utils/eneexp3 level30.ene ../dev/work/enems30.h 30 1 gencounter bin
../../../src/utils/eneexp3 level31.ene ../dev/work/enems31.h 31 1 gencounter bin
../../../src/utils/eneexp3 level32.ene ../dev/work/enems32.h 32 1 gencounter bin

../../../src/utils/eneexp3 level40.ene ../dev/work/enems40.h 40 1 gencounter bin
../../../src/utils/eneexp3 level41.ene ../dev/work/enems41.h 41 1 gencounter bin
../../../src/utils/eneexp3 level42.ene ../dev/work/enems42.h 42 1 gencounter bin

../../../src/utils/eneexp3 level50.ene ../dev/work/enems50.h 50 1 gencounter bin
../../../src/utils/eneexp3 level51.ene ../dev/work/enems51.h 51 1 gencounter bin
../../../src/utils/eneexp3 level52.ene ../dev/work/enems52.h 52 1 gencounter bin

cd ../dev
cat  work/enems*.h > assets/enem_constants.h 
../../../src/utils/binpaster index=work/enem_index1.h out=work/enems1.bin files=work/enems00.h.bin,work/enems01.h.bin,work/enems02.h.bin,work/enems10.h.bin,work/enems11.h.bin,work/enems12.h.bin,work/enems20.h.bin,work/enems21.h.bin,work/enems22.h.bin,work/enems30.h.bin,work/enems31.h.bin,work/enems32.h.bin,work/enems40.h.bin,work/enems41.h.bin,work/enems42.h.bin
../../../src/utils/binpaster index=work/enem_index2.h out=work/enems2.bin files=work/enems50.h.bin,work/enems51.h.bin,work/enems52.h.bin
cat work/enem_index1.h  work/enem_index2.h > assets/enem_index.h

echo Compiling enembehs
cd ../script
../../../src/utils/pencompiler enembehs.spt ../dev/assets/compiled_enems.h debug

echo Making map
cd ../map
../../../src/utils/rle44mapchrrom in=$(pwd)/maplist.txt bin=$(pwd)/../dev/work/mapchr.bin out=../dev/assets/chr_rom_maps.h chr=2
cd ../dev

echo Resizing and pasting binaries into CHR-ROM images

../../../src/utils/fillto work/mapchr.bin.3 6144
../../../src/utils/fillto work/enems1.bin 4096
../../../src/utils/fillto work/enems2.bin 2048

cat work/tileset1p.bin  work/enems1.bin > tileset1.chr
cp  work/mapchr.bin.2 tileset2.chr
cat  work/mapchr.bin.3 work/enems2.bin > tileset3.chr

echo Exporting music and sound
cd ../dev
wine ../../../src/utils/text2data.exe ../ogt/music.txt -ca65 -ch1..4
wine ../../../src/utils/nsf2data.exe ../ogt/sounds.nsf -ca65 -ntsc
cp  ../ogt/music.s .
cp  ../ogt/sounds.s .

cd ../dev

#if [%1]==[nocompile] goto :end

#:justcompile
#if [%2]==[noscript] goto :noscript

echo Building script
cd ../script
../../../src/utils/mscmk1 script.spt ../dev/assets/mscnes.h 25
cd ../dev

#:noscript
wine ../../../cc65_2.13.2/bin/cc65.exe -Oi game.c --add-source -D CNROM
wine ../../../cc65_2.13.2/bin/ca65.exe crt0.s -o crt0.o -D CNROM=1
wine ../../../cc65_2.13.2/bin/ca65.exe game.s
wine ../../../cc65_2.13.2/bin/ld65.exe -v -C nes-CNROM.cfg -o cart.nes crt0.o game.o runtime.lib -m labels.txt

rm *.o 
#rem rm game.s 

cp  cart.nes ../../espitene.nes
rm work/*.h 
rm work/*.bin.*

echo DONE!
