-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup-manager
executable file
·160 lines (139 loc) · 4.83 KB
/
startup-manager
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
#!/bin/bash
# Startup Manager
# Purpose: This manager sets up the environment for either a normal auto-startup entity or a daemon auto-startup entity.
# Author: Daniel Okwufulueze
# Date: 15/05/2017
entity=;
commandSuffix=;
outputStream=;
outputFile=
SHELL_COMMANDS=();
TEMP_SHELL_COMMANDS=();
function populateShellCommandsFromFile() {
while IFS="" read -r fileLine || [[ -n "${fileLine}" ]]; do
[[ "${fileLine}" == "" ]] || TEMP_SHELL_COMMANDS+=("${fileLine}");
done < "$1";
}
function setupManagerForEntity() {
entity="$1";
if [[ -n "${entity}" && "${entity}" == "daemon" ]]; then
commandSuffix='&';
outputStream="black-hole";
else
commandSuffix="";
outputStream=;
fi
}
function prepareExitAction() {
local statusCode="$1";
case "$statusCode" in
20 )
echo "You supplied an invalid command.";
exit 66;
;;
21 )
echo "You didn't supply any command. Use the -c | --command option with a valid command.";
exit 66;
;;
30 )
echo "You supplied an invalid file name.";
exit 66;
;;
31 )
echo "You didn't supply any file name. Use the -f | --file-name option with a valid file name.";
exit 66;
;;
60 )
echo "You supplied an invalid output file name.";
exit 66;
;;
61 )
echo "You didn't supply any output file name. Use the -o | --output option with a valid output file name.";
exit 66;
;;
* )
exit 0;
;;
esac
}
function determineEntryValidity() {
local entry="$1";
local invalidEntryStatus="$2";
local emptyEntryStatus="$3";
local VALIDITY_STATUS_CODE=;
if [[ -n "${entry}" && "${entry::1}" == "-" ]]; then
prepareExitAction "${invalidEntryStatus}";
elif [[ -z "${entry}" ]]; then
prepareExitAction "${emptyEntryStatus}";
fi
VALIDITY_STATUS_CODE="$?";
return "${VALIDITY_STATUS_CODE}";
}
function checkStringConformanceForModifyability() {
local stringObject="$1";
local regularExpression="(\s*)cd(\s*)";
if [[ $stringObject =~ $regularExpression ]]; then
return 1;
else
return 2;
fi
}
function createFileIfNotExists() {
[[ -n "$1" ]] && local fileName="$1" || local fileName="${outputFile}";
[[ -n fileName &&
(
( -n "$2" && "$2" == "/" && "${fileName::1}" == "/" ) ||
( -z "$2" && "${fileName::1}" != "/" )
)
]] && ( test -e "${fileName}" || touch "${fileName}" );
}
function formatCommandForOutputStream() {
local commandStatement="$1";
local formattedCommandStatement=;
if [[ "${outputStream}" == 'nohup' ]]; then
[[ -n "${entity}" && "${entity}" == "daemon" ]] && formattedCommandStatement=" nohup (${commandStatement} ${commandSuffix})" || formattedCommandStatement=" nohup ${commandStatement} ${commandSuffix}";
elif [[ "${outputStream}" == 'black-hole' ]]; then
[[ -n "${entity}" && "${entity}" == "daemon" ]] && formattedCommandStatement=" (${commandStatement} &>/dev/null ${commandSuffix})" || formattedCommandStatement=" ${commandStatement} &>/dev/null ${commandSuffix}";
elif [[ "${outputStream}" == 'custom' ]]; then
[[ -n "${entity}" && "${entity}" == "daemon" ]] && formattedCommandStatement=" (${commandStatement} &>>${outputFile} ${commandSuffix})" || formattedCommandStatement=" ${commandStatement} &>>${outputFile} ${commandSuffix}";
fi
echo "${formattedCommandStatement}" | cat;
}
function assessModifyAndUseString() {
local commandItem="$1";
local commandStatement=;
local formattedToken=;
local validTokens=();
IFS=";&" read -ra commandTokens <<< "${commandItem}";
for i in "${!commandTokens[@]}"; do
[[ -n "${commandTokens[i]}" ]] && validTokens+=("${commandTokens[i]}");
done
if [[ "${#validTokens[@]}" -gt 0 ]]; then
for validToken in "${validTokens[@]}"; do
checkStringConformanceForModifyability "${validToken}";
local FORMATTED_COMMAND_CODE="$?";
[[ "${FORMATTED_COMMAND_CODE}" -eq "1" ]] || formattedToken=$(formatCommandForOutputStream "${validToken}");
[[ -n "${formattedToken}" ]] && commandStatement="${commandItem/$validToken/$formattedToken}" || commandStatement="${commandItem/$validToken/$validToken}";
done
else
checkStringConformanceForModifyability "${commandItem}";
local FORMATTED_COMMAND_CODE="$?";
[[ "${FORMATTED_COMMAND_CODE}" -eq "1" ]] && commandStatement="${commandItem}" || commandStatement=$(formatCommandForOutputStream "${commandItem}");
fi
SHELL_COMMANDS+=("${commandStatement}");
}
function formatShellCommands() {
if [[ -n "${outputStream}" ]]; then
for commandItem in "${TEMP_SHELL_COMMANDS[@]}"; do
assessModifyAndUseString "${commandItem}";
done
else
for commandItem in "${TEMP_SHELL_COMMANDS[@]}"; do
SHELL_COMMANDS+=("${commandItem}");
done
fi
}
export setupManagerForEntity;
export determineEntryValidity;
export populateShellCommandsFromFile;
export formatShellCommands;