Subversion Repositories stackman

Rev

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