forked from cloudmesh-project/sp17-i524
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup
executable file
·214 lines (165 loc) · 4.53 KB
/
setup
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
#!/usr/bin/env bash -x
set -o errexit # exit on failure
declare DEST # one of paper1, paper2, project
declare USER_ID # the homework ID or project ID
declare OUTDIR # the directory that will be created.
################################################################ Usage info
usage() {
cat <<EOF
USAGE:
./setup <TYPE> <ID>
This script generates the scaffolding for working on the paper or
projects. Input parameters:
1) TYPE: one of "paper1", "paper2", "galaxy", or "project"
2) ID: this is your HID for the papers or PID for the project
EXAMPLES:
1) Setup for paper 1 using my HID:
./setup paper1 S17-EX-0000
2) Setup for the project using my PID
./setup project S17-P-0000
3) Setup for the "galaxy" assignment using my PID
./setup galaxy S17-EX-0000
EOF
}
################################################################ Argument checking
# Fail if bad number of arguments
case $# in
2);;
*)
echo "ERROR: invalid invocation: $0 $@"
echo
usage
exit 1
;;
esac
################ check arguments
DEST="$1"
USER_ID="$2"
# sanity check the DEST and declare TYPE
case $DEST in
paper[1-2])
declare -r TYPE=paper
;;
galaxy)
declare -r TYPE=galaxy
;;
project)
declare -r TYPE=project
;;
*)
echo "ERROR: Unsupported type $DEST"
echo
usage
exit 2
;;
esac
################ sanity check that the USER_ID is correct
case $TYPE in
paper)
case $USER_ID in
S17-??-????);;
*)
declare -r TYPE_MISMATCH_ERROR=0;;
esac
esac
if [ $TYPE_MISMATCH_ERROR ]; then
echo "ERROR: User ID '$USER_ID' is not supported for type $TYPE."
echo "ERROR: Check your ID"
echo
usage
exit 3;
fi
################ ensure that no previous work has been done already
OUTDIR=$DEST/$USER_ID
if [ -d $OUTDIR ]; then
echo "ERROR: Destination already exists: $OUTDIR"
echo "Make sure you specified TYPE correctly. You used: $DEST"
exit 4
fi
################################################################
## cross platform support
mymktemp() {
## `mktemp` works differently on linux and os x
## The REAL issue is that Mac comes with BSD mktemp while Linux has GNU mktemp
## These each support different flags.
##
## While checking with `uname` is not really the best option, it has caught most cases.
##
## Tested on
## [x] macOS 10.12
## [x] Linux
## [ ] Windows with Cygwin
## [ ] Windows with Ubuntu
case $(uname) in
Linux|Cygwin)
mktemp $@;;
Darwin)
mktemp -t 'tmp' $@;;
*)
echo "ERROR: unsupported platform $(uname)"
echo "Please file a bug at https://github.com/cloudmesh/sp17-i524/issues"
exit 3
;;
esac
}
################################################################
declare -r TMPDIR=$(mymktemp -d)
cleanup() {
rm -rf "$TMPDIR"
}
trap cleanup EXIT
find_dir_contents() {
local -r path="$1"
shift
find "$path" -maxdepth 1 ! -path "$path" $@
}
git-clone-directory() {
# Retrieve a specific directory from a git repository
# NOTE: this strips the history
local -r url="$1"
local -r dir="$2"
local -r dst="$3"
git clone --depth 1 --recursive "$url" "$dst"
cd "$dst"
## the pipe into read is due to `mv` on macOS not supporting `-t`
find_dir_contents "$dir" -print0 | xargs -0 -n 1 | while read path; do mv "$path" "$TMPDIR"; done
find_dir_contents . -print0 | xargs -0 rm -rf
find_dir_contents "$TMPDIR" -print0 | xargs -0 -n 1 | while read path; do mv "$path" "$PWD"; done
rm -f *.docx
}
do_paper() {
declare -r CLASS=git://github.com/cloudmesh/classes.git
declare -r DIRECTORY=docs/source/format/report
git-clone-directory $CLASS ./$DIRECTORY $OUTDIR
CLONEDIR=`pwd`
rm -rf $CLONEDIR/styles
echo "DONE: $OUTDIR"
}
do_galaxy() {
mkdir -p "$OUTDIR"
}
do_project() {
mkdir -p $OUTDIR
mkdir -p $OUTDIR/code
# touch $OUTDIR/code/.gitkeep$OUTDIR/styles
mkdir -p $OUTDIR/report
touch $OUTDIR/code/.gitkeep
declare -r OUTDIR=$OUTDIR/report
do_paper
mv README.rst ../
}
case $TYPE in
paper*)
do_paper
;;
galaxy)
do_galaxy
;;
project)
do_project
;;
*)
echo "ERROR: the impossible happened"
exit 4
esac
echo "Submit your work in $OUTDIR"