#!/bin/bash

# start the trace;
# wait 5 seconds; #NB ERA 10!
# plug the drive;
# wait 5 seconds;
# mount the drive;
# wait 5 seconds;
# copy a 100MB file;
# wait 5 seconds;
# unmount the drive;
# wait 5 seconds;
# stop the trace.


dimensione=$1 # in BYTES!
wait=5 # Secondi da attendere per inserire la pendrive #NB ERA 10!

testfile=/NoBackup/testfile_$dimensione
echo -n "Using testfile $testfile..."
if [ ! -e $testfile ]
then
  echo " file doesn't exist (yet), creating..."
  dd if=/dev/urandom of=$testfile bs=$dimensione count=1 iflag=fullblock status=progress
else
  echo " (file is already present)"
fi

echo "STACCA TUTTI GLI USB! ANCHE LA PENDRIVE!"
read -p "Premi INVIO per continuare, o ctrl+C per annullare..."

timestamp=$(date +"%Y%m%d_%H%M%S")
echo "Timestamp: $timestamp"
SECONDS=0

cat /sys/kernel/debug/usb/usbmon/3u > bad.mon.out_"$dimensione"_$timestamp &
for ((i=0; i<=$(($wait-1)); i++))
do
  echo "Plug the drive in $(($wait-$i)) seconds..."
  sleep 1
done
echo "Plug the drive!"
sleep 5
echo "Prima di mount: $SECONDS"
# mount UUID="05141239-4ea5-494d-aa91-acd67db89ce5" /mnt/pendrive # EVEN
mount UUID="6a9d3c05-6758-49c0-a46e-6ce221478eb3" /mnt/pendrive # ODD
echo "Dopo mount: $SECONDS"
sleep 5
echo "Prima di cp: $SECONDS"
cp $testfile /mnt/pendrive/
echo "Dopo cp: $SECONDS"
sleep 5
echo "Prima di unmount: $SECONDS"
umount /mnt/pendrive
echo "Dopo unmount: $SECONDS"
sleep 5
echo "Prima di stop trace: $SECONDS"
killall cat
echo "Dopo stop trace: $SECONDS"
echo "...remember to unplug the drive, if you wish to do another usbmon test."
# spd-say "Andrea, ho finito!"
exit 0
