#!/bin/sh
# copy specic sets of file to SD Card

COPY="$(dirname "$0")/CopyToSD"

FlashPrefix=samo-lib/flash
ForthPrefix=samo-lib/forth
programs="${ForthPrefix}/programs"

sdcard=''
for item in /media/SAMO* /media/disk
do
  if [ -d "${item}" ]
  then
    sdcard="${item}"
    break
  fi
done

while :
do
  case "$1" in
    "")
      break
      ;;

    all|--all)
      ${COPY} --sdcard="${sdcard}" kernel.elf "${ForthPrefix}/"*.elf "${programs}/"*.4mu "${programs}/"*.4th "${programs}/"*.ini "${programs}/"*.tst
      ;;

    forth|--forth)
      ${COPY} --sdcard="${sdcard}" "${ForthPrefix}/"*.elf "${programs}/"*.4mu "${programs}/"*.4th "${programs}/"*.ini "${programs}/"*.tst
      ;;

    2|--2|stage2|--stage2)
      ${COPY} --sdcard="${sdcard}" "${ForthPrefix}/forth.elf" "${ForthPrefix}/forth.elf:kernel.elf" "${programs}/"*test.4th "${programs}/"stage2.4th:forth.ini
      ;;

    mtbf|--mtbf)
      ${COPY} --sdcard="${sdcard}" "${ForthPrefix}/forth.elf" "${ForthPrefix}/forth.elf:kernel.elf" "${programs}/"mtbf.4th "${programs}/"mtbf.4mu:forth.ini
      ;;

    image\=*|--image\=*)
      image=${1#*=}
      ${COPY} --sdcard="${sdcard}" "${image}/"*
      ;;

    flash\=*|--flash\=*)
      image=${1#*=}
      ${COPY} --sdcard="${sdcard}" "${FlashPrefix}/flash.elf" "${image}":flash.rom
      ;;

    *)
      echo usage: $(basename "$0") '<option>'
      echo
      echo The following options are available:
      echo
      echo '       all   - copy kernel.elf and all the forth programs'
      echo '       forth - copy all of forth (leaving existing kernel)'
      echo '       2     - make a stage 2 production test card'
      echo '       mtbf  - make an mtbf test card'
      echo
      echo 'If the card is not mounted, the script will use sudo'
      echo 'to mount/umount the card.'
      echo
      echo 'Note: to format SD Card use a command like:'
      echo '      sudo mkfs.vfat -n SAMO-8G /dev/sdb1'
      echo '      sudo mkfs.vfat -n SAMO-4G /dev/sdb1'
      echo '      sudo mkfs.vfat -n SAMO-512M /dev/sdb1'
      echo
      echo '(Change /dev/sdb1 to your drive and partition)'
      exit 1
      ;;
  esac
  shift
done

