#!/bin/bash

PWD_SHORT=`dirname ${0}`
PWD_LONG=`realpath ${PWD_SHORT}`

cd ${PWD_LONG}

TEMPLATE_HD=unix99r3/files_to_image/unix99r3_nofiles.hd
BUILD_HD=${PWD_LONG}/unix99r3.hd
IMAGE_DIR=`realpath image`
cp -p ${TEMPLATE_HD} ${BUILD_HD}

cd ${IMAGE_DIR}
   
FILES=`find . -type f`
DIRS=`dirname ${FILES} | sort -u | grep -v PLUGINS`
PATH=${PATH}:/tools/tiimagetool
 
for DIR in $DIRS
do
   cd ${DIR}

   IMAGE_DIR_NAME=`echo ${DIR} |cut -c3- | tr '/' '.'`

   # remove any proc files that may have been added if an emulator has run against this image
   if [ "${DIR}" == "./proc" ]
   then
      rm * 
   fi

   java -classpath ${PWD_LONG}/tools/tiimagetool/tiimagetool.jar de.mizapf.timt.CommandShell import ${BUILD_HD} ${IMAGE_DIR_NAME}
   cd ${IMAGE_DIR}
done
