b62da609cf70688466da60372a361df5488c75e6
mspeir
  Thu Jan 5 10:40:21 2017 -0800
Adding check to see if user-supplied directory exists before accepting it. Also adding command that checks to see of there is a hubFiles directory in the supplied directory and creates it if it doesn't exist. refs #18603 #18316

diff --git src/utils/qa/getPubHubContact.sh src/utils/qa/getPubHubContact.sh
index a07fbff..460e78a 100755
--- src/utils/qa/getPubHubContact.sh
+++ src/utils/qa/getPubHubContact.sh
@@ -1,107 +1,117 @@
 #!/bin/bash
 
 # quit if something within the script fails
 set -beEu -o pipefail
 source `which qaConfig.bash`
 umask 002
 
 ################################
 #
 #  12-20-2016
 #  Matthew Speir
 #
 #  getPubHubInfo.sh
 #  Collects contact information for
 #  hubs listed in hgcentral.hubPublic.
 #  Outputs contact information to HTML-
 #  formatted file.
 #
 ################################
 
 usage="""
 Collect contact information for hubs in hgcentral.hubPublic on the RR. Places all
 contact information into an HTML-formatted page.\nDefault output location:\n
 /usr/local/apache/htdocs-genecats/qa/test-results/publicHubContactInfo\n\n
 `basename $0` go [outputLocation]\n\n\
 outputLocation is a path to the directory where output should be placed. If not set,
 script assumes default location above.\n
 """
 
 runScript=""
 base=""
 
 # Print usage statement
 if (( $# < 1 )) || (( $# > 2 ))
 then
 	echo -e $usage
 	exit 1
 elif (( $# == 1 ))
 then
 	runScript=$1
 elif (( $# == 2 ))
 then
+	# Check if outputLocation exists
+	if [[ -d $2 ]]
+	then
 		runScript="$1"
 		base="$2"
+	else
+		echo -e "Sorry, directory \"$2\" does not exist. Check spelling or create this directory and try again.\n"
+		exit 1
+	fi
 fi
 
 # If no user provided base location, then assume it's being run on dev and output to default loc.
 if [[ "$base" == "" ]]
 then
 	base="/usr/local/apache/htdocs-genecats/qa/test-results/publicHubContactInfo"
 fi
 
+# Create "hubFiles" directory in base if it doesn't exist
+mkdir -p $base/hubFiles
+
 if [[ "$runScript" == "go" ]]
 then
 	contactFile="$base/publicHubContact.html"
 	# Check if old contact file exists and if it does move it to archive file
 	if [ -e $contactFile ]
 	then
 		mv $contactFile $contactFile.old
 	fi
 
 	# Make header for html file
 	echo -e "
 	<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
 		\"http://www.w3.org/TR/html4/loose.dtd\">
 	" >> $contactFile
 
 	while read url label
 	do
 		# Save hub shortLabel as a file so we can use it in a filename later
 		label=$(echo $label | sed -e 's/ /_/g')
 
 		# Make name for hub.txt output file that includes hub shortLabel
 		hubFile="$base/hubFiles/$label.hub.txt"
 		wget -t 5 -O $hubFile $url &> /dev/null
 
 		# Extract email from hub.txt file we saved
 		email=$(egrep "^email" $hubFile)
 
 		# If email is empty, then wget failed or hub is down
 		if [[ $email == "" ]]
 		then
 			# Attempt to get hub.txt file w/ curl
 			curl --retry 5 $url -o $hubFile  &> /dev/null
 			# Extract email from hub.txt file we saved
 			email=$(grep "^email" $hubFile)
 
 			# If email is still empty, hub is likely down and
 			# we want to use the last email we have as contact email
 			if [[ $email == "" ]] && [ -e $contactFile.old ]
 			then
 				email=$(grep "$url" $contactFile.old | awk '{print $4" "$5}')
 			fi
 		fi
 
 		# Create hyperlinks to hub.txt files
 		hubLink="<a href=\"$url\" target=\"_blank\">$label</a>"
 
 		# Write contact email + hubUrl to file
 		echo -e "$hubLink $email<br>" >> $contactFile
 	
 	done<<<"$(hgsql -h genome-centdb -Ne "select hubUrl,shortLabel from hubPublic" hgcentral)"
 else
 	echo -e "Error: \"$1\" is not a valid input. Please see the usage message.\n\n"
 	echo -e $usage
 	exit 1
 fi