-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.sh
executable file
·183 lines (154 loc) · 3.88 KB
/
functions.sh
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
#!/bin/sh
#!/bin/bash
#!/bin/sed -f
# ======================================
# Setings default variables
# ======================================
function set_defaults_variables()
{
site="$1"
db_name="$2"
prefix_table="nq4g6X_"
sr_url="n"
is_replace="n"
is_clone="n"
install_wp="n"
git_url=""
is_import_db="n"
file_sql=""
root="$HTTP_PATH/$site"
DB_TMP="db_tmp.sql"
usr_random="$(cat /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1)_usr"
WP_USER=$(id -un)
is_db_installed="n"
bold=$(tput bold)
normal=$(tput sgr0)
}
# ======================================
# Importing variable of configurations.
# ======================================
function importing_variables()
{
CONFIG_FILE="$1/config.conf"
VERSION_FILE="$dir_name/version.conf"
if [ ! -f "$CONFIG_FILE" ]; then
CONFIG_FILE="$1/config-sample.conf"
fi
if [ ! -f "$VERSION_FILE" ]; then
echo "CURRENT_WP_VERSION=$LATEST_WP_VERSION" > "$VERSION_FILE"
fi
source $CONFIG_FILE
source $VERSION_FILE
}
# ======================================
# Verifying necessary params
# ======================================
function verifying_params()
{
if [ ! "$1" ]; then
echo -n "Enter your domain, [example.com]: "
read site
fi
if [ ! "$2" ]; then
echo -n "Enter your database name: "
read db_name
fi
check_params_is_empty $site $db_name
root="$HTTP_PATH/$site"
}
# ======================================
# Check and download latest wordpress
# ======================================
function download_latest_wordpress()
{
if [[ "$LATEST_WP_VERSION" > "$CURRENT_WP_VERSION" ]] || [[ ! -d "$dir_name/wordpress" ]]; then
# Remove old directory wordpress
sudo rm -rf "$dir_name/wordpress"
# Download latest version WordPress
echo "Download latest WordPress version..."
wget --no-verbose --output-document="$dir_name/$LATEST_FILE" $WP_LATEST 2>/dev/null &
spinner $!
# Extract zip WordPress
echo "Extracting WordPress..."
unzip -qq "$dir_name/$LATEST_FILE" -d $dir_name
# Remove file latest.zip
echo "Removing zip file"
rm -rf "$dir_name/$LATEST_FILE"
# Set global Current WP Version
echo "CURRENT_WP_VERSION='$LATEST_WP_VERSION'" > "$dir_name/version.conf"
echo "[Done]"
add_separator
fi
}
# ======================================
# Check for Vhosts file exists
# ======================================
function check_vhosts_exits()
{
if [ ! -f $VHOSTS_FILE ]; then
echo -n "Vhosts file not exists, create new? [y/n]: "
read is_create_new_vhosts
create_new_vhosts_file $is_create_new_vhosts
aborted_process $is_create_new_vhosts
fi
}
# ======================================
# Create new Vhots files
# ======================================
function create_new_vhosts_file()
{
if [ $1 = 'y' ]; then
echo "=== Creating new vhosts file"
sudo su -c "echo 'Listen 80' > $VHOSTS_FILE"
echo "[Done]"
add_separator
fi
}
# ======================================
# Setting up database
# ======================================
function setting_database()
{
if [ "$is_import_db" != 'y' ]; then
create_file_tmp_db
installing_database "$dir_name/$DB_TMP"
rm -rf "$dir_name/$DB_TMP"
echo "=== Replace url from database"
replace_url "example.dev"
echo "[Done]"
is_db_installed="y"
fi
}
# ======================================
# Create temporary DB
# ======================================
function create_file_tmp_db()
{
if [ $WP_USER = "root" ]; then
WP_USER=$usr_random
fi
if [ "$is_multisite" = 'y' ]; then
db_multisite='_multisite'
fi
sed -e "s/{{USER}}/$WP_USER/g;s/{{COUNT_STRING_USER}}/${#WP_USER}/g;" "$dir_name/wp_db$db_multisite.sql" > "$dir_name/$DB_TMP"
}
function add_separator()
{
cols=$(/usr/bin/tput cols)
declare line
for i in `seq 1 ${cols}`; do
line="${line}-"
done
echo $line
}
function spinner()
{
PID=$1
i=0
spin="/-\|"
while [ -d /proc/$PID ]; do
i=$(( (i+1) %4 ))
printf "\r${spin:$i:1}"
sleep .1
done
}