summaryrefslogtreecommitdiff
path: root/rvs/wrapper/rvs.sh
blob: 101268ffeff606f090256d5741d18901fd8eb333 (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
#!/bin/sh
name='@name@'
ver='0.9'
#   Copyright (C) 2009-2010 Luke Shumaker
#   
#   This file is part of rvs.
#   
#   rvs is free software; you can redistribute it and/or modify it under the
#   terms of the GNU General Public License as published by the Free Software
#   Foundation; either version 2, or (at your option) any later version.
#   
#   rvs is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#   See the GNU General Public License for more details.
#   
#  You should have received a copy of the GNU General Public License
#  along with this program;  see the file COPYING.
#  If not, see <http://www.gnu.org/licenses>.

export RVS="$0" #RVS='@RVS@'
       BINDIR='@BINDIR@'
   baseTMPDIR='@TMPDIR@'
export LIBDIR='@LIBDIR@'
export ETCDIR='@ETCDIR@'

################################################################################
# Internal use                                                                 #
################################################################################

# General ############################################################

_error() {
	echo "$RVS: $1" >> /dev/stderr
	exit ${2-1}
}

# Repository #########################################################

_repo() {
	if [ -z "$REPO" ]; then # we aren't getting a value from then env
		repo=".$name"
		owd="`pwd`" # old working directory
		
		#     [----can ascend-----] && [-haven't found repo-]
		while [ "$pwd" != "`pwd`" ] && [ ! -e "`pwd`/$repo" ]; do
			pwd=`pwd`
			cd ..
		done
		
		if [ -e "`pwd`/$repo" ]; then
			# we found a repository
			echo "`pwd`/$repo"
		else 
			# we didn't find a repository
			_error "no $name repository found"
		fi
		cd "$owd"
	else
		echo "$REPO"
	fi
}

# Modules ############################################################

_runcom() {
	# exit states
	#  0 - fine
	#  1 - could not find command
	#  2 - could not run  command
	usage="usage: $RVS runcom COMMAND"
	command=${1?"$usage"}; shift
	
	if [ ! -e "$BINDIR/$command" ]; then
		exit 1
	else
		mkdir -p "$baseTMPDIR"
		export TMPDIR="`mktemp -d --tmpdir="$baseTMPDIR"`"
		
		if [ -z "$RVS_LEVEL" ]; then
			export RVS_LEVEL=0;
		else
			export RVS_LEVEL=`expr $RVS_LEVEL + 1`
		fi
		
		  if [ -f "$BINDIR/$command" ]; then
			exec "$BINDIR/$command" $@ | tee "$TMPDIR/$command"
		elif [ -d "$BINDIR/$command" ]; then
			mkdir -p "$TMPDIR/$command"
			for file in "$BINDIR/$command"/*; do
				id="`basename "$file" | sed 's/^[0-9]*-//'`"
				"$file" $@ | tee "$TMPDIR/$command/$id"
			done
		else
			exit 2
		fi
		rm -rf "$TMPDIR"
	fi
}

_nextpriority() {
	usage="usage: $RVS nextpriority COMMAND"
	command=${1?"$usage"}
	for file in "$BINDIR/$command"/*; do
		echo "$file" | sed 's/^\([0-9]*\)-.*/\1/'
	done | sort -n | tail -n1 | xargs expr 1 + 
}

################################################################################
# Builtin commands                                                             #
################################################################################

_init() {
	# exit states:
	#  0 - fine
	#  1 - repo already exists
	#  2 - error running init commands
	REPO="`_repo 2> /dev/null`"
	if [ -z "$REPO" ]; then
		export REPO="`pwd`/.$name"
		install -d "$REPO"
		_runcom init
		case "$?" in
			0)                                exit 0;;
			1)                                exit 0;;
			2) _error 'error running init scripts' 2;;
		esac
	else
		_error "repository already exists at \`$REPO'" 1
	fi
}

################################################################################
# Builtin commands -- installing modules                                       #
################################################################################

_install() {
	usage="usage: $RVS install FILE COMMAND ID [PRIORITY]"
	    file=${1?"$usage"}
	 command=${2?"$usage"}
	      id=${3?"$usage"}
	priority=${4-"`_nextpriority "$command"`"}
	
	install -d "$BINDIR/$command"
	install "$file" "$BINDIR/$command/$priority-$id"
}

_uninstall() {
	usage="usage: $RVS uninstall COMMAND [ID]"
	command=${1?"$usage"}
	id=$2
	
	if [ -z "$id" ]; then
		# no ID specified
		rm "$BINDIR/$command"
	else
		# ID specified
		rm "$BINDIR/$command/"*-"$id"
		if [ -z "$(ls "$BINDIR/$command")" ]; then 
			rmdir "$BINDIR/$command"
		fi
	fi
}

################################################################################
# Main                                                                         #
################################################################################

# START OPTION HANDLING #
com=$1;
# END   OPTION HANDLING #
case "$com" in
	'') _error 'no command specified';;
	repo) _repo; exit $?;;
	'init'     ) shift;      _init $@; exit $?;;
	  'install') shift;   _install $@; exit $?;;
	'uninstall') shift; _uninstall $@; exit $?;;
	*)
		REPO=`_repo`
		if [ "$?" = '0' ]; then
			export REPO
			_runcom $@
			case "$?" in
				0)                                  exit 0;;
				1) _error "could not find command \`$1'" 1;;
				2) _error "could not run  command \`$1'" 2;;
			esac
		else
			exit 1
		fi
	:;;
esac

# Copy/Paste Virus 1.3c Please copy and paste this text anywhere. Track
# its progress by searching for this MD5#f7eac285ebfe21c4587bfebb9582f90d