#!/bin/sh
#] 
#] *********************
#] $ bash "$d_bin"'virus/virus - clamscan.sh'  - clam virus scan of specified [drives, directories]
# www.BillHowell.ca created 21Jun2020 from QNial program dating back to 09Jun2016 
# 03Nov2020 added diff etc
# check that cron is always running freshclam!!  


# 
# 08Nov2015 Warning - it appears that only a small portion of /home/bill is scanned !!!  
#48************************************************48




#24************************24
# Table of Contents, generate with :
# $ grep  "^#]"  "$d_bin"'virus/virus - clamscan.sh' |  sed "s/^#\]/  /"  >"$d_bin"'virus/virus - clamscan TblOfCont.txt'
#


#24************************24


#24************************24
#  Summary

# clamscan IS  - virus protection - freshclam & clamscan 

# Notes : 
# includes bash versions from earlier clamscans that still apply
# 03Jun2015  error msg - /var/log/clamav/freshclam.log is locked by another process
#    This is owned by adm, but I am a member of that group?

#08********08
# ToDos 
# 03Nov2020 I need a bash header function that saves backups!!




#**********************************
#] initial setup [header, environmental variables (enVars)]
# loose collection of handy routines [plog,dates,becho,beval,...]
#   functions [becho, beval] track [progress, errors, variables]
# environmental variables must be set BEFORE running!  (see _Howell email.txt)

source  "$d_bin""fileops.sh"

# defined in header file - must refresh for stages of process
#echo  "date_ymdhm= $date_ymdhm"
#echo  "date_ddmmmyyyy= $date_ddmmmyyyy"
#echo  "date_ddmmmyyyy_hm= $date_ddmmmyyyy_hm"

date_ymdhm=$(date +"%0y%0m%0d %0kh%0Mm")

d_anyRun="/media/bill/Virus_samples/any.run/"
d_virus="$d_bin"'virus/'

f_log='pLog virus clamscan.txt' 
p_log="$d_bin$f_log"
# don't lose ANY viruses, keep forever : 
p_standrd="$d_virus"'6_virus standard.txt'  
p_current="$d_virus"'7_virus current.txt'
p_diffrnt="$d_virus"'8_virus diff [standrd, current].txt'


#***********************************************
# functions

#] header()  - 

	header()
{  
	date_ddmmmyyyy_hms=$(date +"%0e%0b%0Y %0kh%0Mm%0Ss")
	becho  '' 
	becho  "08********08"
	becho  "$date_ddmmmyyyy_hms, start of virus - clamscan.sh" 
	becho  '' 
}  


#] footer()  - 

	footer()
{  
	date_ddmmmyyyy_hms=$(date +"%0e%0b%0Y %0kh%0Mm%0Ss")
	becho  '' 
	becho  "$date_ddmmmyyyy_hms, end of virus - clamscan.sh" 
	becho  '' 
}  


# clamscan functions - to compare current viruses and compare to historocal to find new ones

# clamscan options 
# -i, --infected    only print infected files
# -r, --recursive   scan directories recursively

#] clam_it  - clamscan virus protection of all disks

	clam_it()
{  
	dScan="$1"
	dExc1="$2"
	dExc2="$3"
	p_log="$d_bin"'clamscan'

	date_ddmmmyyyy_hms=$(date +"%0e%0b%0Y %0kh%0Mm%0Ss")
	becho  '' 
	becho  '+-----+' 
	becho  "$date_ddmmmyyyy_hms, $dScan" 
	beval  'clamscan >>"'"$p_current"'"  -ri --bell  --exclude-dir="'"$dExc1"'"  --exclude-dir="'"$dExc2"'"   "'"$dScan"'"  '
}  

# 	date_ddmmmyyyy_hm=$(date +"%e%B%Y %kh%Mm")


#] clam_special      #() clamscan virus protection with specific conditions

	clam_special()
{  
	dScan='/'
	dExc1='/home/'
	dExc2='/media/'
	dExc3='/proc/'
	dExc4='/timeshift/'

	header

	date_ddmmmyyyy_hms=$(date +"%0e%0b%0Y %0kh%0Mm%0Ss")
	becho  '+-----+' 
	becho  "$date_ddmmmyyyy_hms, $dScan" 
	beval  'clamscan >>"'"$p_current"'"  -ri --bell  --exclude-dir="'"$dExc1"'"  --exclude-dir="'"$dExc2"'"   --exclude-dir="'"$dExc3"'"    --exclude-dir="'"$dExc4"'"    "'"$dScan"'"  '

	footer
}  


#] clam_its      #() uncomment dirs to select which to clamscan

	clam_its()
{  
	becho  '' 
	becho  "08********08"
	becho  "$date_ddmmmyyyy_hms, clam_its() start of virus - clamscan.sh" 
	becho  '' 
	date_ddmmmyyyy_hms=$(date +"%0e%0b%0Y %0kh%0Mm%0Ss")
	header
	# uncomment to select which to run
	#							dScan										dExc1					dExc2
	#beval	'clam_it		'"'/home/bill/'"'						'"'$d_web'"'		'"'$d_PROJECTS'"'  '
	#beval	'clam_it		'"'$d_web'"'							'"'z_Archive/'"'	'"'z_Old/'"'       '
	 beval	'clam_it		'"'$d_PROJECTS'"'						'"'z_Archive/'"'	'"'z_Old/'"'       '
	#beval	'clam_it		'"'$d_Midas'"'							'"'z_Archive/'"'	'"'z_Old/'"'       '
	#beval	'clam_it		'"'/media/bill/Virus_samples'"'	'"'z_Archive/'"'	'"'z_Old/'"'       '
	footer
}

#	clam_it  "/home/bill/Thunderbird" - redundant
#	clam_it  "/media/bill/Dell2/SAMSUNG_G4"
#



# diff functions - to compare current virus and compare to historical to find new ones

#] diff_it      #() produce a list of FOUND detections in p_current that are NOT in historical list
## 03Nov2020 initial, 04Feb2024 incomplete changes
# NEVER auto-update p_standrd !!!  manually add new stuff

	diff_it()
{  
	date_ddmmmyyyy_hms=$(date +"%0e%0b%0Y %0kh%0Mm%0Ss")
	p_temp="$d_temp"'clamscan diff_it pTmp.txt'

	becho  '' 
	becho  "+-----+"
	becho  "$date_ddmmmyyyy_hms, diff_it() - compare p_[Current, Standard] virus to find new ones" 
	becho  '' 

	# diff list of additional viruses found, no attempt to [sort, rename old [dir, fil]s]
	diff  "$p_standrd"  "$p_current"  --suppress-common-lines  --exclude='z_Archive'  |  grep "^>" | sed "s|^> ||"  |  sort -u  >"$p_diffrnt"  

	echo  >>"$p_standrd"  ''
	echo  >>"$p_standrd"  "$date_ddmmmyyyy_hms"
	cat   >>"$p_standrd"  "$p_diffrnt"  

	nStandrd=$( wc -l  "$p_standrd"  |  sed  's|\([0-9]*\) .*|\1|' )
	nCurrent=$( wc -l  "$p_current"  |  sed  's|\([0-9]*\) .*|\1|' )
	nDiffrnt=$( wc -l  "$p_diffrnt"  |  sed  's|\([0-9]*\) .*|\1|' )

	becho  'key results : '
	becho  "nStandrd= $nStandrd"
	becho  "nCurrent= $nCurrent"
	becho  "nDiffrnt= $nDiffrnt"
	becho  '' 
}  

# 11May2025 examples of beval - taken mostly from "$d_bin"'standard header.sh'
#	beval  'rsync  --stats  -rltgu  "'"$d_travel"'bin_home/"  '"$d_home"'bin"  >>"'"$p_log"'" '
#	beval  'gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-key "'"$keye_ID"'"  >>"'"$p_log"'" '
#	beval  'keye_sha=$( sha256sum -b '"'$p_iso'"' )'
#	beval  'code_sig=$( echo "'"$keye_sig"'"  |  sed "s/\([a-z,0-9]*\) .*/\1/" )'

# 12May2025 
# $ d_virus="$d_bin"'virus/'
# $ p_standrd="$d_virus"'6_virus standard.txt'  
# $ p_current="$d_virus"'7_virus current.txt'
# $ diff  "$p_standrd"  "$p_current"  --suppress-common-lines  --exclude='z_Archive'  




#**********************************************
# Functions specific to [drives, directories]



#*******************************************************************************
# Procedures -  To run :
# 01.	Preferences - power (screen & power!!) & screen saver to Never
# 02.	Shut off internet connections
# 03.	ensure USB drives [Midas, virus, other] are mounted
# 04.	"uncomment those to run"  (list below) 
# 06.	save this file
# 07.	copy-paste to command line :   $ bash "$d_bin"'virus/virus - clamscan.sh'  

#	IMPORTANT! my hourly virus database updates no longer occur : 
	#becho  'sudo freshclam update of virus database'
	#sudo freshclam

	if [ -f "$p_standrd" ]; then  pinn_arcArch  "$p_standrd";  fi
	if [ -f "$p_current" ]; then  pinn_arcArch  "$p_current";  fi
	if [ -f "$p_diffrnt" ]; then  pinn_arcArch  "$p_diffrnt";  fi


#	uncomment the one to run
	#clam_its				#() uncomment dirs to select which to clamscan
	#clam_special			#() clamscan virus protection with specific conditions

# diff_it: ALWAYS ON! has to be run after either of the above :
	#diff_it					#() produce a list of FOUND detections in p_log NOT in standard historical list

# Final manual steps : 
	#add comments for the session to 
	#move_contaminated files into USBvirus



# enddoc
