-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinstall
More file actions
executable file
·202 lines (178 loc) · 4.97 KB
/
Copy pathinstall
File metadata and controls
executable file
·202 lines (178 loc) · 4.97 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
set -uo pipefail
IFS=$'\n\t'
#
# Installs LibreMail, checks for any diagnostic errors,
# and sets up the configuration files.
# 1. Capture the SQL config information
# 2. Create the database if it doesn't exist
# 3. Write the local.ini file with this info
# 4. Run `sync -d` to check for diagnostic errors
# 5. Run `sync -u` to run the DB migration scripts
##
## Get working directory
basepath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)";
## Sync local config file
syncLocalIni="${basepath}/config/local.ini"
## Webmail local environment file
webmailLocalEnv="${basepath}/../webmail/.env"
webmailDefaultEnv="${basepath}/../webmail/.env.example"
## SQL credentials (user input)
username=''
password=''
## Flag, if DB connection is up
dbConn=1
## Name of the database, could be prompted in the future
dbName='libremail'
## REMOVE
UPDATEDB="${basepath}/sync -u"
## Store the colors based on platform
if [[ "$(uname)" == "Darwin" ]] ; then
dim='\033[2;37m'
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
redBold='\033[1;31m'
greenBold='\033[1;32m'
NC='\033[0m' ## No color
else
dim='\e[2;37m'
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[0;33m'
redBold='\e[1;31m'
greenBold='\e[1;32m'
NC='\e[0m' ## No color
fi
## Displays info at the beginning of execution
function startup {
echo -e " _ _ _ __ __ _ _ "
echo -e " | | (_) |__ _ __ ___| \/ | __ _(_) |"
echo -e " | | | | '_ \| '__/ _ \ |\/| |/ _\` | | |"
echo -e " | |___| | |_) | | | __/ | | | (_| | | |"
echo -e " |_____|_|_.__/|_| \___|_| |_|\__,_|_|_|"
echo ''
echo -e " The #1 GPL Email Application Suite"
echo -e " Version 1.0 – Mike Gioia"
echo ''
}
## Checks if MySQL is installed on the machine
function checkSqlInstalled {
type mysql >/dev/null 2>&1
result=$?
if [ $result -ne 0 ]; then
echo -e "${redBold}MySQL is not installed!${NC}"
echo -e "${red}You must install MySQL to use LibreMail.${NC}"
echo ''
echo -e "${yellow}See the README (sync/README.md) for more info.${NC}"
echo ''
exit 0
fi
}
## Calls the sync script to check the SQL database
function checkDatabase {
echo -e "${green}Checking SQL connection${NC}"
${basepath}/sync -x -b
result=$?
if [ $result -eq 1 ]; then
echo -e "[${red}fail${NC}] ${red}Connection failed${NC}"
echo ''
dbConn=0
elif [ $result -eq 2 ]; then
echo -e "[${red}fail${NC}] ${red}Database not found or not created yet${NC}"
echo ''
fi
if [ $result -ne 0 ]; then
setupDatabase
else
echo -e "[ ${green}ok${NC} ] ${dim}Successfully connected to SQL${NC}"
echo ''
fi
}
## Prompts the user for database credentials
## Creates the database if not exist
function setupDatabase {
if [ $dbConn -eq 1 ]; then
createDatabase
else
captureCredentials
saveCredentialsSync
saveCredentialsWebmail
createDatabase
fi
}
## Reads in temporary username/password to create database
function createDatabase {
echo -e "${yellow}Please enter root user MySQL password${NC}"
echo -e "${yellow}This is needed to create the new database and won't be stored${NC}"
echo ''
read -sp ' Password: ' password
echo ''
echo ''
mysql -uroot -p${password} -e "CREATE DATABASE IF NOT EXISTS ${dbName} /*\!40100 DEFAULT CHARACTER SET utf8 */;"
result=$?
if [ $result -ne 0 ]; then
exit 0
fi
}
## Reads in the SQL username/password
function captureCredentials {
echo -e "${yellow}Please enter the database username and password${NC}"
echo -e "${yellow}These will be written to configuration files${NC}"
echo ''
read -p ' SQL username: ' username
read -sp ' SQL password: ' password
echo ''
echo ''
}
## Saves the username and password to the config file
function saveCredentialsSync {
echo '; Put your local configuration and overrides here. This' > $syncLocalIni
echo '; extends default.ini.' >> $syncLocalIni
echo '' >> $syncLocalIni
echo '[sql]' >> $syncLocalIni
echo '' >> $syncLocalIni
echo "username = \"${username}\"" >> $syncLocalIni
echo "password = \"${password}\"" >> $syncLocalIni
}
## Saves the username and password to the config file
function saveCredentialsWebmail {
## If the .env file doesn't exist, copy .env.example
if [[ ! -f $webmailLocalEnv ]]; then
cp $webmailDefaultEnv $webmailLocalEnv
fi
## Update the credential lines
sed -i '' -e "/DB_USERNAME=/ s/=.*/=${username}/" $webmailLocalEnv
sed -i '' -e "/DB_PASSWORD=/ s/=.*/=${password}/" $webmailLocalEnv
}
## Runs the sync script's diagnostic checks
function runDiagnostics {
${basepath}/sync -D
result=$?
if [ $result -ne 0 ]; then
exit 0
else
echo ''
fi
}
## Runs the database update scripts
function updateDatabase {
${basepath}/sync -u
result=$?
if [ $result -ne 0 ]; then
exit 0
else
echo ''
fi
}
## Prints closing message
function shutdown {
echo -e "${green}Installation finished!${NC}"
}
## Run everything
startup
checkSqlInstalled
checkDatabase
runDiagnostics
updateDatabase
shutdown