forked from ioi-2017/tps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tps.sh
165 lines (123 loc) · 3.78 KB
/
tps.sh
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
#!/bin/bash
# A supplementary command as a tool used in TPS repositories
# Kian Mirjalali, Hamed Saleh, MohammadReza Maleki
# IOI 2017, Iran
tps_version=1.1
set -e
function errcho {
>&2 echo "$@"
}
__tps_target_file__="problem.json"
#looking for ${__tps_target_file__} in current and parent directories...
__tps_curr__="$PWD"
while [ "${__tps_curr__}" != "${__tps_prev__}" ] ; do
if [ -f "${__tps_curr__}/${__tps_target_file__}" ] ; then
BASE_DIR="${__tps_curr__}"
break
fi
__tps_prev__="${__tps_curr__}"
__tps_curr__="$(dirname "${__tps_curr__}")"
done
__tps_scripts__="scripts"
__tps_scripts_dir__="${BASE_DIR}/${__tps_scripts__}"
function __tps_list_commands__ {
ls -a -1 "${__tps_scripts_dir__}" 2>/dev/null | grep -E ".\\.(sh|py|exe)$" | while read f; do echo ${f%.*} ; done
}
function __tps_unify_elements__ {
_sort=$(which -a "sort" | grep -iv "windows" | head -1)
if [ -z "${_sort}" ] ; then
_sort="cat"
fi
${_sort} | uniq
}
function __tps_help__ {
echo "TPS version ${tps_version}"
echo ""
echo "Usage: tps <command> <arguments>..."
echo ""
if [ -z "${BASE_DIR+x}" ]; then
echo "Currently not in a TPS repository ('${__tps_target_file__}' not found in any of the parent directories)."
elif [ ! -d "${__tps_scripts_dir__}" ] ; then
echo "Directory '${__tps_scripts__}' is not available."
elif [ -z "$(__tps_list_commands__)" ] ; then
echo "No commands available in '${__tps_scripts__}'."
else
echo "Available commands:"
__tps_list_commands__ | __tps_unify_elements__
fi
exit 1
}
[ $# -gt 0 ] || __tps_help__
__tps_command__="$1"; shift
if [ "${__tps_command__}" == "--bash-completion" ] ; then
[ $# -gt 1 ] || exit 0
[ ! -z "${BASE_DIR+x}" -a -d "${__tps_scripts_dir__}" ] || exit 0
index="$1"; shift
[ ${index} -gt 0 ] || exit 0
cursor_location="$1"; shift
[ ${cursor_location} -ge 0 ] || exit 0
# removing 'tps'
shift
cur="${!index}"
cur="${cur:0:${cursor_location}}"
if [ ${index} -eq 1 ]; then
opts="$(__tps_list_commands__)"
compgen -W "${opts}" -- "${cur}" | {
while read -r tmp; do
printf '%s \n' "$tmp"
done
}
exit 0
fi
command="$1"; shift
command_bc_options_file="${__tps_scripts_dir__}/bash_completion/${command}.options"
if [ -f "${command_bc_options_file}" ] && [[ ${cur} == --* ]]; then
if ! [[ ${cur} == *=* ]]; then
compgen -W "$(cat "${command_bc_options_file}")" -- "${cur}" | {
while read -r tmp; do
if [[ ${tmp} != *= ]]; then
printf '%s \n' "$tmp"
else
printf '%s\n' "$tmp"
fi
done
}
exit 0
else
value="${cur#*=}"
compgen -f -- "${value}"
exit 0
fi
fi
compgen -f -- "${cur}"
exit 0
fi
if [ -z "${BASE_DIR+x}" ]; then
errcho "Error: Not in a TPS repository ('${__tps_target_file__}' not found in any of the parent directories)"
exit 2
fi
export BASE_DIR
#keeping the lower-case variable 'base_dir' for backward compatibility.
export base_dir="${BASE_DIR}"
if [ ! -d "${__tps_scripts_dir__}" ] ; then
errcho "Error: Directory '${__tps_scripts__}' not found."
exit 2
fi
__tps_init__="${__tps_scripts__}/internal/tps_init.sh"
__tps_init_file__="${BASE_DIR}/${__tps_init__}"
if [ ! -f "${__tps_init_file__}" ] ; then
errcho "Error: File '${__tps_init__}' not found."
exit 2
fi
source "${__tps_init_file__}"
if [ -f "${__tps_scripts_dir__}/${__tps_command__}.sh" ]; then
bash "${__tps_scripts_dir__}/${__tps_command__}.sh" "$@"
elif [ -f "${__tps_scripts_dir__}/${__tps_command__}.py" ]; then
python "${__tps_scripts_dir__}/${__tps_command__}.py" "$@"
elif [ -f "${__tps_scripts_dir__}/${__tps_command__}.exe" ]; then
"${__tps_scripts_dir__}/${__tps_command__}.exe" "$@"
else
errcho "Error: command '${__tps_command__}' not found in '${__tps_scripts__}'".
errcho "Searched for '${__tps_command__}.sh', '${__tps_command__}.py', '${__tps_command__}.exe'."
exit 2
fi