#!/bin/sh
#if [ ! -d /media/usb0 ]; then
#  echo "cannot found /media/usb0"
#  echo "please insert usb storage."
#  exit
#fi

FOUND_EXTERNAL_USB_STORAGE=
for f in `df | awk '{print $6}'`; do
  if [ $f = "/media/usb0" ]; then
    FOUND_EXTERNAL_USB_STORAGE=$f
    break
  fi
done
if [ "$FOUND_EXTERNAL_USB_STORAGE" = "/media/usb0" ]; then
  mkdir -p /media/usb0/output
else
  echo "cannot found /media/usb0"
  echo "please insert usb storage."
  exit
fi

SALVAGE_DATE=`date +%F-%H%M%S`
for f in /tmp/input* /tmp/output* /tmp/_output*; do
  if [ -e $f ]; then
    mkdir -p /media/usb0/salvage/$SALVAGE_DATE
    mv /tmp/input* /media/usb0/salvage/$SALVAGE_DATE/
    mv /tmp/output* /media/usb0/salvage/$SALVAGE_DATE/
    mv /tmp/_output* /media/usb0/salvage/$SALVAGE_DATE/
    echo "!!!! CAUTION !!!! auto salvage executed."
    echo "note: move from /tmp/<data> to /media/usb0/salvage/<date>/<data>."
    break
  fi
done

DATE_SUFFIX=`date +%F-%H%M%S`
mkdir -p /media/usb0/output/$DATE_SUFFIX
./vsserver -d /dev/obd -f /media/usb0/output/$DATE_SUFFIX/logged.can &
/hark/hark --config /hark/hark-ody_recording.conf

sleep 2

ps | grep /vsserver | grep -v grep | awk '{print "kill -9 "$1}' > ./killhark.sh
ps | grep /hark | grep -v grep | awk '{print "kill -9 "$1}' > ./killhark.sh
chmod -c 777 ./killhark.sh
./killhark.sh
rm ./killhark.sh
echo "Process kill completed."

echo "Please wait... moving to usb storage now."
mv /tmp/input* /media/usb0/output/$DATE_SUFFIX/
mv /tmp/output* /media/usb0/output/$DATE_SUFFIX/
mv /tmp/_output* /media/usb0/output/$DATE_SUFFIX/
echo "Data moving process completed."

