#!/bin/bash

function copy_text_files () {
   OTHER_DIR=files_to_image
   IMAGE_DIR=../../image

   cd ${OTHER_DIR}

   FILES=`find . -type f -name "*.txt"`

   for FILE_IN in $FILES
   do
      FILE_FINAL_NAME=`basename ${FILE_IN} | cut -f1 -d.`
      FILE_OUT=`echo ${IMAGE_DIR}/${FILE_IN} | cut -f-6 -d.`
      ../../tools/xdt99/xdm99.py -f DIS/VAR80 -n ${FILE_FINAL_NAME} -T ${FILE_IN} -o ${FILE_OUT}
   done

   cd ..
}

function copy_text_bin_files () {
   OTHER_DIR=files_to_image
   IMAGE_DIR=../../image

   cd ${OTHER_DIR}

   FILES=`find . -type f -name "*.bin"`

   for FILE_IN in $FILES
   do
      FILE_OUT=`echo ${IMAGE_DIR}/${FILE_IN} | cut -f-6 -d.`

      if [[ ! -e "${FILE_OUT}" || "${FILE_IN}" -nt "${FILE_OUT}" ]]; then

         cp $FILE_IN .t1
         docker run --rm -v ${TOOLS_DIR}:/tools -v `pwd`:`pwd` -w `pwd` cmcureau/tms9900-gcc /tools/c2ubin/c2ubin -no_exec .t1 .t2 2>&1 | grep -v "The requested image"
         FILE_FINAL_NAME=`basename ${FILE_IN} | cut -f1 -d.`
         ../../tools/xdt99/xdm99.py -T .t2 -f INT/FIX255 -n "${FILE_FINAL_NAME}" -o "${FILE_OUT}"
      fi
   done

   cd ..
}

function copy_tipi_plugins () {
   OTHER_DIR=tipi_plugins
   IMAGE_DIR=../../image

   cd ${OTHER_DIR}
   cp -p * ${IMAGE_DIR}/PLUGINS

   cd ..
}

function make_unix_cartridge_rom () {
   cd ${BUILD_DIR}

   CWD=`pwd`
   cp -p ../layout/unix99r3.xml /tmp/layout.xml
   cat unix99r3_rom0.bin unix99r3_rom1.bin unix99r3_rom2.bin unix99r3_rom3.bin > /tmp/unix99r38.bin
   cp -p /tmp/unix99r38.bin uthree.bin
   cp -p /tmp/unix99r38.bin ../../uthree.bin
   cd /tmp
   zip ${CWD}/UNIX99R3.RPK layout.xml unix99r38.bin | grep -v adding | grep -v updating
   cd ${CWD}
   cp -p ${CWD}/UNIX99R3.RPK ../..
   cd ..
}

function make_all () {
   LIBTI99_DIR=`realpath ../libti99`
   TOOLS_DIR=`realpath ../tools`
   docker run --rm -v ${LIBTI99_DIR}:/libti99 -v ${TOOLS_DIR}:/tools -v `pwd`:`pwd` -w `pwd` cmcureau/tms9900-gcc make -j13 $1 2>&1 | grep -v "The requested image"
}

function clean_all () {
   cd ${BUILD_DIR}
   rm UNIX99R3.RPK
#  rm unix99r3.hd
#  rm unix99r3.tar
   cd ..
}

function make_programs () {
   cd ${BUILD_DIR}

   BASE_DIR=${1}
   EXES="${2}"
   
   IMAGE_DIR=../../image/${BASE_DIR}
   for EXE in ${EXES}
   do
      C2UBIN_FILE=${EXE}.c2ubin
      if [ -e ${C2UBIN_FILE} ]
      then
         FINAL_FILE=${IMAGE_DIR}/${EXE}
         ../../tools/xdt99/xdm99.py -T "${C2UBIN_FILE}" -f INT/FIX255 -n "${EXE}" -o "${FINAL_FILE}"
      else
         echo could not find ${C2UBIN_FILE}
      fi
   done

   cd ..
}

# the main program

BUILD_DIR=build

# udpate version numbers
ver_bldr

# make all compile-link or clean
make_all $1

# check if clean operation
if [ "$1" == "clean" ]
then
   # clean the image directories
   clean_all
else
   echo remove icloud versioning files
   find . -name "* *" -exec rm {} \;

   echo "make_unix_cartridge_rom"
   make_unix_cartridge_rom

   # make the executables
   echo "make_programs bin"
   make_programs bin "launchd unix99logo adduser basic bye cat catb cp date diff lp cd clear color cut df display echo exit fontload free grep groff head hexdump id login logout ls man mkdir more ms_drv mv passwd pwd reboot rm rmdir rmuser sh shutdown sort stat tail tee touch uname vi wc xkb_drv"

   echo "make_programs example"
   make_programs example "basic_ex bitmap_1 bitmap_2 bitmap_3 comp double dsr file_bin file_bin2 file_bl file_bseq file_k file_text files gets hello imagedraw images chris life long longtest malloc mouse pi pio shmcreate shmreader shmremove shmmgr sound speech test textfmt tmatrix sprite sptest sramtest"

   echo "make_programs game"
   make_programs game "pacman missile"

   # copy the tipi plugins
   echo copy_tipi_plugins
   copy_tipi_plugins

   echo copy_text_files
   copy_text_files

   echo copy_text_bin_files
   copy_text_bin_files

   echo build distribution
   ../build_distro

   echo build mame hd
   ../build_mame_image
fi
