#!/bin/bash
# Copyright (C) 2008 - 2012 vortexbox.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

NOW=`date +'%m/%d/%Y %H:%M:%S'`
NEWDEV="/dev/$2"
DISKINFO=`/sbin/fdisk -l $NEWDEV | grep -m1 Disk`
DISKSIZE=`echo $DISKINFO | awk '{print $5}'`

#echo "1= $1" >> /var/log/usbbackup.log
#echo "2= $2" >> /var/log/usbbackup.log
#echo "3= $3" >> /var/log/usbbackup.log
#echo "NEWDEV= $NEWDEV" >> /var/log/usbbackup.log
#echo "DISKINFO= $DISKINFO" >> /var/log/usbbackup.log
#echo "DISKSIZE= ${DISKSIZE}" >> /var/log/usbbackup.log

if [ "$DISKINFO" ]; then
 if [ -z "$3" ]; then
  if [ "$1" = "add" ]; then
    echo "$NOW - USB disk added [$DISKINFO]" >> /var/log/usbbackup.log
    if [ "$DISKSIZE" -gt "10000000000" ]; then
	 echo $NEWDEV > /etc/vortexbox/CurrentUsbDrive.txt
	 if [ ! -d "/backup" ]; then mkdir /backup; fi
	 umount /backup
	 rm -rf /backup/*
	else
	 if [ -d "/hidefstick" ]; then
	  /opt/vortexbox/playaction ${NEWDEV}1 &
	 fi
	fi
  fi 
 fi
else
 if [ -z "$3" ]; then
  echo "$NOW - USB disk $NEWDEV removed" >> /var/log/usbbackup.log
  umount /hidefstick
  umount /backup
  rm /etc/vortexbox/CurrentUsbDrive.txt
 fi
fi
