aboutsummaryrefslogtreecommitdiff
path: root/use
blob: d0f962c6eed1e906a53e9a9c80b3f8ac6c988b89 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# This file is public domain.
#
# This is an interactive command line utility to handle scripts and
# use cases (an use case is a scripts that opens everything needed for
# the user to perform a task).
#
# In order to allow any programming language, your script must have
# a sha-bang (#!) line. If you want it to start a GUI, you can use 
# nohup in the command line. Using nohup in the sha-bang has the
# potential unwanted effects of exploding (if you forget to put the
# name of the shell after the word 'nohup') or scattering nohup.out
# files all over the place (if you start it from different places),
# so it's discouraged.
#
# The environment variables affecting this program are:
#  $USE_DIR: The directory to store (and read) the scripts. By
#    default, it is '~/.userc'.
#  $EDITOR: The text editor to edit the scripts. By default, it is
#    'nano'.
#  $SHOW: The default command to show the scripts (not editing). By
#    default, it is 'less' (you can use cat, nano...).


# Normalize environment variables.
USE_DIR=${USE_DIR:-~/.userc}
[ "$USE_DIR" != / ] && USE_DIR=${USE_DIR%/}

EDITOR=${EDITOR:-nano}
SHOW=${SHOW:-less}

# If if doesn't exist, create it if possible.
if [ ! -d "$USE_DIR" ]; then
	if [ -e "$USE_DIR" ]; then
		echo "$0: $USE_DIR: Not a directory."
		exit
	elif mkdir -p "$USE_DIR"; then
		echo "$0: Created directory '$USE_DIR'."
	else
		exit
	fi
fi

# Check read/list permissions.
if [ ! -r "$USE_DIR" -o ! -x "$USE_DIR" ]; then
	echo "$0: $USE_DIR: Unable to access directory contents."
	exit
fi

cd "$USE_DIR"

# Functions
user_wait() {
	echo -n "Press any key to continue..."
	read
}

error_ro() {
	echo "$0: $USE_DIR: Read-only directory."
	user_wait
}

read_num () {
	echo -n "Script number: "
	read num
	while [ -z "${use[num]}" ]; do
		[ -z "$num" ] && return 0
		echo "$0: Invalid number."
		echo -n "Script number: "
		read num
	done
	return 0
}

new_script () {
	if $writable; then
		echo -n "Name: "
		read name
		[ -z "$name" ] && return 0
		touch "$name"
		chmod +x "$name"
		$EDITOR "$name"
	else
		error_ro
	fi
}

delete_script () {
	if $writeable; then
		read_num
		rm "${use[num]}"
	else
		error_ro
	fi
}

edit_script () {
	read_num
	while [ ! -w "${use[num]}" ]; do
		echo "$0: ${use[num]}: Read-only file."
		read_num
	done
	$EDITOR "${use[num]}"
}
		

rename_script () {
	if $writeable; then
		read_num
		echo -n "New name: "
		read name
		[ -z "$name" ] && return 0
		mv "${use[num]}" "$name"
	else
		error_ro
	fi
}
		
show_script () {
	read_num
	$SHOW "${use[num]}"
}

		
# Menu
menu () {
	clear

	# Check write permission.
	if [ -w "$USE_DIR" ]; then
		writeable=true
	else
		echo "NOTE: Running in read-only mode."
		writeable=false
	fi

	# Get directory contents
	typeset -i cnt=0
	use[0]=""
	for f in *; do
		if [ -f "$f" -a -x "$f" ]; then
			cnt=cnt+1
			use[cnt]=$f
		fi
	done	

	# Start listing options
	if $writeable; then
		echo " n) New script"
		echo " d) Delete script"
		echo " r) Rename script"
	fi

	echo " e) Edit script"
	echo " s) Show script"

	for i in `seq $cnt`; do
		echo -n " $i) ${use[i]}"
		[ ! -w "${use[i]}" ] && echo -n " (read only)"
		echo
	done

	# Ask
	echo
	echo -n "Choose an option: "
	read opt

	case $opt in
	n|N) new_script ;;
	d|D) delete_script ;;
	e|E) edit_script ;;
	r|R) rename_script ;;
	s|S) show_script ;;
	*)
		if [ -n "${use[opt]}" ]; then
			"$USE_DIR/${use[opt]}"
			exit
		fi
	esac
}

while true; do
	menu
done