#!/bin/sh
# send a 4 byte (32 bit LE) count then a file to standard output

FileList='
kernel.elf
kernel/kernel.elf
mahatma.elf
kernel/mahatma.elf
KERNEL
forth.elf
forth/forth.elf
'

usage()
{
  [ -z "$1" ] || echo error: $* >&2
  echo usage: $(basename "$0") '<filename>' >&2
  exit 1
}

FileName="$1"

if [ -z "${FileName}" ]
then
  for f in ${FileList}
  do
    echo trying: ${f} >&2
    if [ -e "${f}" ]
    then
      FileName="${f}"
      break
    fi
  done
  [ -z "${FileName}" ] && usage missing argument
else
  [ -e "${FileName}" ] || usage file, "'"${FileName}'"' does not exist >&2
fi

if [ -L "${FileName}" ]
then
  FileName=$(readlink -m "${FileName}")
fi

size=$(ls -l "${FileName}" | awk '{print $5}')

printf 'File: "%s", size = 0x%x [%d]\n' ${FileName} ${size} ${size} >&2

b1=$(printf '%02x' $((${size} & 0xff)))
b2=$(printf '%02x' $((( ${size} >> 8 ) & 0xff)))
b3=$(printf '%02x' $((( ${size} >> 16 ) & 0xff)))
b4=$(printf '%02x' $((( ${size} >> 24 ) & 0xff)))

printf "\x${b1}\x${b2}\x${b3}\x${b4}"
dd if="${FileName}" bs=1
