#! /bin/bash

uri_file=$1

LOCAL_DIR="./data"
mkdir "${LOCAL_DIR}/uris"


function get_uris_from_sameas() {
	
	local sameAsLink=$1
	local domain=`awk -F/ '{print $3}' <<<"$sameAsLink"`

	local propfile="${LOCAL_DIR}/uris/${domain}"

	if ! [[ -e $propfile ]];
	then
		touch $propfile
	fi

	echo $sameAsLink >> $propfile
	#rapper -g $sameAsLink | cut -d " " -f2 | sed "s/[<>]//g" >> $propfile
}


cat $uri_file | while read viaf_uri; do
	# print out a time stamp for debugging
	timestamp=`date +"%D %T"`
	>&2 echo "${timestamp}     ${viaf_uri}"

        # download rdfxml directly
        sameAsLinks=`rapper -g "${viaf_uri}/rdf.xml" | grep "${viaf_uri}" | grep "http://schema.org/sameAs" | cut -d ' ' -f3 | sed "s/[<>]//g"`
	>&2 echo "Found sameAs Links: ${sameAsLinks}"

	for sa in $sameAsLinks; do
		
		#echo $sa
		get_uris_from_sameas $sa 
	done
done


