#!/bin/sh
test -z "$1" && echo "Usage: split-by-one.sh big-file" && exit

FILE="$1"
SIZE="1" # in Gb!
FLASH="/flash"

echo "Writing ${SIZE}000M parts of file $FILE to $FLASH:"
echo
if [ ! -f "$FILE" ] ; then echo "no such file: $FILE"; exit; fi

umount "$FLASH" 2> /dev/null
declare -i i=0

while true; do
    echo "Insert flash-card, press [Enter]" && read
    mount "$FLASH" || exit
    test -n "$j" && rm -f "$FLASH/$FILE.$j"

    echo -n "Writing part $i... "
    dd if="$FILE" of="$FLASH/$FILE.$i" bs=${SIZE}000M skip=${i}000 count=1000 || exit
    test -s "$FLASH/$FILE.$i" || break
    echo -n "Unmounting... "
    umount "$FLASH" || exit

    echo -e "OK\nYou can remove flash-card.\n"
    sleep 5
    j=$i
    let i=i+1
done

rm -f "$FLASH/$FILE.$i"
umount "$FLASH" 2> /dev/null

echo
echo "All done"
