#!/bin/bash
# ehowton@us.ibm.com

# DO NOT RUN THIS MANUALLY - CALLED BY sanitize_emcgrab_runme.sh

#populate list with all the files in all the grabs
find * -type f >> emcgrabit
#remove this script and the newly-created list from itself
sed -i '/emcgrabit/d' emcgrabit
sed -i '/sanitize_emcgrab_data.sh/d' emcgrabit
#modify files with spaces to underscores and update the list to reflect
while IFS= read -r x ; do mv -- "$x" "`echo $x | sed "s/ /\_/g"`" ; done < emcgrabit
sed -i 's/ /\_/g' emcgrabit
#suppress read-only errors from sparse files
for x in `cat emcgrabit` ; do chmod -f -R 777 $x; done
find * -type f >> emcgrabit
for x in `cat emcgrabit` ; do chmod -f -R 777 $x; done
#Scrub hostname from containing directory
ls -l ../ | grep "^d" | awk -F" " '{print $9}' >> dirlist
sed -i 's/^/..\//' dirlist
HOSTNAME=`cat host/NetworkInfo.txt | grep Hostname | sed -r 's/# Hostname : //' | sed -r 's/\..*//'`
[[ -z "$HOSTNAME" ]] && unset $HOSTNAME || 
	for x in `cat dirlist` ; do mv $x `echo $x | sed -E "s/$HOSTNAME/HOSTNAME/"` ; done
#discover the hostnames and ips of the server and scrub them from every file in which they appear
HOSTNAME=`cat host/NetworkInfo.txt | grep Hostname | sed -r 's/# Hostname : //' | sed -r 's/\..*//'`
[[ -z "$HOSTNAME" ]] && unset $HOSTNAME || echo Scrubbing HOSTNAME $HOSTNAME
        for x in `cat emcgrabit` ; do sed -i -r "s/$HOSTNAME/HOSTNAME/g" $x ; done
IP0=`cat host/NetworkInfo.txt | grep VNic0IPAddress | sed 's/NetworkInfo-VNic0IPAddress: //'`
[[ -z "$IP0" ]] &&  unset $IP0 || echo Scrubbing IP $IP0
        for x in `cat emcgrabit` ; do sed -i -r "s/$IP0/IP0/g" $x ; done
IP1=`cat host/NetworkInfo.txt | grep VNic1IPAddress | sed 's/NetworkInfo-VNic1IPAddress: //'`
[[ -z "$IP1" ]] && unset $IP1 || echo Scrubbing IP $IP1
        for x in `cat emcgrabit` ; do sed -i -r  "s/$IP1/IP1/g" $x ; done
IP2=`cat host/NetworkInfo.txt | grep VNic2IPAddress | sed 's/NetworkInfo-VNic2IPAddress: //'`
[[ -z "$IP2" ]] && unset $IP2 || echo Scrubbing IP $IP2
        for x in `cat emcgrabit` ; do sed -i -r "s/$IP2/IP2/g" $x ; done
#cleanup
rm emcgrabit
rm dirlist
rm sanitize_emcgrab_data.sh
#mark complete
touch sanitize_complete
