-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-compile
executable file
·116 lines (99 loc) · 4.4 KB
/
docker-compile
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
#!/usr/bin/env bash
set -e
PRE_RUN='pre-run'
HELP="
Usage: ./docker-compile -n <server-name> [options]
Options:
-n <server-name> : Name of the server. (Required)
-p <pre-run> : Pre-run script name. (Optional: default is 'pre-run')
-r : Remove emojis from output. (Optional)
-h : Display this help message.
Example:
./docker-compile -n mysite.com -p pre-run-mysite.com
Note:
The '-p' option is optional if there's an active pre-run already set.
"
REMOVE_EMOJIS=false
# getopts for -p for pre-run (default:pre-run) and require -n for server name, take a -h for help too, -r for remove emojis (optional)
while getopts ":n:p:rh" opt; do
case ${opt} in
n ) # process option n
SERVER_NAME=$OPTARG
;;
p ) # process option p
PRE_RUN=$OPTARG
;;
r ) # process option r
REMOVE_EMOJIS=true
;;
h ) # process option h
printf "$HELP"
exit 1
;;
\? ) # invalid option
printf "Invalid option: $OPTARG" 1>&2
printf "$HELP"
exit 1
;;
: ) # invalid option
printf "Invalid option: $OPTARG requires an argument" 1>&2
printf "$HELP"
exit 1
;;
esac
done
# Check if -c requires -n
if [ -z "$SERVER_NAME" ]; then
printf "============ ❌ compiling requires -n option for server-name to know which server-script to compile\n"
exit 1
fi
# check if scripts/server-<server-name>/run-<server-name> exists
if [ ! -f scripts/server-$SERVER_NAME/run-$SERVER_NAME ]; then
printf "============ ❌ scripts/server-$SERVER_NAME/run-$SERVER_NAME does not exist\n"
exit 1
fi
# check if pre-run exists
if [ ! -f "pre-run/$PRE_RUN" ]; then
printf "============ ❌ pre-run/$PRE_RUN does not exist\n"
exit 1
fi
# We need to compile the pre-run and scripts/server-<server-name>/run-<server-name> into scripts/server-<server-name>/compiled-<server-name>
# in the in the pre-run, everything below the line "#### START SCRIPT" should be copied into the compiled script, and everything in scripts/server-<server-name>/run-<server-name> should be copied into the compiled script after that
# then we need to chmod +x scripts/server-<server-name>/compiled-<server-name>
printf "============ ⚙️\tPre-Run: $PRE_RUN\n"
printf "============ 🖥️\tServer name: $SERVER_NAME\n"
# check if scripts/server-<server-name>/compiled-<server-name> exists
if [ -f scripts/server-$SERVER_NAME/compiled-$SERVER_NAME ]; then
# prompt to override it?
read -p "============ 🤔 scripts/server-$SERVER_NAME/compiled-$SERVER_NAME already exists. Overwrite? (y/n)" -n 1 -r
printf "\n"
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm scripts/server-$SERVER_NAME/compiled-$SERVER_NAME
else
printf "============ ❌ scripts/server-$SERVER_NAME/compiled-$SERVER_NAME already exists\n"
exit 1
fi
fi
# in pre-runs/pre-run grab everything after #### START SCRIPT and put it into a variable
PRE_RUN=$(sed -n '/#### START SCRIPT/,$p' pre-run/$PRE_RUN)
# Copy scripts/server-<server-name>/run-<server-name> into scripts/server-<server-name>/compiled-<server-name>
cp scripts/server-$SERVER_NAME/run-$SERVER_NAME scripts/server-$SERVER_NAME/compiled-$SERVER_NAME
# Insert the content of $PRE_RUN into scripts/server-<server-name>/compiled-<server-name> at the #### INSERT PRE-RUN SCRIPT HERE line
sed -i '' -e "/#### INSERT PRE-RUN SCRIPT HERE/r /dev/stdin" scripts/server-$SERVER_NAME/compiled-$SERVER_NAME <<< "$PRE_RUN"
if [ "$REMOVE_EMOJIS" = true ]; then
printf "============ 🧹 Removing emojis from scripts/server-$SERVER_NAME/compiled-$SERVER_NAME\n"
# Remove any emojis from scripts/server-<server-name>/compiled-<server-name>
tmpfile=$(mktemp)
while IFS= read -r line; do
if [[ $line == *printf* ]] || [[ $line == *echo* ]]; then
echo "$line" | tr -cd '\11\12\15\40-\176' >> "$tmpfile"
else
echo "$line" >> "$tmpfile"
fi
done < "scripts/server-$SERVER_NAME/compiled-$SERVER_NAME"
mv "$tmpfile" "scripts/server-$SERVER_NAME/compiled-$SERVER_NAME"
fi
# chmod +x scripts/server-<server-name>/compiled-<server-name>
chmod +x scripts/server-$SERVER_NAME/compiled-$SERVER_NAME
printf "============ ✅ Compiled script created: scripts/server-$SERVER_NAME/compiled-$SERVER_NAME\n"
exit 1