#!/bin/sh

if [ -n "$1" ]; then

if [ "$1" = "-q" ]; then
   shift
   quiet=" -i"
fi

if [ "$1" = "-t" ]; then
if [ -n "$2" ]; then
if [ -d "$2" ]; then
   shift 2
   tempdir=" --tempdir=$2"
else
   echo "No such directory \`$2'!"
   exit 1
fi
else
   echo "You didn't specify a temp directory \`-t directory'!"
   exit 1
fi
fi

if [ -z "$1" ]; then
   echo "You didn't specify a device!"
   exit 1
elif [ -b "$1" ]; then

 if [ ! -x "`type -path clamscan`" ]; then
    echo "Can't find \`clamscan'!"
    exit 1
 elif [ ! -x "`type -path ntfs-3g`" ]; then
    echo "Can't find \`ntfs-3g'!"
    exit 1
 fi
 
if [ ! -f /var/lib/clamav/main.cvd -o ! -f /var/lib/clamav/daily.cvd ]; then
   echo "ERROR: Run 'freshclam' to create or update the virus data files!"
   exit
fi

umount $1 2>/dev/null
umount /tmp/CLAMAV 2>/dev/null
mkdir -p /tmp/CLAMAV
  ntfs-3g $1 /tmp/CLAMAV -o ro 2>/dev/null
if [ $? = 0 ]; then
  ntfs=yes
else
  mount -t vfat -o ro $1 /tmp/CLAMAV || exit
fi

echo "*** Scanning \`$1' for VIRUSES..."
if [ -z "$quiet" ]; then
   sleep 5
fi
rm -f /var/log/clamscan.log
clamscan --exclude-dir VIRUSES --exclude=pagefile.sys --exclude=hiberfil.sys$quiet$tempdir -r -l /var/log/clamscan.log /tmp/CLAMAV
grep FOUND /var/log/clamscan.log | awk '{print $1}' | cut -d : -f 1 > /tmp/viruses.log
if [ -s /tmp/viruses.log ]; then
echo "*** VIRUSES found on \`$1'! (read /var/log/clamscan.log)"
echo "*** Remounting \`$1' read-write."
umount /tmp/CLAMAV || exit
if [ "$ntfs" = "yes" ]; then
  ntfs-3g $1 /tmp/CLAMAV -o rw,force || exit
else
  mount -t vfat -o rw $1 /tmp/CLAMAV || exit
fi
echo "*** Copying infected files to /VIRUSES/viruses.tar.gz on \`$1'," 
echo "*** you'll be prompted afterwards for deletion!"
mkdir -p /tmp/CLAMAV/VIRUSES		
cat /tmp/viruses.log | while read v 
do 
tar rf /tmp/CLAMAV/VIRUSES/viruses.tar "$v" 2>/dev/null
done
gzip /tmp/CLAMAV/VIRUSES/viruses.tar 2>/dev/null
cp -f /var/log/clamscan.log /tmp/CLAMAV/VIRUSES/clamscan-log.txt
unix2dos /tmp/CLAMAV/VIRUSES/clamscan-log.txt 2>/dev/null
else 
umount /tmp/CLAMAV 2>/dev/null && rmdir /tmp/CLAMAV
echo "*** No VIRUSES found on \`$1'!"
exit 0
fi
else
echo "No such device \`$1'!"
exit 1
fi
else
echo '
-------------------------------------------------------------------------
*** Use CLAMAV to scan for VIRUSES on Windows system! ***

usage: vscan device

Run 'freshclam' first to create or update the virus data files!
# freshclam

If you have a Windows system on /dev/hda1 you want to scan!

example: vscan /dev/hda1

example: vscan -q /dev/hda1               "Be Quiet, unless VIRUS is found!"

example: vscan -q -t /temp_dir /dev/hda1  "Specify temp directory to use,
                                           the default is /tmp!"    
-------------------------------------------------------------------------
'
exit 1
fi

while :
do
echo "
Would you like to delete ALL infected files at once?

Warning! Some viruses infect system-critical files and may prevent
your system from booting if they're removed. Clamav can only detect
viruses, but not clean files internally.

You can restore your files afterwards from the quarantine archives in
/VIRUSES. This will put back every file, including viruses, to its
original location.
"
echo -n "('y' or 'n' or 'l' to list?) "
read CHOOSE
if [ $CHOOSE = y -o $CHOOSE = Y ]; then
  break
elif [ $CHOOSE = l ]; then
unset LESS
echo "
*** Press 'q' to Quit pager!

`grep FOUND /var/log/clamscan.log`" | less -MiQ
else 
  umount /tmp/CLAMAV 2>/dev/null && rmdir /tmp/CLAMAV
  rm -f /tmp/viruses.log
  echo "Done!"
  exit 0
fi
done

cat /tmp/viruses.log | while read k 
do
   rm -v -f "$k"
done

umount /tmp/CLAMAV 2>/dev/null && rmdir /tmp/CLAMAV
rm -f /tmp/viruses.log
echo "Done!"
exit 0
