This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrpg-sh-setup.sh
More file actions
472 lines (414 loc) · 14.1 KB
/
rpg-sh-setup.sh
File metadata and controls
472 lines (414 loc) · 14.1 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/bin/sh
# RPG shell utility library.
#
# This file is sourced by all `rpg-*` utilities. It handles environment
# setup and provides utility functions.
#
# A typical rpg program should look like this:
#
# #!/bin/sh
# # Launch a rocket missile or something like that.
# set -e
# . rpg-sh-setup
#
# ARGV="$@"
# USAGE '${PROGNAME} [-f] <missle>
# Launch a rocket missile.'
#
# # missile launching code
#
# That will handle `--help` usage messages and load the RPG environment.
# Guard against sourcing this file multiple times
test $__rpg_sh_setup_sourced && return 0
__rpg_sh_setup_sourced=true
# Constants
# ---------
# Useful BRE patterns for matching various gem stuffs.
GEMNAME_BRE='[0-9A-Za-z_.-]\{1,\}'
GEMVERS_BRE='[0-9][0-9.]*'
GEMPRES_BRE='[0-9A-Za-z][0-9A-Za-z.]*'
# This seems to be the most portable way of getting a variable with
# embedded newline. `$'\n'` is POSIX but doesn't work in my version of
# `dash(1)`. This works in `bash` and `dash` at least.
#
# The `ENEWLINE` is just an escaped version, useful for `sed` patterns.
NEWLINE='
'
ENEWLINE="\\$NEWLINE"
# Usage Messages, Logging, and Stuff Like That
# --------------------------------------------
# The program name used in usage messages, log output, and other places
# probably. You can set this before sourcing rpg-sh-setup to override
# the default `$(basename $0)` value but it's probably what you want.
: ${PROGNAME:=$(basename $0)}
# The progam's usage message. See the documentation for the `USAGE`
# function for information on setting this and how it plays with the
# other usage related functions.
: ${__USAGE__:='${PROGNAME} <args>'}
# This is the main usage setting thingy. Scripts should start as
# follows to take advantage of it:
#
# set -e # always
# . rpg-sh-setup # bring in support lib
#
# ARGV="$@"
# USAGE '${PROGNAME} <options> ...
# A short, preferably < 50 char description of the script.
#
# Options:
# -b Booooyaaahhh.'
#
# That will automatically trigger option parsing for a `--help`
# argument and whatnot.
#
# Note that the string passed in is single-quote escape. The string will
# evaluated at help time so you can do wild/expensive interpolations if
# that's your thing.
#
# One more quick usage tip. Some scripts want to show usage when `$@` is
# empty and others don't. These `USAGE` routines default to *not*
# showing the usage message when no arguments were passed. If you want
# to show usage when no arguments are passed, put this immediately
# before setting the `ARGV` variable:
#
# [ "$*" ] || set -- --help
# ARGV="$@"
# USAGE ...
#
# That'll cause empty arg invocations to show the help message.
USAGE () {
__USAGE__="${1:-$(cat)}"
case "$ARGV" in
*--h|*--he|*--hel|*--help*|*-h|*-\?*)
helpthem 0
exit 0;; # just in case
esac
}
# Show usage message defined in USAGE environment variable. The usage message
# is first evaluated as a string so interpolations can be performed if
# necessary.
helpthem () {
: ${REAL_USAGE:=$(eval "echo \"$__USAGE__\"")}
echo "Usage: $REAL_USAGE"
exit ${1:-2}
}
# Write a warning to stderr. The message is prefixed with the
# program's basename.
warn () { echo "$PROGNAME:" "$@" 1>&2; }
# Write an informationational message to stderr prefixed with the name
# of the current script. Don't use this, use `notice`.
heed () {
printf "%18s %s\n" "${PROGNAME#rpg-}:" "$*" |
sed 's/^\([^ ]\)/ \1/' 1>&2
}
# We rewite the `notice` function to `heed` if `RPGVERBOSE` is enabled
# after sourcing config files.
notice () { true; }
# Abort with a message and exit with failure.
die () { warn "$@"; exit 1; }
# Ruby Related Utility Functions
# ------------------------------
# Retrieve a rbconfig value.
rbconfig () { $RUBY -rrbconfig -e "puts RbConfig::CONFIG['$1']"; }
# The file extension for dynamic libraries on this operating system.
# e.g., `so` on Linux, `dylib` on MacOS.
ruby_dlext() { echo "$RUBYDLEXT"; }
# The command that should be executed to run `ruby`. This is used to
# rewrite shebang lines.
#
# TODO this is only used in rpg-build, which should be changed.
ruby_command () {
command -v ruby 2>/dev/null ||
echo "/usr/bin/env ruby"
}
# Misc Utility Functions
# ----------------------
# `readlink(1)` for systems that don't have it.
readlink () {
test -L "$1"
command readlink "$1" 2>/dev/null || {
_p=$(ls -l "$1")
echo ${_p##* -> }
}
}
# Alias `yes`, `no`, `1`, `0` to `true` and `false` so options can be
# set to any of those values.
yes () { true; }
no () { false; }
alias 1=true
alias 0=false
# Turn on the shell's built in tracing facilities if RPGTRACE is enabled.
rpg_init () {
${RPGTRACE:-false} && set -x
${RPGVERBOSE:-false} && {
notice () { heed "$@"; }
}
true
}
# This is replaced with the `config.sh` file that's generated when the
# `./configure` script is run. It includes a bunch of environment variables
# for program paths and defaults for the `RPGPATH`, `RPGBIN`, `RPGLIB`, etc.
# options.
: __RPGCONFIG__
# rpg's default installation and database locations are based on the
# currently active ruby environment. We use Ruby's `rbconfig` module to
# load the `bin`, `lib`, `man`, and `var` directories then set and export
# the `__RPGENV__` variable so that we only do this once per rpg
# process hierarchy.
#
# Any of the variables exported below may be used in `rpgrc` config files to
# determine the best locations for various RPG paths.
test -n "$__RPGENV__" && { rpg_init; return 0; }
PATH="${libexecdir}:$PATH"
RUBY="$(command -v ruby 2>/dev/null || echo "${RUBY:-ruby}")"
__RPGENV__="$RUBY"
eval "$(
$RUBY <<__RUBY__
require 'rbconfig'
conf = RbConfig::CONFIG
puts "
RUBYPREFIX='#{conf['prefix']}'
RUBYDLEXT='#{conf['DLEXT']}'
RUBYARCH='#{conf['arch']}'
RUBYSITEDIR='#{conf['sitelibdir']}'
RUBYVENDORDIR='#{conf['vendorlibdir']}'
RUBYMANDIR='#{conf['mandir']}'
RUBYBINDIR='#{conf['bindir']}'
RUBYSTATEDIR='#{conf['localstatedir']}'
RUBYVERSION='#{conf['ruby_version']}'
RUBYLIBDIR='#{File.dirname(conf['rubylibdir'])}'
"
__RUBY__
)"
# Determine if this is the MacOS Ruby framework
RUBYMACFRAMEWORK=false
expr -- "$RUBYPREFIX" : "/System/Library/Frameworks" >/dev/null && {
RUBYMACFRAMEWORK=true
RUBYLIBDIR=/usr/lib/ruby
RUBYSTATEDIR=$RUBYLIBDIR
RUBYSITEDIR=/usr/lib/ruby/site_ruby/$RUBYVERSION
RUBYVENDORDIR=/usr/lib/ruby/vendor_ruby/$RUBYVERSION
RUBYPREFIX=/usr
RUBYBINDIR=$RUBYPREFIX/bin
RUBYMANDIR=$RUBYPREFIX/share/man
}
export __RPGENV__ RUBY
export RUBYPREFIX RUBYDLEXT RUBYARCH RUBYSITEDIR RUBYVENDORDIR RUBYMANDIR RUBYBINDIR
export RUBYSTATEDIR RUBYLIBDIR RUBYVERSION RUBYMACFRAMEWORK
# With `configure --development`, set all paths to be inside a work dir.
if $develmode
then
: ${RPGPATH:="$prefix"}
: ${RPGLIB:="$RPGPATH/lib"}
: ${RPGMAN:="$RPGPATH/man"}
: ${RPGBIN:="$RPGPATH/bin"}
fi
# Configuration Files
# -------------------
# The system configuration file. Sourced near the end of this script. This
# is one of the variables that the configure script overrides.
: ${RPGSYSCONF:=/etc/rpgrc}
# The user configuration file. Sourced immediately after the system
# configuration file.
: ${RPGUSERCONF:=~/.rpgrc}
# Store current RPG environment settings
: oldrpgenv=$(env | grep '^RPG')
# Source the system `/etc/rpgrc` file.
test -r "$RPGSYSCONF" && { . "$RPGSYSCONF" || true; }
# Source the user `~/.rpgrc` file.
test -r "$RPGUSERCONF" && { . "$RPGUSERCONF" || true; }
# Restore previous RPG settings
eval "${oldrpgenv}"
# Install Paths
# -------------
# This is the psuedo root directory where `rpg` keeps all its stuff. The
# default locations of other rpg paths use this as a base. *HOWEVER*,
# no rpg utility targets this directory -- every significant location must
# have a separate path variable so that things stay flexible in
# configuration.
: ${RPGPATH:=${rpgdir:-$(
if $RUBYMACFRAMEWORK
then echo "/Library/Ruby/RPG/$RUBYVERSION"
else echo "${RUBYLIBDIR:-/var/lib}/rpg"
fi
)}}
# `RPGLIB` is the shared Ruby `lib` directory where library files are
# installed. It defaults to the currently active ruby's `vendor_ruby`
# directory (or `site_ruby` when Ruby < 1.8.7). If neither of those
# locations be determined for some reason, `RPGPATH/lib` is assumed.
if test -n "$rpgdir"
then
: ${RPGLIB:=$rpgdir/lib}
else
: ${RPGLIB:="${RUBYVENDORDIR:-${RUBYSITEDIR:-$RPGPATH/lib}}"}
fi
# `RPGBIN` is where executable scripts included in packages are installed.
# It defaults to the currently active ruby's `bindir` and falls back to
# `RPGPATH/bin` if no ruby `bindir` can be determined.
if test -n "$rpgdir"
then
: ${RPGBIN:=$rpgdir/bin}
else
: ${RPGBIN:="${RUBYBINDIR:-$RPGPATH/bin}"}
fi
# `RPGMAN` is where manpages included with packages are installed. This
# is basically the whole reason `rpg` was written in the first place.
if test -n "$rpgdir"
then
: ${RPGMAN:=$RPGPATH/man}
else
: ${RPGMAN:="${RUBYMANDIR:-$RPGPATH/man}"}
fi
# RPG Paths
# ---------
# `RPGCACHE` is where `rpg-fetch(1)` looks for and stores gem files.
# Set this to your Rubygems `cache` directory to share the gem cache.
: ${RPGCACHE:=$RPGPATH/cache}
# `RPGPACKS` is where `rpg-install(1)` unpacks gems before installing. The
# package directories are not used after package installation is complete.
: ${RPGPACKS:=$RPGPATH/packs}
# `RPGDB` is where the local package database is kept. It's a
# filesystem hierarchy. It looks like this:
#
# $ rpg-env sh -c 'cd $RPGDB && tree'
# RPGDB
# |-- bcrypt-ruby
# | |-- 2.1.2
# | | |-- authors
# | | |-- bindir
# | | |-- date
# | | |-- dependencies
# | | |-- description
# | | |-- email
# | | |-- executables
# | | |-- extensions
# | | |-- files
# | | |-- gemspec
# | | |-- homepage
# | | |-- manifest
# | | |-- name
# | | |-- platform
# | | |-- require_paths
# | | |-- summary
# | | |-- test_files
# | | `-- version
# | `-- active -> 2.1.2
# |-- do_sqlite3
# | |-- 0.10.1.1
# | | |-- authors
# | | |-- bindir
# | | |-- date
# | | |-- dependencies
# | | |-- description
# | | |-- email
# | | |-- executables
# | | |-- extensions
# | | |-- files
# | | |-- gemspec
# | | |-- homepage
# | | |-- manifest
# | | |-- name
# | | |-- platform
# | | |-- require_paths
# | | |-- summary
# | | |-- test_files
# | | `-- version
# | `-- active -> 0.10.1.1
# `-- sinatra
# |-- 0.9.6
# | |-- authors
# | |-- bindir
# | |-- date
# | |-- dependencies
# | |-- description
# | |-- email
# | |-- executables
# | |-- extensions
# | |-- files
# | |-- gemspec
# | |-- homepage
# | |-- manifest
# | |-- name
# | |-- platform
# | |-- require_paths
# | |-- summary
# | |-- test_files
# | `-- version
# |-- 1.0.b
# | |-- authors
# | |-- bindir
# | |-- date
# | |-- dependencies
# | |-- description
# | |-- email
# | |-- executables
# | |-- extensions
# | |-- files
# | |-- gemspec
# | |-- homepage
# | |-- manifest
# | |-- name
# | |-- platform
# | |-- require_paths
# | |-- summary
# | |-- test_files
# | `-- version
# `-- active -> 0.9.6
#
# The database is meant to be "stable". That is, you can write programs
# that rely on this structure. Maybe it should be documented first.
: ${RPGDB:=$RPGPATH/db}
# `RPGINDEX` is where the index of available gems is kept. It's a
# directory. The `rpg-sync(1)` program manages the files under it.
#
# * `release`:
# All available packages and all versions of all packages. Each line is a
# `<package> <version>` pair, separated by whitespace. The file is sorted
# alphabetically by package name, reverse by version number, such that the
# first line for a package is the most recent version.
#
# * `release-recent`:
# The most recent versions of all packages. The format is otherwise
# identical to the `release` file. This mostly exists so that
# `join(1)` can be used on it. Otherwise, we'd just build it from
# `release` when we needed it.
#
# * `prerelease`:
# **NOT YET IMPLEMENTED.**
# This is the same as `release` but includes only prelease packages.
#
# * `prerelease-recent`:
# **NOT YET IMPLEMENTED.**
# This is the same as `release-recent` but includes only prelease
# packages.
#
: ${RPGINDEX:=$RPGPATH/index}
# Path where installation session data is stored. The `rpg-prepare(1)` and
# `rpg-install(1)` programs store information about the packages being
# installed in subdirectories of this path.
: ${RPGSESSION:=$RPGPATH/session}
# Enable verbose logging to stderr.
: ${RPGVERBOSE:=false}
# Enable the shell's trace facility (`set -x`) in all rpg programs.
: ${RPGTRACE:=false}
# Show extconf.rb and make output when building extensions.
: ${RPGSHOWBUILD:=false}
# Default stale time for use with `rpg-sync -s`. Values can be stuff
# like `10 days` or `10d`, `30 minutes` or `30m`. A number with no time
# designator is considered in days. This value can also be `never`, in
# which case the database will never be automatically sync'd in the
# course of running other programs.
: ${RPGSTALETIME:=14 days}
# URL to the specs file used to build the package index.
: ${RPGSPECSURL:='http://rubygems.org/specs.4.8.gz'}
# URL to used to fetch gems.
: ${RPGGEMURL:='http://rubygems.org/downloads'}
# Export all RPG variables.
export RPGLIB RPGBIN RPGMAN
export RPGPATH RPGCACHE RPGPACKS RPGDB RPGINDEX RPGSESSION
export RPGTRACE RPGSHOWBUILD RPGSTALETIME RPGSPECSURL RPGGEMURL
export RPGSYSCONF RPGUSERCONF
# Setup logging and other stuff like that now that our variables are set.
rpg_init
# make sure we don't accidentally exit with a non-zero status
: