forked from XSCE/xsce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsc_tools
executable file
·355 lines (340 loc) · 12 KB
/
xsc_tools
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
#!/bin/bash
# tools to create, manage, simplify services and modules on XS server
PLUGINS_DIR=plugins.d
CHILD=`ls | grep plugins.d`
if [ ! $CHILD = $PLUGINS_DIR ]; then
echo "These school server developer tools must be run from $PLUGINS_DIR's parent"
exit 2
fi
CURDIR=`pwd`
DIRECTORY_MAKEFILE=$CURDIR/$PLUGINS_DIR/Makefile
#=================================================================================
#functions to make the big case statement more understandable
function generate_directories()
# first make a list of the manifest files for all plugins
{
rm -f $DIRECTORY_MAKEFILE
# scan through all the files in the plugin tree, and create the directories
# use an array to eliminate duplicates
for plugin in `find $PLUGINS_DIR/ -maxdepth 1 -type d | sed s:$PLUGINS_DIR/::`; do
cd plugins.d/$plugin
#echo "$PWD/$plugin.manifest"
if [ -f "./$plugin.manifest" ]; then
manlist+=" plugins.d/$plugin/$plugin.manifest"
# make list of files for later use
find ./files/ | sed s:^./files/:: > find
fi
cd $CURDIR
done
echo -e "install:\n" >>$MKDIR_MAKEFILE
# now feed all of these files, or connectors, to gawk to find unique path names
for manifest in $manlist; do
cat $manifest | \
gawk -v mkfile=$MKDIR_MAKEFILE -v man_file=$manifest '{
# get back the lookup array
while ((getline line < mkfile) > 0) {
path = substr(line,27)
directories[path] = ""
}
manifest_line = $0
# if it is a directory, terminate it with a slash
if (system("test -d " manifest_line) && ! match(manifest_line, /.*\/$/ )){
manifest_line = manifest_line "/"
}
for (i in nibbles) {delete nibble[i]}
#if(man_file == "plugins.d/vnc/vnc.manifest") {print("input from manifest", $0)}
num = split($0,nibbles,"/")
path = ""
for (i=1; i<num; i++){
if (nibbles[i] != ""){path = path nibbles[i] "/"}
if (!( path in directories)) {
write_directory(path, mkfile, directories)
directories[path] = ""
}
}
add_leaf_directories(nibbles, man_file, mkfile, directories)
}
function add_leaf_directories(connector, manifest_path, mkfile, directories){
match(manifest_path,/.*\//)
prefix = "./" substr(manifest_path,1,RLENGTH) "files/"
if (RLENGTH > 0){
find_file_data = sprintf("%s%s",substr(manifest_path,1,RLENGTH),"find")
while ((getline line < find_file_data) > 0) {
#print(line)
split(line, chunks, "/")
last_parent = connector[length(connector)]
if (last_parent == ""){last_parent = connector[length(connector) - 1]}
#if (manifest_path == "plugins.d/vnc/vnc.manifest")
#{print("root of files:",chunks[1],"parent:",last_parent)}
if (chunks[1] == last_parent && length(chunks) > 1){
full_path = "/"
candidate = prefix
for (i=1; i<length(connector)-1;i++) {
if (connector[i] != ""){full_path = full_path connector[i] "/"}
}
#for (j=1;j<length(chunks);j++) print (chunks[j])
for (i=1; i<=length(chunks); i++){
candidate = candidate chunks[i] "/"
#print(full_path " candidate:" candidate)
if (!system("test -d " candidate)){
# #if (chunks[i] != ""){full_path = full_path chunks[i] "/"}
full_path = full_path chunks[i] "/"
}
}
if (!( full_path in directories)) {
write_directory(full_path, mkfile, directories)
#print("writing:",full_path)
}
}
}
close(find_file_data)
}
}
function write_directory(path, mkfile, directories){
makef = sprintf("\tinstall -D -d \044(DESTDIR)%s",path)
cmd = sprintf("echo '\''%s'\'' >> %s", makef, mkfile)
#print(cmd " " path)
system(cmd)
directories[path]=""
}
'
done
echo -e "\tinstall -D -d \044(DESTDIR)usr/share/xs-config/plugins.d" >>$MKDIR_MAKEFILE
echo -e "\tcp -rp * \044(DESTDIR)usr/share/xs-config/plugins.d" >>$MKDIR_MAKEFILE
echo -e "\trm -rf \044(DESTDIR)usr/share/xs-config/plugins.d/*/files/" >>$MKDIR_MAKEFILE
}
#=================================================================================
function make_makefile_for_plugin()
{
cd $CURDIR
cd ./plugins.d/$1/
rm -f Makefile
echo -e "install:\n" >> Makefile
if [ -f "./$1.manifest" ]; then
manifest_list=`cat ./$1.manifest`
else
manifest_list=""
fi
for manifest_line in $manifest_list; do
#echo "manifest line is $manifest_line"
echo $manifest_line | \
gawk -v manifest_line=$manifest_line 'BEGIN {FS = "/"}
{
# create a fast lookup array
parent = $(NF)
if (parent == "") parent = $(NF-1)
connectors[parent]=manifest_line
#print("parent:" parent "manifest_line:" manifest_line)
# cycle through all the files
while ((getline line < "./find") > 0) {
#print(line)
num = split(line,chunks,"/")
if (chunks[1] in connectors) {
full_path = connectors[chunks[1]]
for(i=2;i<num;i++){
full_path = full_path chunks[i] "/"
}
#print("prefix:" connectors[chunks[1]] "loc:" line)
if (! system("test -f ./files/" line)){
#print("writing")
write_makefile(line,full_path,"Makefile")
}
}
}}
function write_makefile(source, full_path, mkfile ){
makef = sprintf("\tinstall -D files/%s \044(DESTDIR)%s", source, full_path)
cmd = sprintf("echo '\''%s'\'' >> %s", makef, mkfile)
#print(cmd " " path)
system(cmd)
}
'
done
#clean up the "find" file created by generate Makefiles
cd $CURDIR
rm -f ./plugins.d/$1/find
}
function dummy(){
#echo `cat $plugin.manifest `
if [ -f "$mod.manifest" ]; then
cat $mod.manifest | gawk -v pluginloc=$mod '{
ar[0] = ""
num = split($0,ar,"/")
path = ""
for (i=1; i<num; i++){
path = path ar[i] "/"
}
makef = sprintf(" install -D files/%s \044(DESTDIR)%s",ar[num],path )
cmd = sprintf("echo '\''%s'\'' >> ~/dextrose/xs-config/plugins.d/%s/Makefile",makef,pluginloc)
system(cmd)
#print(cmd)}'
fi
cd $curdir
}
#=================================================================================
function extract_directory_list_from_files()
# first get a list of connec
{
PLUGINS=`find $PLUGINS_DIR/ -maxdepth 1 -type d`
for PLGIN in $PLUGINS; do
ls -1 $PLGIN/files/
done
}
#=================================================================================
case "$1" in
"mktree")
# Read plugin name from 2nd parameter and create plugin tree
for mod in $2; do
mkdir -p plugins.d/$mod
pushd plugins.d/$mod
mkdir -p yum files
touch $mod.sh Makefile $mod.manifest $mod.README
chmod 755 $mod.sh
popd
done
;;
"yum")
# echo the yum modules in all the plugins
PLUGINDIR=/usr/share/xs-config/plugins.d
INSTALLTHESE=""
for mod in `ls $PLUGINDIR`; do
if [ -d "$PLUGINDIR/$mod" ];then
INSTALLTHESE=$INSTALLTHESE" "`ls -1 plugins.d/$mod/yum/`
fi
done
echo $INSTALLTHESE
;;
"functions")
# echo the function names (*.sh) in the plugin tree
curdir=`pwd`
for mod in `ls plugins.d`; do
cd plugins.d/$mod
echo `ls -1 *.sh`
cd $curdir
done
;;
"exec")
# source all the functions in the plugin tree
curdir=`pwd`
for mod in `ls plugins.d`; do
cd plugins.d/$mod
script=`ls -1 *.sh`
if [ ! -z $script ]; then
source "./$script"
fi
cd $curdir
done
;;
"savetree")
# copy the plugin tree to ../plugins.d.backup, or 2nd parameter
target=../plugins.d.backup
if [ $# = 1 ]; then
target=$1
fi
mkdir -p $target
cp -rp ./plugins.d/* $target
;;
"restoretree")
# copy the plugin tree from ../plugins.d.backup, or 2nd parameter to ./plugins.test
source=../plugins.d.backup
if [ $# = 1 -a -d $1 ]; then
source=$1
fi
mkdir -p ./plugins.test
cp -rp $source/* ./modules.test
;;
"git2linux")
# use make install to extract the current git branch to a tree
# to 2nd param or ../linuxtree if no 2nd param
target=../linuxtree
if [ $# = 1 ];then
target=../$1
fi
export DESTDIR=$target
mkdir -p $target
make -e install
;;
"mkmake")
# use contents of "plugins.d/files" to generate Makefiles in each plugin
MKDIR_MAKEFILE=$CURDIR/$PLUGINS_DIR/Makefile
if [ $# -eq 1 ]; then
echo -e "\nusage: ./xsc_tools mkmake ["all"|<plugin name>]\n"
exit 0
fi
if [ $# -eq 2 -a $2 = "all" ]; then
PLUGINS=`find $PLUGINS_DIR/ -maxdepth 1 -type d | sed s:$PLUGINS_DIR/::`
fi
if [ $# -eq 2 -a $2 != "all" ]; then
PLUGINS=$2
fi
generate_directories
#echo "directories done, now do makefile for plugin"
for plugin in $PLUGINS; do
#echo "$plugin"
make_makefile_for_plugin $plugin
done
;;
"tree2plugins.d")
# input is list of files (find BUILDROOT with plugin name added),
# param1=treeroot output is files copied to ./files, manifest written
# usage: cat <find / of root> | xsc_tools tree2plugins.d ../linuxtree
gawk 'BEGIN { RS = "\r" }
{
treeloc = "../linuxtree"
src = $1
tree = $1
sub(/\./,"",tree)
sub(/\./,"",src)
if ($2 != ""){
line = sprintf("cp %s%s plugins.d/%s/files/",treeloc,tree,$2)
print( line )
system(line)
addline = sprintf("echo '%s' >> plugins.d/%s/%s.manifest",tree,$2,$2)
print(addline)
system(addline)
}
}' $2
;;
"treediff")
diff -r . ~/dextrose/linuxtree
;;
"index")
PLUGINDIR="plugins.d"
FILELIST=
for mod in `ls -1 $PLUGINDIR`; do
#NEWLIST=`find "$PLUGINDIR/$mod/files/"`
NEWLIST=`find "$PLUGINDIR/$mod/files/" | gawk 'BEGIN {FS="/"} {print($4,$2);}'`
echo -e "$NEWLIST"
FILELIST="$FILELIST$NEWLIST"
done
echo `echo -e "$FILELIST" | sort`
;;
#PLUGINDIR="plugins.d"
# FILELIST=
#for mod in `ls -d $PLUGINDIR`; do
# echo $mod
#if [ -d "$PLUGINDIR/$mod" ] && [ -f "$PLUGINDIR/$mod/yumenabled" ]; then
# NEWLIST=`ls -1 "$PLUGINDIR/$mod/files/"`
# FILELIST="$FILELIST $NEWLIST"
# echo "$FILELIST"
#fi
#done
#echo $FILELIST
"help")
gawk ' /^\".*)$/{
print $0;
flag="true";
while (flag == "true"){
getline
if (substr($0,1,1) == "#"){
print $0;
}else{
flag="false";
}
}
}' $0
;;
*)
echo -e "\nThe following functions are available in xsc_tools"
cat $0 | grep -e "^\".*)$"
;;
esac