summaryrefslogtreecommitdiffstats
path: root/init.sh
blob: 094ca2d722e40947f60eba2539480cd4424620eb (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
# vim:set noet ts=4 sts=4:
# jpokorny@redhat.com

#
# internals
#

CLR_USR=34
CLR_SCR=32
CLR_BAD=31
CLR_WRN=33

RET_DEL=64

CHOICES=""

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

check_ret () {
	[ $1 -ne 0 -a $1 -ne $RET_DEL ] \
		&& do_announce "action failed with exit status $1" $CLR_BAD
	return $1
}

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

do_announce () {
	# usage: see announce ($1, $3) + [$2 = colour (defaults to 32/green)]
	# use colours if available
	[ "$3" = "delimit" ] && echo
	if test -t 1; then
		colour=$2
		if [ -z "$colour" ]; then
			colour=$CLR_USR
		fi
		echo -en "\\033[${colour}m"
		echo -n "$1"
		echo -e "\\033[0m"
	else
		echo "[[ $1 ]]"
	fi
}

#
# "API" for sourced choices
#

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

announce () {
	# usage: $1=message to output [, $2 = start with extra newline if "delimit")]
	do_announce "$1" $CLR_OK $2
}

require () {
	# usage: $1=what is required [, $2=test (function/command)]
	tester=$2
	if [ -z "$tester" ]; then
		tester=which
	fi
	$tester $1 >/dev/null
	ret=$?
	[ $ret -eq 0 ] \
		&& do_announce "require $1: checked ok" $CLR_OK \
		|| do_announce "require $1: not met" $CLR_BAD
	return $ret
}

do_git_submodule () {
	# usage: $1=action ($1 passed from main), $2..$N=submodule(s)
	ret=0
	git_prefix=$(git rev-parse --show-prefix)
	for submodule in "${@:2}"; do
		do_announce "$1 $submodule (git submodule)" $CLR_SCR delimit
		if [ "$1" == "get" ]; then
			pushd "$(git rev-parse --show-toplevel)" >/dev/null
			# TODO: --recursive seems to be buggy?
			git submodule update --init "${git_prefix}${submodule}" 2>&1
			ret=$?
			popd >/dev/null
			if [ $ret -eq 0 ]; then
				pushd $submodule >/dev/null
					git submodule update --init 2>&1
				popd >/dev/null
			fi
		else
			do_del "$submodule"
			ret=$?
		fi
		[ $ret -ne 0 ] && [ $ret -ne $RET_DEL ] && break
	done
	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
	do_announce "$1 $toplevel (wget)" $CLR_SCR delimit
	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
	do_announce "$1 $toplevel (svn)" $CLR_SCR delimit
	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 $?
}

do_post_get () {
	# usage: $1=action ($1 passed from main), $2..$N command + args
	[ "$1" != "get" ] || "${@:2}"
	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
				ret=\$?
				[ \$ret -ne 0 -a \$ret -ne $RET_DEL ] && return \$ret
				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 "$@"