#Simple Script to Dump Contents of Live to CSV
#Author: Lee Kirkpatrick
#Date: 25/04/2016


#Define Colours
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BROWN="$(tput setaf 4)"
PINK="$(tput setaf 6)"
RESET="$(tput setaf 7)"


check_conn()
{
tput clear
echo -e "Checking connectivity to cms.netwitness.com\n"
connTest=$(curl -sL -w "%{http_code}\\n" "cms.netwitness.com" -o /dev/null)
if [ "$connTest" != "200" ]
then

    echo -e "$RED [BAD] $RESET - Cannot reach cms.netwitness.com! Fix the issue and try again....."
    echo -e "$YELLOW Exiting...$RESET"
else

    echo -e "$GREEN [GOOD] $RESET - Successful connection to cms.netwitness.com"
    sleep 3
    get_creds
fi
}


#Request Live Credentials
get_creds()
{
tput clear
echo -n "Please enter RSA Live Username: " 
read username
echo -n "Please enter RSA Live Password for $username: "
read -s password
echo
cred_check
}



cred_check()
{
if [[ $username = "" ]]
then
	echo -e "$RED [BAD] $RESET - Username must have a value..."
	sleep 2
	get_creds
else
	loginTest=$(curl -u $username:$password "https://cms.netwitness.com" -s)
	if [[ $loginTest = "" ]]
		then
			echo -e "\n$GREEN [GOOD] $RESET - Successfully Logged in to RSA Live"
			sleep 2
			dump_options
		else
			echo -e "\n$RED [BAD] - $RESET Incorrect Credentials, try again..."
			sleep 2
			get_creds
	fi
fi
}



dump_options()
{
tput clear
echo -e """$YELLOW
 (                     (                                      
 )\ )                  )\ )                                   
(()/( (    )      (   (()/(     (      )             (   (    
 /(_)))\  /((    ))\   /(_))   ))\    (     '  )    ))\  )(   
(_)) ((_)(_))\  /((_) (_))_   /((_)   )\  ' /(/(   /((_)(()\  
| |   (_)_)((_)(_))    |   \ (_))(  _((_)) ((_)_\ (_))   ((_) $RED
| |__ | |\ V / / -_)   | |) || || || '  \()| '_ \)/ -_) | '_| 
|____||_| \_/  \___|   |___/  \_,_||_|_|_| | .__/ \___| |_|   
                                           |_|                
$RESET"""

echo "1. Dump Flex Parsers"
echo "2. Dump Lua Parsers"
echo "3. Dump App Rules"
echo "4. Dump Feeds" 
echo "5. Dump Correlation Rules"
echo "6. Dump Log Parsers"
echo "7. Dump ESA Rules"
echo "8. All of the Above"
echo "9. Anything else to exit..."
read option

if [[ $option = "1" ]]
then
        dumpOption="parser"
	dump $dumpOption
elif [[ $option = "2" ]]
then
	dumpOption="luaParser"
	dump $dumpOption
elif [[ $option = "3" ]]
then
        dumpOption="appRule"
	dump $dumpOption
elif [[ $option = "4" ]]
then
	dumpOption="feed"
        dump $dumpOption
elif [[ $option = "5" ]]
then
	dumpOption="correlationRule"
	dump $dumpOption
elif [[ $option = "6" ]]
then
	dumpOption="logDevice"
	dump $dumpOption
elif [[ $option = "7" ]]
then
	dumpOption="esaRule"
	dump $dumpOption
elif [[ $option = "8" ]]
then
	carryOn="true"
	declare -a dumpAll=('parser' 'luaParser' 'appRule' 'feed' 'correlationRule' 'logdevice' 'esaRule');
	for dumpOption in "${dumpAll[@]}"
        do
		dump $dumpOption $carryOn
	done
	
	if [[ -f "RSALive-FullOuput.csv" ]]
	then
		rm RSALive-FullOuput.csv
	else
		continue="1"
	fi
	
	for i in "${dumpAll[@]}"
	do
		cat RSALive-$i.csv >> RSALive-FullOuput.csv
	done
	sed -i '2,${/^Name/d;}' RSALive-FullOuput.csv
	echo -e "\n$YELLOW [X] $RESET - Merging all to one CSV file..."
	echo -e "$GREEN [X} $RESET - "RSALive-FullOutput.csv" created\n"
	
else
	exit
	ls
fi
}



dump()
{
echo -e "\n$YELLOW [X] $RESET - Dumping selection of $dumpOption..."
#Grab Names for Dump Option
curl -u $username:$password "https://cms.netwitness.com/alfresco/service/search?types=nw%5C:$dumpOption&releaseMode=release" -k -s | grep 'term="display"' | awk -F "=" '{print $3}' | sed 's/"//g;s/\/>//g' >> Names.txt

#Grab Summary for Dump Option
curl -u $username:$password "https://cms.netwitness.com/alfresco/service/search?types=nw%5C:$dumpOption&releaseMode=release" -k -s | grep '<summary>' | sed 's/<summary>//g;s/<\/summary>//g;s/    //g' >> Summary.txt

#Grab Last Updated for Dump Option
curl -u $username:$password "https://cms.netwitness.com/alfresco/service/search?types=nw%5C:$dumpOption&releaseMode=release" -k -s | grep '</updated>' | tail -n +2 | sed 's/    //g;s/<updated>//g;s/<\/updated>//g' >> Updated.txt

#Grab Download Link
curl -u nawraslive:aA\!6CN87Fj "https://cms.netwitness.com/alfresco/service/search?types=nw%5C:$dumpOption&releaseMode=release" -k -s | grep '<link rel="alternate" href=' | sed 's/<link rel="alternate" href="//g;s/"\/>//g;s/    //g' >> URL.txt

#Remove Commas from Summary
sed -i 's/,//g' Summary.txt

#Add Content Type Column
sed -i 's/^/'$dumpOption',/g' Names.txt

#Combine the files into a CSV
paste -d "," Names.txt Summary.txt Updated.txt URL.txt >> Output.csv

#Tidy Up
tr -d '\r' < Output.csv > RSALive-$dumpOption.csv
sed -i '1s/^/Content Type,Name,Summary,Updated,Download Link\n/' RSALive-$dumpOption.csv
sed -i 's/&quot;/"/g' RSALive-$dumpOption.csv
rm Updated.txt
rm Names.txt
rm Summary.txt
rm Output.csv
rm URL.txt
echo -e "$GREEN [X} $RESET - "RSALive-$dumpOption.csv" created"

if [[ $carryOn != "true" ]]
then
	sleep 3
	dump_options
else
	continue="1"
fi



}



check_conn
