summaryrefslogtreecommitdiff
path: root/scripts/rename-xml.sh
blob: e82742d9c9f99a2bc449cda059a17ca1e5ae291d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#
# Fetch Service Provider metadata and save into entityid filename
#

error()
{
	echo "Error: $*" 1>&2
	exit 1
}

entityID=$1
if [ -z "$entityID" ] ; then
	cat <<EOF
Usage: `basename $0` <entityID>
Ex:    `basename $0` https://shibsp.mysite.com/swamidproxy/gitlab.xml
EOF
	exit 1
fi

if [ `uname -s` == "Darwin" ]; then
	SEDI="sed -i ''"
else
	SEDI="sed -i"
fi

script_cwd=`dirname "$0"`
if test -d swamid-2.0 ; then
	echo "Moving into swamid-2.0/"
	cd swamid-2.0
	echo "$script_cwd" | grep -q ^/ || script_cwd=../$script_cwd
fi

if echo "$entityID" | grep -qE '^http://|^https://' ; then
	metadata=`grep -il "entityID.*$entityID" *`
else
	echo "Missing http(s) in entityID"
fi
[ -n "$metadata" ] || error "Failed to fetch metadata from $metadataurl"

entityid=`cat "$metadata" | sed -n 's/.*entityID=['\''"]\([^"]*\)['\''"].*/\1/p'`
[ -n "$entityid" ] || error "Failed to find entityID in metadata"
[ `echo "$entityid" | wc -l` = 1 ] || error "Multiple entityid:s found: `echo $entityid`"

entityidfn=`echo "$entityid" | sed 's;https*://;;' | sed 's/[^a-zA-Z0-9_.-]/-/g' | sed 's/$/.xml/'`
[ -n "$entityidfn" ] || error "Failed to generate filename from entityid $entityid"

[ -r "$entityidfn" ] && new=false || new=true
if $new ; then
	echo -n "Move $metadata into $entityidfn [Y/n]? "
else
	echo "$entityidfn and $metadata are the same"
	exit
fi
read x
case $x in
	Y|y|"")
		grep -l "$metadata\"" ../*.mxml | while read file; do
			$SEDI "s;href=\"swamid-2.0/$metadata\";href=\"swamid-2.0/$entityidfn\";" $file
		done
		git mv $metadata $entityidfn

		;;
	*)
		echo "Nothing done"
		;;
esac