summaryrefslogtreecommitdiff
path: root/rvs/wrapper/rvs.sh
blob: bac92e5708f8c1c356f6ac279c467b160f98b92a (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
#!/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@"
export BINDIR="@BINDIR@"
   baseTMPDIR="@TMPDIR@"
export LIBDIR="@LIBDIR@"
export ETCDIR="@ETCDIR@"

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

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

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

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

_repo() {
	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"
}

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

_runcom() {
	usage="usage: $RVS runcom COMMAND"
	command=${1?"$usage"}; shift
	
	if [ ! -e "$BINDIR/$command" ]; then
		_error "cannot find command \`$command"
	else
		mkdir -p "$baseTMPDIR"
		export TMPDIR=`mktemp -d --tmpdir="$baseTMPDIR"`
		
		  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" $@ > "$TMPDIR/$command/$id"
			done
			cat "$TMPDIR/$command/$id"
		else
			_error "cannot run \`$command'"
		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() {
	REPO=`_repo 2> /dev/null`
	if [ -z "$REPO" ]; then
		export REPO="`pwd`/.$name"
		install -d "$REPO"
		_runcom init
	else
		_error "repository already exists at \`$REPO'"
	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 $@
			exit $?
		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