Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | daniel-mar | 1 | #!/bin/bash |
2 | |||
3 | # Get the directory of this script (also works with symlinks) |
||
4 | # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in |
||
5 | SOURCE="${BASH_SOURCE[0]}" |
||
6 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink |
||
7 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" |
||
8 | SOURCE="$(readlink "$SOURCE")" |
||
9 | [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located |
||
10 | done |
||
11 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" |
||
12 | |||
13 | # ToDo for Future: Implement getopt |
||
14 | if [[ "$1" == "--batchmode" ]]; then |
||
15 | IS_BATCHMODE=1 |
||
16 | else |
||
17 | IS_BATCHMODE=0 |
||
18 | fi |
||
19 | |||
20 | # Read in the system identifier |
||
21 | if [ $IS_BATCHMODE -eq 0 ]; then |
||
22 | echo "Terminal Stackman 2.3" |
||
23 | echo "(C) 2013-2017 ViaThinkSoft" |
||
24 | echo "" |
||
25 | |||
26 | if [ -f ~/".autorun" ]; then |
||
27 | echo "Starting autorun script..." |
||
28 | ~/".autorun" |
||
29 | echo "" |
||
30 | fi |
||
31 | |||
32 | sysid=$( "$DIR"/sysname ) |
||
33 | echo "System ID: lta:$sysid" |
||
34 | echo "" |
||
35 | echo "Enter '-help' for available commands." |
||
36 | echo "" |
||
37 | fi |
||
38 | |||
39 | # Check MySQL config |
||
40 | "$DIR"/check_db_conn -q |
||
41 | if [ $? -ne 0 ]; then |
||
42 | # TODO: fragen ob man write_stam_config aufrufen möchte |
||
43 | exit 1 |
||
44 | fi |
||
45 | |||
46 | DEFAULTCAT="." |
||
47 | |||
48 | CURCAT="$DEFAULTCAT" |
||
49 | PREV_CAT="$DEFAULTCAT" |
||
50 | LAST_ID=-1 |
||
51 | |||
52 | echo_yellow() { |
||
53 | echo -ne "\033[1;33;40m" |
||
54 | echo "$*" |
||
55 | echo -ne "\033[0m" |
||
56 | } |
||
57 | |||
58 | echo_red() { |
||
59 | echo -ne "\033[1;31;40m" |
||
60 | echo "$*" |
||
61 | echo -ne "\033[0m" |
||
62 | } |
||
63 | |||
64 | echo_cyan() { |
||
65 | echo -ne "\033[1;36;40m" |
||
66 | echo "$*" |
||
67 | echo -ne "\033[0m" |
||
68 | } |
||
69 | |||
70 | echo_green() { |
||
71 | echo -ne "\033[1;32;40m" |
||
72 | echo "$*" |
||
73 | echo -ne "\033[0m" |
||
74 | } |
||
75 | |||
76 | # Main menu |
||
77 | while (( 1 )); do |
||
78 | # Warning if category does not exist yet. |
||
79 | "$DIR"/exists1 "$CURCAT" |
||
80 | if [ $? -eq 1 ]; then |
||
81 | SIGN="*" |
||
82 | else |
||
83 | SIGN="+" |
||
84 | fi |
||
85 | |||
86 | # "-r" übernimmt alle "\". gut für z.B. "\\netbios\", schlecht für "\" am zeilenende, um weiterzuschreiben |
||
87 | read -erp "<$CURCAT> $SIGN " inp |
||
88 | if [[ $? -gt 0 ]]; then |
||
89 | # For example end of stream -- exit |
||
90 | break |
||
91 | fi |
||
92 | |||
93 | # Log to journal |
||
94 | echo "$inp" >> ~/.stam_history |
||
95 | if [ -f ~/.stam_history_mir ]; then |
||
96 | echo "$inp" >> ~/.stam_history_mir |
||
97 | fi |
||
98 | |||
99 | # Split command and (single combined) argument |
||
100 | cmd=$( echo "$inp" | cut -d " " -f 1 ) |
||
101 | arg=$( echo "$inp" | cut -s -d " " -f 2- ) |
||
102 | |||
103 | # Commands begin with '-'. The first character can be escaped by using '--' |
||
104 | echo "$cmd" | grep -E "^-" > /dev/null |
||
105 | COMMANDMODE=$? |
||
106 | echo "$cmd" | grep -E "^--" > /dev/null |
||
107 | if [ $? -eq 0 ]; then |
||
108 | cmd="${cmd:1}" |
||
109 | # 1=nein, 0=ja |
||
110 | COMMANDMODE=1 |
||
111 | fi |
||
112 | |||
113 | # Category will be changed with '#'. The first character can be escaped by using '##' |
||
114 | echo "$cmd" | grep -E "^#" > /dev/null |
||
115 | CATCHANGE=$? |
||
116 | echo "$cmd" | grep -E "^##" > /dev/null |
||
117 | if [ $? -eq 0 ]; then |
||
118 | cmd="${cmd:1}" |
||
119 | # 1=nein, 0=ja |
||
120 | CATCHANGE=1 |
||
121 | fi |
||
122 | |||
123 | if [ $CATCHANGE -eq 0 ]; then |
||
124 | PREV_CAT="$CURCAT" |
||
125 | |||
126 | # Change category: "#<cat>" |
||
127 | cmd="-" |
||
128 | arg=$( echo "$inp" | sed 's/^#//' ) |
||
129 | |||
130 | # Do not allow empty category (reserved for '*') |
||
131 | if [[ "$arg" == "" ]]; then |
||
132 | # echo_red "[!] FATAL ERROR: Category cannot be empty!" |
||
133 | # continue |
||
134 | arg="$DEFAULTCAT" |
||
135 | fi |
||
136 | |||
137 | # change category |
||
138 | CURCAT="$arg" |
||
139 | |||
140 | # if [ $IS_BATCHMODE -eq 0 ]; then |
||
141 | # echo_green "Your category is now '$CURCAT'" |
||
142 | # fi |
||
143 | |||
144 | # Warning if category does not exist yet. |
||
145 | "$DIR"/exists1 "$CURCAT" |
||
146 | if [ $? -eq 1 ]; then |
||
147 | echo_yellow "(i) Note: The category does not exist and will be created if you continue:" |
||
148 | echo_yellow " $CURCAT" |
||
149 | fi |
||
150 | elif [ $COMMANDMODE -eq 0 ]; then |
||
151 | # Command mode: "-<cmd> [<arg>]" |
||
152 | cmd="${cmd:1}" |
||
153 | |||
154 | # Kommando verarbeiten |
||
155 | if [[ "$cmd" == "listcat" || "$cmd" == "ls" || "$cmd" == "lc" ]]; then |
||
156 | # List categories |
||
157 | "$DIR"/listcat "$arg" | less |
||
158 | elif [[ "$cmd" == "strike" || "$cmd" == "str" ]]; then |
||
159 | # Strike entry |
||
160 | if [[ "$arg" == "last" ]]; then |
||
161 | arg=$LAST_ID |
||
162 | fi |
||
163 | "$DIR"/str "$arg" |
||
164 | elif [[ "$cmd" == "unstrike" || "$cmd" == "unstr" ]]; then |
||
165 | # Un-Strike entry |
||
166 | if [[ "$arg" == "last" ]]; then |
||
167 | arg=$LAST_ID |
||
168 | fi |
||
169 | "$DIR"/unstr "$arg" |
||
170 | elif [[ "$cmd" == "singleadd" || "$cmd" == "sa" || "$cmd" == "as" || "$cmd" == "appendsingle" || "$cmd" == "aps" ]]; then |
||
171 | array=(${arg// / }) |
||
172 | CAT="${array[0]}" |
||
173 | TXT="${array[@]:1}" |
||
174 | |||
175 | # Warning if category does not exist yet. |
||
176 | "$DIR"/exists1 "$CAT" |
||
177 | if [ $? -eq 1 ]; then |
||
178 | echo_yellow "(i) Note: Category '$CAT' will be created." |
||
179 | fi |
||
180 | |||
181 | OUT=$( "$DIR"/aps "$CAT" "$TXT" ) |
||
182 | EC=$? |
||
183 | echo -n "$OUT" |
||
184 | if [ $EC -ne 0 ]; then |
||
185 | echo_red "[!] FATAL ERROR: Could not append the entry!" 1>&2 |
||
186 | else |
||
187 | LAST_ID=$( echo "$OUT" | sed -r 's/^.*OK! ([[:digit:]]+).*$/\1/g' ) |
||
188 | fi |
||
189 | elif [[ "$cmd" == "movefromid" || "$cmd" == "mfi" ]]; then |
||
190 | # Move ID $arg to category $CURCAT |
||
191 | if [[ "$arg" == "last" ]]; then |
||
192 | arg=$LAST_ID |
||
193 | fi |
||
194 | "$DIR"/id_move "$CURCAT" "$arg" |
||
195 | elif [[ "$cmd" == "movefromcat" || "$cmd" == "mfc" ]]; then |
||
196 | # Move CAT $arg to category $CURCAT |
||
197 | "$DIR"/cat_move "$CURCAT" "$arg" |
||
198 | elif [[ "$cmd" == "listent" || "$cmd" == "le" ]]; then |
||
199 | # List entries |
||
200 | "$DIR"/listent "$arg" | less |
||
201 | elif [[ "$cmd" == "listcurent" || "$cmd" == "lce" ]]; then |
||
202 | # List current entries |
||
203 | "$DIR"/listent "$CURCAT" | less |
||
204 | elif [[ "$cmd" == "clear" || "$cmd" == "cls" ]]; then |
||
205 | clear |
||
206 | elif [[ "$cmd" == "batch" || "$cmd" == "bat" ]]; then |
||
207 | TMPFIL=$( mktemp --suffix=.stam ) |
||
208 | # TODO: can the .nanorc file be enforced? |
||
209 | nano -Y stam -- "$TMPFIL" |
||
210 | if [ -f "$TMPFIL" ]; then |
||
211 | echo_cyan "Processing batch script..." |
||
212 | "$0" --batchmode < "$TMPFIL" |
||
213 | rm "$TMPFIL" |
||
214 | echo_cyan "Batch finished!" |
||
215 | else |
||
216 | echo_yellow "No data entered in batch mode editor. Cancelled." |
||
217 | fi |
||
218 | elif [[ "$cmd" == "sub" || "$cmd" == "su" ]]; then |
||
219 | # At the moment, we do not use '--subshell' |
||
220 | "$0" --subshell |
||
221 | elif [[ "$cmd" == "back" || "$cmd" == "b" ]]; then |
||
222 | X_PREV_CAT="$CURCAT" |
||
223 | |||
224 | # change category |
||
225 | CURCAT="$PREV_CAT" |
||
226 | |||
227 | # if [ $IS_BATCHMODE -eq 0 ]; then |
||
228 | # echo_green "Your category is now '$CURCAT'" |
||
229 | # fi |
||
230 | |||
231 | # Warning if category does not exist yet. |
||
232 | "$DIR"/exists1 "$CURCAT" |
||
233 | if [ $? -eq 1 ]; then |
||
234 | echo_yellow "(i) Note: The category does not exist and will be created if you continue:" |
||
235 | echo_yellow " $CURCAT" |
||
236 | fi |
||
237 | |||
238 | # Special ability: Allow that you can go forward again |
||
239 | PREV_CAT="$X_PREV_CAT" |
||
240 | elif [[ "$cmd" == "showhistory" || "$cmd" == "shi" ]]; then |
||
241 | # Leider enthält das nicht die Ausgaben :-( |
||
242 | cat ~/.stam_history | less |
||
243 | elif [[ "$cmd" == "help" || "$cmd" == "h" || "$cmd" == "?" ]]; then |
||
244 | echo_cyan "#<cat>" |
||
245 | echo -e "\tChange category. Parameter: Category" |
||
246 | echo -e "\tUse '##' at the beginning, if you want to create an entry beginning with '#'." |
||
247 | echo_cyan "-(back|b)" |
||
248 | echo -e "\tSwitch to previous used category" |
||
249 | echo_cyan "-(strike|str) <id>" |
||
250 | echo -e "\tStrike entry <id>" |
||
251 | echo -e "\t<id> can have ranges using '<min>-<max>' or be 'last' for the last inserted ID." |
||
252 | echo_cyan "-(singleadd|sa|as|appendsingle|aps) <cat> <entry>" |
||
253 | echo -e "\tAdd <entry> to category <cat> without changing the category." |
||
254 | echo -e "\t<cat> may not contain a white space" |
||
255 | echo_cyan "-(unstrike|unstr) <id>" |
||
256 | echo -e "\tUn-Strike entry <id>" |
||
257 | echo -e "\t<id> can have ranges using '<min>-<max>' or be 'last' for the last inserted ID." |
||
258 | echo_cyan "-(movefromid|mfi) <id>" |
||
259 | echo -e "\tMove ID <id> to the current selected category" |
||
260 | echo -e "\t<id> can have ranges using '<min>-<max>' or be 'last' for the last inserted ID." |
||
261 | echo_cyan "-(movefromcat|mfc) <cat>" |
||
262 | echo -e "\tMove all entries of category <cat> to the current selected category" |
||
263 | echo -e "\t<cat> can have wildcards (*)" |
||
264 | echo_cyan "-(listent|le) [<cat>]" |
||
265 | echo -e "\tShow all entries. Optional parameter: Category" |
||
266 | echo -e "\t<cat> can have wildcards (*)" |
||
267 | echo_cyan "-(listcurent|lce)" |
||
268 | echo -e "\tShow all entries of the selected category." |
||
269 | echo_cyan "-(listcat|ls|lc) [<cat>]" |
||
270 | echo -e "\tShow all categories. Optional parameter: Category" |
||
271 | echo -e "\t<cat> can have wildcards (*)" |
||
272 | echo_cyan "-(clear|cls)" |
||
273 | echo -e "\tClears the screen" |
||
274 | echo_cyan "-(batch|bat)" |
||
275 | echo -e "\tEnters batch mode (opens a subshell, without keeping your current category!)" |
||
276 | echo_cyan "-(sub|su)" |
||
277 | echo -e "\tEnters subshell mode" |
||
278 | echo_cyan "-(showhistory|shi)" |
||
279 | echo -e "\tShow history of all entered commands" |
||
280 | echo_cyan "-(help|h|?)" |
||
281 | echo -e "\tThis help screen" |
||
282 | echo_cyan "-(exit|quit|x|q)" |
||
283 | echo -e "\tExits the program" |
||
284 | elif [[ "$cmd" == "exit" || "$cmd" == "quit" || "$cmd" == "x" || "$cmd" == "q" ]]; then |
||
285 | # Exit the program |
||
286 | break |
||
287 | else |
||
288 | # Unknown command |
||
289 | echo_red "[!] FATAL ERROR: Unknown command '$cmd'. Use '-help' for available commands." 1>&2 |
||
290 | echo_red " If you want to add an entry, Use '--' to escape the first character." 1<&2 |
||
291 | fi |
||
292 | else |
||
293 | # (Blind) appender mode |
||
294 | |||
295 | # Keine leeren Zeilen (ist das OK?) |
||
296 | if [[ "$inp" == "" ]]; then |
||
297 | continue |
||
298 | fi |
||
299 | |||
300 | # Add single entry (aps) |
||
301 | OUT=$( "$DIR"/aps "$CURCAT" "$inp" ) |
||
302 | EC=$? |
||
303 | echo -n "$OUT" |
||
304 | if [ $EC -ne 0 ]; then |
||
305 | echo_red "[!] FATAL ERROR: Could not append the entry!" 1>&2 |
||
306 | else |
||
307 | LAST_ID=$( echo "$OUT" | sed -r 's/^.*OK! ([[:digit:]]+).*$/\1/g' ) |
||
308 | fi |
||
309 | fi |
||
310 | done |
||
311 | |||
312 | if [ $IS_BATCHMODE -eq 0 ]; then |
||
313 | echo "" |
||
314 | echo "Goodbye." |
||
315 | fi |
||
316 |