#!/bin/bash

if [[ "$OSTYPE" =~ ^darwin ]]; then
    alias md5='md5 -r'
    alias md5sum='md5 -r'
    # https://sagebionetworks.jira.com/wiki/display/PLFM/Fixing+sed+on+OSx
    alias sed='gsed'
fi

for file in ./Aus*.xls
do
	echo "${file}"
	md5=`md5sum "${file}" | cut -d' ' -f 1`
	echo "${md5}"
	newfile=`sed -n "4p" "${file}" | sed -e "s/\t/^/g" | cut -d"^" -f 3 | sed -e "s/=//g" | sed -e 's/"//g'`
	echo "${newfile}"
	mv "${file}" "${newfile}.${md5}.xls"
done

