-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_prob.sh
More file actions
62 lines (52 loc) · 1.42 KB
/
make_prob.sh
File metadata and controls
62 lines (52 loc) · 1.42 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
#!/usr/bin/env bash
DIR=$(dirname $0)
search_up ()
(
while [[ $PWD != "/" ]]; do
if [[ -e "$1" ]]; then
pwd
if [[ ! -e "$1/$2" ]]; then
break
fi
fi
cd ..
done
)
TEMPLATE_DIR='.template'
PARENT_FILE='$PARENT'
IFS=$'\n'
TEMPLATE_DIRS=($(search_up "$TEMPLATE_DIR" "$PARENT_FILE" | tac))
unset IFS
TEMPLATE_DIRS=(${TEMPLATE_DIRS[@]/%/\/"$TEMPLATE_DIR"})
if hash rename.ul 2>/dev/null; then
RENAME=rename.ul
else
RENAME=rename
fi
for filepath in "$@"; do
PROBLEM_NAME=$(basename "$filepath")
if [[ -e "$filepath" ]]; then
echo "$filepath already exists. Remove it and retry."
continue
fi
# Copy files in
mkdir -p "$filepath"
for CURRENT_TEMPLATE_DIR in "${TEMPLATE_DIRS[@]}"; do
cp -r -T "$CURRENT_TEMPLATE_DIR" "$filepath/"
done
rm -f "$filepath/$PARENT_FILE"
# Rename PROBLEM_NAME in file names
find $filepath -type f -print0 | xargs -0 ${RENAME} "\$PROBLEM_NAME" "$PROBLEM_NAME"
# Envsubst PROBLEM_NAME in files
export PROBLEM_NAME
REPLACE_STRING='${PROBLEM_NAME}'
find $filepath -type f -print0 | xargs -0 -I{} bash -c\
'TEMP=$(mktemp) && cat "$1" > "$TEMP" && envsubst '"'$REPLACE_STRING'"' < "$TEMP" > "$1" && rm "$TEMP"'\
-- {}
pushd $filepath > /dev/null
if [[ -e "setup" ]]; then
echo "Running setup"
./setup
fi
popd > /dev/null
done