summaryrefslogtreecommitdiffstats
path: root/init.sh
blob: c5cc4a1a63b7f546270c91d3a9fa248e04334341 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
# vim:set noet ts=4 sts=4:
# jpokorny@redhat.com

#
# internals
#

CHOICES=""

check_nargs () {
	[ $1 -ge $2 ]
	ret=$?
	[ $ret -eq 0 ] || announce "action failed (expected $1+ args, got $2)"
	return $ret
}

check_ret () {
	[ $1 -ne 0 ] && announce "action failed with exit status $1"
	return $1
}

do_del () {
	#echo "delete $1?"
	[ -d "$1" ] && rm -rfI -- "$1" || echo "$1 not present"
}

#
# "API" for sourced choices
#

register () {
	# usage: $1=choice to register
    CHOICES="$CHOICES|$1"
}

announce () {
	# usage: $1=message to output
	# use colours if available
	echo
	if test -t 1; then
		echo -en "\\033[32m"
		echo -n "$1"
		echo -e "\\033[0m"
	else
		echo "[[ $1 ]]"
	fi
}

do_git_submodule () {
	# usage: $1=action ($1 passed from main), $2..$N=submodule(s)
	ret=0
	# TODO: pushd $GIT_ROOT
	for submodule in "${@:2}"; do
		announce "$1 $submodule (git submodule)"
		if [ "$1" == "get" ]; then
			# TODO: avoid this workaround
			if [ ! -f .gitmodules ]; then
				submodule="$SUBMODULES_DIR/$submodule"
			fi
			git submodule update --init "$submodule" 2>&1
		else
			do_del "$submodule"
		fi
		ret=$?
		[ $ret -ne 0 ] && break
	done
	popd
	check_ret $ret
}

do_wget () {
	# usage: $1=action ($1 passed from main), $2=DIR, $3..$N=arguments (to wget)
	check_nargs $# 3 || return $?
	toplevel="$2"
	if [ "$(dirname $2)" != "." ]; then
		toplevel="$(dirname $2)"
	fi
	announce "$1 $toplevel (wget)"
	if [ "$1" == "get" ]; then
		wget --no-verbose --no-clobber --execute robots=off \
		     --directory-prefix "$2" "${@:3}" 2>&1
	else
		do_del "$toplevel"
	fi
	check_ret $?
}

do_svn () {
	# usage: $1=action ($1 passed from main), $2=DIR, $3=SVN [, $4=REV]
	check_nargs $# 3 || return $?
	toplevel="$2"
	if [ "$(dirname $2)" != "." ]; then
		toplevel="$(dirname $2)"
	fi
	announce "$1 $toplevel (svn)"
	if [ "$1" == "get" ]; then
		rev=""
		if [ $# -ge 4 ]; then
			rev="--revision $4"
		fi
		svn checkout --force $rev "$3" "$2" | grep "revision"
	else
		do_del "$toplevel"
	fi
	check_ret $?
}

#
# source the choices (./init-* files) and go
#

for choice in $(ls init-*); do [ -f $choice ] && source ./$choice; done
CHOICES=$(echo $CHOICES | sed 's/^|//')

main () {
	action="get"
	if [ "$1" == "get" -o "$1" == "del" ]; then
		action="$1"
		[ $# -gt 1 ] && shift # || fall-through via unrecognized choice
	fi
	while [ -n "$1" ]; do
		eval "case \"$1\" in
			$CHOICES)
				$1 $action || exit $?
				shift
				continue;;
			all)
				main $action $(echo $CHOICES | sed 's/|/ /g')
				break;;
			help|*)
				echo \"usage: ./init.sh [del] ($CHOICES|all)+\"
				break;;
		esac"
	done
}

[ $# -eq 0 ] && main help || main "$@"