This repository was archived by the owner on Mar 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_one.sh
More file actions
executable file
·154 lines (125 loc) · 3.45 KB
/
install_one.sh
File metadata and controls
executable file
·154 lines (125 loc) · 3.45 KB
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
#!/bin/bash
trap 'exit' ERR
# --- Constant definitions. ---
# x is empty.
x=''
# The name of this script.
scriptName='install_one.sh'
# --- --- ---
# --- Function definitions. ---
function error_exit
{
# ----------------------------------------------------------------
# Print error message and exit.
# Accepts 2 argument:
# string - optional - error message
# integer - optional - the exit code
# ----------------------------------------------------------------
echo "${scriptName}: ${1:-"Unknown Error"}" 1>&2
exit ${2:-"1"}
}
# --- --- ---
# --- Check arguments. ---
if [ -z "${1+x}" ]; then
error_exit "Usage: ./${scriptName} script_name" 2
fi
if [[ "${1}" != *'.sh' ]]; then
error_exit "Can only install '.sh' files." 2
fi
if [[ "${1}" == 'install_'*'.sh' ]]; then
error_exit 'Cannot install the install util scripts.' 2
fi
# --- --- ---
# --- Determine the location of this script. ---
if [ ! -z "${0+x}" ]\
&& [ -e "${0}" ]\
&& [[ "${0}" == *"${scriptName}" ]]\
&& [ $(basename "${0}") = "${scriptName}" ]; then
scriptLocation="$(dirname "${0}")"
elif [ -e "$(pwd)/${scriptName}" ]; then
scriptLocation="$(pwd)"
elif [ -e "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/${scriptName}" ]; then
scriptLocation="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
else
error_exit 'Can not determine the location of this script...\nPlease run this script from the same folder as the script.' 1
fi
# --- --- ---
# --- Install the script. ---
f="${scriptLocation}/${1}"
echo 'vvv Heading vvv'
echo 'Script:'
echo "${f}"
echo '^^^ Heading ^^^'
# Installed check.
if [ -e "${f}.installed" ]; then
echo 'Already installed.'
echo '### ### ###'
echo
exit 0
fi
# Existence check.
if [[ ! -e "${f}" ]]; then
error_exit 'Script not found.' 2
fi
# Executable check.
if [[ ! -x "${f}" ]]; then
error_exit 'Script is not executable.' 2
fi
# Optional check.
if [[ "${1}" == *'_opt_'* ]]; then
# Ask if they want to install the optional script.
while true; do
read -n 1 -p 'Do you want to install this optional script (y/n)? ' choice
echo
case "$choice" in
y|Y )
break
;;
n|N )
echo 'User said they did not want to install the optional script.'
echo '### ### ###'
echo
exit 0
;;
* )
echo 'Invalid option.'
;;
esac
done
fi
# Run the script.
echo 'vvv Script output vvv'
if [[ "${1}" == *'_sudo_'* ]] || [[ "${1}" == *'_opt_sudo_'* ]]; then
sudo "${f}"
exitCode=$?
else
bash "${f}"
exitCode=$?
fi
echo '^^^ Script output ^^^'
# Check the exit status.
if [ "${exitCode}" -ne 0 ]; then
error_exit "Something went wrong with the scripts execution... (Exit code: ${exitCode})" 3
fi
# Ask user to check the output.
while true; do
read -n 1 -p 'Please check the output. Did the script install correctly (y/n)? ' choice
echo
case "$choice" in
y|Y )
break
;;
n|N )
error_exit 'User said that the script did not install correctly.' 4
;;
* )
echo 'Invalid option.'
;;
esac
done
# Everything went well, add a '.installed' file.
touch "${f}.installed" || error_exit "Failed to create file '${f}.installed' please create it manually."
echo 'Installed!'
echo '^^^ ^^^ ^^^'
echo
# --- --- ---