summaryrefslogtreecommitdiffstats
path: root/init.sh
blob: 3780389692eae28aa1c3671d6b7e3c574c9952b8 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/bin/sh
# vim: noet:ts=8:sw=8
# jpokorny@redhat.com
# NOTE: not ready for arbitrary CWD, from . only
# NOTE: this is always to be run from within git repo so things like
#       "git rev-parse" will work (see do_git_submodule)
# NOTE: really stupid, no timestamping logic and similar fancy stuff
# TODO: new action "ahead" to check new versions (tags in git, etc.)

#
# internals
#

CLR_USR=32
CLR_SCR=36
CLR_BAD=31
CLR_WRN=33
CLR_INF=35
CLR_CMD=34

PRINTED=false
GIT_PREFIX=$(git rev-parse --show-prefix)

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 ] \
	  && do_announce "action failed with exit status $1" $CLR_BAD
	return $1
}

do_del () {
	do_announce "about to delete $*" $CLR_WRN
	do_announce "has to be confirmed (y)..." $CLR_INF
	rm -rfI -- "$@" 2>/dev/null
	[ $? -eq 0 ] || do_announce "something to be deleted not present" $CLR_WRN
}

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

do_action () {
	# usage: $1=action, $2=directory, $3=type-specific hook for get/refresh
	#        sources [ , $4=custom function for hooking-in ]

	# if two-phased and function for hooking-in is present
	hook=false
	if [ $# -ge 4 ] && type -t function_name $4 | grep -q "^function$"; then
		hook=true
	fi

	case "$1" in
	"get"|"refresh")
		# shortcut if target dir already exists or target
		# files were subsequently generated OK
		[ "$1" = "get" ] && $hook && $4 "probe" && continue
		$3 "$2"
		;;
	"clean")
		ret=0
		;;
	"purge")
		do_del "$submodule"
		ret=$?
		;;
	*)
		do_announce "Unexpected action $1" $CLR_BAD
		exit 1
		;;
	esac
	# repeat if two-phased and function for hooking-in is present
	$hook && $4 "$1"
	return $ret
}

#
# kind of API for sourced choices (functions starting with "init_")
#

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

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

init_verbosely () {
	do_announce "$*" $CLR_CMD
	$@
}

init_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_INF \
	  || do_announce "require $1: not met" $CLR_BAD
	return $ret
}

init_target () {
	# usage: $1=action ($1 passed from main or "probe"), $2..target dir
	#        [, $3=getter ]
	existing=$(find "$2" -mindepth 1 -name dir_info.txt -o -type l -prune \
	           -o -print 2>/dev/null)
	case "$1" in
	"probe"|"get"|"refresh")
		[ "$(echo "$existing" | wc -w)" -gt 0 ] && return
		mkdir -p "$2" 2>/dev/null
		if [ $# -gt 2 ]; then
			do_announce "about to $1 target files..." $CLR_INF
			$3
			ret=$?
			if [ $ret -ne 0 ]; then
				# recursion (should be safe)
				init_target purge "$2"
				return $ret
			fi
		fi
		;;
	"clean"|"purge")
		[ "$(echo "$existing" | wc -w)" -eq 0 ] || do_del $existing
		;;
	*)
		do_announce "Unexpected action $1" $CLR_BAD
		exit 1
		;;
	esac
}

init_git_submodule () {
	# usage: $1=action ($1 passed from main), $2..$N=submodule(s)
	#        + optionally, if last arg is a function, it is used
	#          for hooking-in
	get_refresh_hook () {
		pushd "$(git rev-parse --show-toplevel)" >/dev/null
		# TODO: --recursive seems to be buggy?
		do_announce "about to update git submodule ${1}..." $CLR_INF
		init_verbosely git submodule update --init "${GIT_PREFIX}${1}" 2>&1
		ret=$?
		popd >/dev/null
		if [ $ret -eq 0 ]; then
			pushd "$1" >/dev/null
			git submodule update --init 2>&1
			popd >/dev/null
		fi
		return $ret
	}

	arg_len=$#
	arg_last=${@:$arg_len}
	if type -t function_name $arg_last | grep -q "^function$"; then
		let arg_len-=2
	else
		let arg_len-=1
	fi

	ret=0
	for submodule in "${@:2:$arg_len}"; do
		do_announce "$1 $submodule (git submodule)" $CLR_SCR delimit
		do_action $1 "$submodule" get_refresh_hook $arg_last
		ret=$?
		[ $ret -ne 0 ] && break
	done
	check_ret $ret
}

# TODO: no support for custom function for hooking-in as with git_submodule
init_url_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
	other_params="${@:3}"

	get_refresh_hook () {
		wget --no-verbose --no-clobber --execute robots=off \
		     --directory-prefix "$toplevel" "$other_params" 2>&1

	}
	do_announce "$1 $toplevel (wget)" $CLR_SCR delimit
	do_action $1 "$toplevel" get_refresh_hook
	check_ret $?
}

# TODO: no support for custom function for hooking-in as with git_submodule
init_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
	svn="$3"
	rev=""
	if [ $# -ge 4 ]; then
		rev="--revision $4"
	fi

	get_refresh_hook () {
		svn checkout --force $rev "$svn" "$toplevel" | grep "revision"
	}

	do_announce "$1 $toplevel (svn)" $CLR_SCR delimit
	do_action $1 "$toplevel" get_refresh_hook
	check_ret $?
}

#
# hook-enablers
# usage: $1=action ($1 passed from main), $2..$N command + args
#

init_post_get ()     { [ "$1" != "get" ]     || "${@:2}"; check_ret $?; }
init_post_refresh () { [ "$1" != "refresh" ] || "${@:2}"; check_ret $?; }
init_post_clean ()   { [ "$1" != "clean" ]   || "${@:2}"; check_ret $?; }
init_post_purge ()   { [ "$1" != "purge" ]   || "${@:2}"; check_ret $?; }

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

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

ACTIONS="get|refresh|clean|purge"
help_actions () {
	cat <<"EOF"
quick explanation of actions:
  - basically, get-refresh and clean-purge are very similar (or even
    aliasing in some cases) depending on whether the way to get the target
    files is single-phase (what you fetch is what you use) or two-phased
    (what you fetch is what you use to generate target files you then use)

  * get/refresh: get the target files
    - single-phased (get=refresh): refetch sources (optionally: if needed)
    - two-phased:
      - get:     if not present, generate the target files from sources, but
                 if sources are missing as well, fetch them first (=refresh)
      - refresh: remove existing target files, refetch sources (optionally:
                 if needed) and generate fresh target files out of them

  * clean/purge: remove the files
    - single-phased:
      - clean:   NOOP (unless something performed via hook)
      - purge:   remove the source/target files directory
    - two-phased:
      - clean:   remove the target files directory
      - purge:   remove both the target files and source files directories
 
  NOTE: you can create composite actions on the commandline, e.g.:
        ./init.sh purge all && ./init.sh all  # completely from scratch
EOF
}

main () {
	action="get"  # mostly, existing target files will be just reused
	eval "case \"$1\" in
	$ACTIONS)
		action=\"$1\"
		[ $# -gt 1 ] && shift # || fall-through via unrecognized choice
		;;
	esac"
	while [ -n "$1" ]; do
		eval "case \"$1\" in
		$CHOICES)
			$1 $action
			ret=\$?
			[ \$ret -ne 0 ] && return \$ret
			shift
			continue;;
		all)
			main $action $(echo $CHOICES | sed 's/|/ /g')
			break;;
		help|*)
			echo \"usage: ./init.sh [$ACTIONS] ($CHOICES|all)+\"
			shift
			if [ \"\$1\" != \"actions\" ]; then
				echo \"see also 'help actions' for details about actions\"
			else
				echo
				help_actions
			fi
			break;;
		esac"
	done
}

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