Rev 4 | Go to most recent revision | Details | Compare with Previous | 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 |
||
5 | daniel-mar | 22 | echo "Terminal Stackman 2.4.1" |
23 | echo "(C) 2013-2020 ViaThinkSoft" |
||
3 | daniel-mar | 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 | |||
5 | daniel-mar | 93 | # No empty lines |
94 | if [[ "$inp" == "" ]]; then |
||
95 | continue |
||
96 | fi |
||
97 | |||
3 | daniel-mar | 98 | # Log to journal |
99 | echo "$inp" >> ~/.stam_history |
||
100 | if [ -f ~/.stam_history_mir ]; then |
||
101 | echo "$inp" >> ~/.stam_history_mir |
||
102 | fi |
||
4 | daniel-mar | 103 | "$DIR"/weblog_add "$inp" |
3 | daniel-mar | 104 | |
105 | # Split command and (single combined) argument |
||
106 | cmd=$( echo "$inp" | cut -d " " -f 1 ) |
||
107 | arg=$( echo "$inp" | cut -s -d " " -f 2- ) |
||
108 | |||
109 | # Commands begin with '-'. The first character can be escaped by using '--' |
||
110 | echo "$cmd" | grep -E "^-" > /dev/null |
||
111 | COMMANDMODE=$? |
||
112 | echo "$cmd" | grep -E "^--" > /dev/null |
||
113 | if [ $? -eq 0 ]; then |
||
114 | cmd="${cmd:1}" |
||
115 | # 1=nein, 0=ja |
||
116 | COMMANDMODE=1 |
||
117 | fi |
||
118 | |||
119 | # Category will be changed with '#'. The first character can be escaped by using '##' |
||
120 | echo "$cmd" | grep -E "^#" > /dev/null |
||
121 | CATCHANGE=$? |
||
122 | echo "$cmd" | grep -E "^##" > /dev/null |
||
123 | if [ $? -eq 0 ]; then |
||
124 | cmd="${cmd:1}" |
||
125 | # 1=nein, 0=ja |
||
126 | CATCHANGE=1 |
||
127 | fi |
||
128 | |||
129 | if [ $CATCHANGE -eq 0 ]; then |
||
130 | PREV_CAT="$CURCAT" |
||
131 | |||
132 | # Change category: "#<cat>" |
||
133 | cmd="-" |
||
134 | arg=$( echo "$inp" | sed 's/^#//' ) |
||
135 | |||
136 | # Do not allow empty category (reserved for '*') |
||
137 | if [[ "$arg" == "" ]]; then |
||
138 | # echo_red "[!] FATAL ERROR: Category cannot be empty!" |
||
139 | # continue |
||
140 | arg="$DEFAULTCAT" |
||
141 | fi |
||
142 | |||
143 | # change category |
||
144 | CURCAT="$arg" |
||
145 | |||
146 | # if [ $IS_BATCHMODE -eq 0 ]; then |
||
147 | # echo_green "Your category is now '$CURCAT'" |
||
148 | # fi |
||
149 | |||
150 | # Warning if category does not exist yet. |
||
151 | "$DIR"/exists1 "$CURCAT" |
||
152 | if [ $? -eq 1 ]; then |
||
153 | echo_yellow "(i) Note: The category does not exist and will be created if you continue:" |
||
154 | echo_yellow " $CURCAT" |
||
155 | fi |
||
156 | elif [ $COMMANDMODE -eq 0 ]; then |
||
157 | # Command mode: "-<cmd> [<arg>]" |
||
158 | cmd="${cmd:1}" |
||
159 | |||
160 | # Kommando verarbeiten |
||
161 | if [[ "$cmd" == "listcat" || "$cmd" == "ls" || "$cmd" == "lc" ]]; then |
||
162 | # List categories |
||
163 | "$DIR"/listcat "$arg" | less |
||
164 | elif [[ "$cmd" == "strike" || "$cmd" == "str" ]]; then |
||
165 | # Strike entry |
||
166 | if [[ "$arg" == "last" ]]; then |
||
167 | arg=$LAST_ID |
||
168 | fi |
||
169 | "$DIR"/str "$arg" |
||
170 | elif [[ "$cmd" == "unstrike" || "$cmd" == "unstr" ]]; then |
||
171 | # Un-Strike entry |
||
172 | if [[ "$arg" == "last" ]]; then |
||
173 | arg=$LAST_ID |
||
174 | fi |
||
175 | "$DIR"/unstr "$arg" |
||
176 | elif [[ "$cmd" == "singleadd" || "$cmd" == "sa" || "$cmd" == "as" || "$cmd" == "appendsingle" || "$cmd" == "aps" ]]; then |
||
177 | array=(${arg// / }) |
||
178 | CAT="${array[0]}" |
||
179 | TXT="${array[@]:1}" |
||
180 | |||
181 | # Warning if category does not exist yet. |
||
182 | "$DIR"/exists1 "$CAT" |
||
183 | if [ $? -eq 1 ]; then |
||
184 | echo_yellow "(i) Note: Category '$CAT' will be created." |
||
185 | fi |
||
186 | |||
187 | OUT=$( "$DIR"/aps "$CAT" "$TXT" ) |
||
188 | EC=$? |
||
189 | echo -n "$OUT" |
||
190 | if [ $EC -ne 0 ]; then |
||
191 | echo_red "[!] FATAL ERROR: Could not append the entry!" 1>&2 |
||
192 | else |
||
193 | LAST_ID=$( echo "$OUT" | sed -r 's/^.*OK! ([[:digit:]]+).*$/\1/g' ) |
||
194 | fi |
||
195 | elif [[ "$cmd" == "movefromid" || "$cmd" == "mfi" ]]; then |
||
196 | # Move ID $arg to category $CURCAT |
||
197 | if [[ "$arg" == "last" ]]; then |
||
198 | arg=$LAST_ID |
||
199 | fi |
||
200 | "$DIR"/id_move "$CURCAT" "$arg" |
||
201 | elif [[ "$cmd" == "movefromcat" || "$cmd" == "mfc" ]]; then |
||
202 | # Move CAT $arg to category $CURCAT |
||
203 | "$DIR"/cat_move "$CURCAT" "$arg" |
||
204 | elif [[ "$cmd" == "listent" || "$cmd" == "le" ]]; then |
||
205 | # List entries |
||
206 | "$DIR"/listent "$arg" | less |
||
207 | elif [[ "$cmd" == "listcurent" || "$cmd" == "lce" ]]; then |
||
208 | # List current entries |
||
209 | "$DIR"/listent "$CURCAT" | less |
||
210 | elif [[ "$cmd" == "clear" || "$cmd" == "cls" ]]; then |
||
211 | clear |
||
212 | elif [[ "$cmd" == "batch" || "$cmd" == "bat" ]]; then |
||
213 | TMPFIL=$( mktemp --suffix=.stam ) |
||
214 | # TODO: can the .nanorc file be enforced? |
||
215 | nano -Y stam -- "$TMPFIL" |
||
216 | if [ -f "$TMPFIL" ]; then |
||
217 | echo_cyan "Processing batch script..." |
||
218 | "$0" --batchmode < "$TMPFIL" |
||
219 | rm "$TMPFIL" |
||
220 | echo_cyan "Batch finished!" |
||
221 | else |
||
222 | echo_yellow "No data entered in batch mode editor. Cancelled." |
||
223 | fi |
||
224 | elif [[ "$cmd" == "sub" || "$cmd" == "su" ]]; then |
||
225 | # At the moment, we do not use '--subshell' |
||
226 | "$0" --subshell |
||
227 | elif [[ "$cmd" == "back" || "$cmd" == "b" ]]; then |
||
228 | X_PREV_CAT="$CURCAT" |
||
229 | |||
230 | # change category |
||
231 | CURCAT="$PREV_CAT" |
||
232 | |||
233 | # if [ $IS_BATCHMODE -eq 0 ]; then |
||
234 | # echo_green "Your category is now '$CURCAT'" |
||
235 | # fi |
||
236 | |||
237 | # Warning if category does not exist yet. |
||
238 | "$DIR"/exists1 "$CURCAT" |
||
239 | if [ $? -eq 1 ]; then |
||
240 | echo_yellow "(i) Note: The category does not exist and will be created if you continue:" |
||
241 | echo_yellow " $CURCAT" |
||
242 | fi |
||
243 | |||
244 | # Special ability: Allow that you can go forward again |
||
245 | PREV_CAT="$X_PREV_CAT" |
||
246 | elif [[ "$cmd" == "showhistory" || "$cmd" == "shi" ]]; then |
||
247 | # Leider enthält das nicht die Ausgaben :-( |
||
248 | cat ~/.stam_history | less |
||
249 | elif [[ "$cmd" == "help" || "$cmd" == "h" || "$cmd" == "?" ]]; then |
||
250 | echo_cyan "#<cat>" |
||
251 | echo -e "\tChange category. Parameter: Category" |
||
252 | echo -e "\tUse '##' at the beginning, if you want to create an entry beginning with '#'." |
||
253 | echo_cyan "-(back|b)" |
||
254 | echo -e "\tSwitch to previous used category" |
||
255 | echo_cyan "-(strike|str) <id>" |
||
256 | echo -e "\tStrike entry <id>" |
||
257 | echo -e "\t<id> can have ranges using '<min>-<max>' or be 'last' for the last inserted ID." |
||
258 | echo_cyan "-(singleadd|sa|as|appendsingle|aps) <cat> <entry>" |
||
259 | echo -e "\tAdd <entry> to category <cat> without changing the category." |
||
260 | echo -e "\t<cat> may not contain a white space" |
||
261 | echo_cyan "-(unstrike|unstr) <id>" |
||
262 | echo -e "\tUn-Strike entry <id>" |
||
263 | echo -e "\t<id> can have ranges using '<min>-<max>' or be 'last' for the last inserted ID." |
||
264 | echo_cyan "-(movefromid|mfi) <id>" |
||
265 | echo -e "\tMove ID <id> to the current selected category" |
||
266 | echo -e "\t<id> can have ranges using '<min>-<max>' or be 'last' for the last inserted ID." |
||
267 | echo_cyan "-(movefromcat|mfc) <cat>" |
||
268 | echo -e "\tMove all entries of category <cat> to the current selected category" |
||
269 | echo -e "\t<cat> can have wildcards (*)" |
||
270 | echo_cyan "-(listent|le) [<cat>]" |
||
271 | echo -e "\tShow all entries. Optional parameter: Category" |
||
272 | echo -e "\t<cat> can have wildcards (*)" |
||
273 | echo_cyan "-(listcurent|lce)" |
||
274 | echo -e "\tShow all entries of the selected category." |
||
275 | echo_cyan "-(listcat|ls|lc) [<cat>]" |
||
276 | echo -e "\tShow all categories. Optional parameter: Category" |
||
277 | echo -e "\t<cat> can have wildcards (*)" |
||
278 | echo_cyan "-(clear|cls)" |
||
279 | echo -e "\tClears the screen" |
||
280 | echo_cyan "-(batch|bat)" |
||
281 | echo -e "\tEnters batch mode (opens a subshell, without keeping your current category!)" |
||
282 | echo_cyan "-(sub|su)" |
||
283 | echo -e "\tEnters subshell mode" |
||
284 | echo_cyan "-(showhistory|shi)" |
||
285 | echo -e "\tShow history of all entered commands" |
||
286 | echo_cyan "-(help|h|?)" |
||
287 | echo -e "\tThis help screen" |
||
288 | echo_cyan "-(exit|quit|x|q)" |
||
289 | echo -e "\tExits the program" |
||
290 | elif [[ "$cmd" == "exit" || "$cmd" == "quit" || "$cmd" == "x" || "$cmd" == "q" ]]; then |
||
291 | # Exit the program |
||
292 | break |
||
293 | else |
||
294 | # Unknown command |
||
295 | echo_red "[!] FATAL ERROR: Unknown command '$cmd'. Use '-help' for available commands." 1>&2 |
||
296 | echo_red " If you want to add an entry, Use '--' to escape the first character." 1<&2 |
||
297 | fi |
||
298 | else |
||
299 | # (Blind) appender mode |
||
300 | |||
301 | # Add single entry (aps) |
||
302 | OUT=$( "$DIR"/aps "$CURCAT" "$inp" ) |
||
303 | EC=$? |
||
304 | echo -n "$OUT" |
||
305 | if [ $EC -ne 0 ]; then |
||
306 | echo_red "[!] FATAL ERROR: Could not append the entry!" 1>&2 |
||
307 | else |
||
308 | LAST_ID=$( echo "$OUT" | sed -r 's/^.*OK! ([[:digit:]]+).*$/\1/g' ) |
||
309 | fi |
||
310 | fi |
||
311 | done |
||
312 | |||
313 | if [ $IS_BATCHMODE -eq 0 ]; then |
||
314 | echo "" |
||
315 | echo "Goodbye." |
||
316 | fi |
||
317 |