-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.sh
More file actions
323 lines (268 loc) Β· 10.4 KB
/
Build.sh
File metadata and controls
323 lines (268 loc) Β· 10.4 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash
MODULES_DIR="Modules"
BUILD_DIR="Build"
# Check if megumi.sh exists and load configuration
TELEGRAM_ENABLED=false
if [ -f "megumi.sh" ]; then
source megumi.sh
TELEGRAM_ENABLED=true
fi
mkdir -p "$BUILD_DIR"
welcome() {
clear
echo "---------------------------------"
echo " Yamada Module Builder "
echo "---------------------------------"
echo ""
}
success() {
echo "---------------------------------"
echo " Build Process Completed "
printf " Ambatukam : %s seconds\n" "$SECONDS"
echo "---------------------------------"
}
# Function to send file to Telegram
send_to_telegram() {
local file_path="$1"
local caption="$2"
local chat_id="$3"
if [ -z "$TELEGRAM_BOT_TOKEN" ]; then
echo "Error: TELEGRAM_BOT_TOKEN is not set in megumi.sh!"
return 1
fi
if [ -z "$chat_id" ]; then
echo "Error: Chat ID is empty!"
return 1
fi
echo "Uploading $(basename "$file_path") to chat ID: $chat_id..."
# Send document to Telegram
response=$(curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendDocument" \
-F "chat_id=$chat_id" \
-F "document=@$file_path" \
-F "caption=$caption")
# Check if upload was successful
if echo "$response" | grep -q '"ok":true'; then
echo "β Successfully uploaded $(basename "$file_path") to $chat_id"
return 0
else
echo "β Failed to upload $(basename "$file_path") to $chat_id"
echo "Response: $response"
return 1
fi
}
# Function to send message to Telegram
send_message_to_telegram() {
local message="$1"
local chat_id="$2"
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$chat_id" ]; then
return 1
fi
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
-d "chat_id=$chat_id" \
-d "text=$message" \
-d "parse_mode=Markdown" > /dev/null
}
# Function to display available groups and get selection
select_telegram_groups() {
local available_groups=()
local group_names=()
# Parse TELEGRAM_GROUPS array
if [ ${#TELEGRAM_GROUPS[@]} -eq 0 ]; then
echo "No Telegram groups configured in megumi.sh"
return 1
fi
echo ""
echo "Available Telegram groups:"
echo "--------------------------"
local index=1
for group in "${TELEGRAM_GROUPS[@]}"; do
# Parse group entry: "GROUP_NAME:CHAT_ID"
local group_name=$(echo "$group" | cut -d':' -f1)
local chat_id=$(echo "$group" | cut -d':' -f2)
available_groups+=("$chat_id")
group_names+=("$group_name")
echo "$index. $group_name ($chat_id)"
((index++))
done
echo "a. All groups"
echo "0. Cancel"
echo ""
while true; do
read -p "Select groups (comma-separated numbers, 'a' for all, or '0' to cancel): " selection
selection=${selection,,} # Convert to lowercase
if [[ "$selection" == "0" ]]; then
return 1
elif [[ "$selection" == "a" || "$selection" == "all" ]]; then
SELECTED_GROUPS=("${available_groups[@]}")
SELECTED_GROUP_NAMES=("${group_names[@]}")
return 0
else
# Parse comma-separated selections
SELECTED_GROUPS=()
SELECTED_GROUP_NAMES=()
IFS=',' read -ra SELECTIONS <<< "$selection"
local valid=true
for sel in "${SELECTIONS[@]}"; do
sel=$(echo "$sel" | tr -d '[:space:]') # Remove whitespace
if [[ "$sel" =~ ^[0-9]+$ ]] && [ "$sel" -ge 1 ] && [ "$sel" -le ${#available_groups[@]} ]; then
local idx=$((sel-1))
SELECTED_GROUPS+=("${available_groups[$idx]}")
SELECTED_GROUP_NAMES+=("${group_names[$idx]}")
else
echo "Invalid selection: $sel"
valid=false
break
fi
done
if [ "$valid" = true ] && [ ${#SELECTED_GROUPS[@]} -gt 0 ]; then
return 0
fi
fi
echo "Please enter valid selections."
done
}
# Function to prompt for changelog
prompt_changelog() {
echo ""
read -p "Give changelog? (Y/N): " ADD_CHANGELOG
ADD_CHANGELOG=${ADD_CHANGELOG,,} # Convert to lowercase
if [[ "$ADD_CHANGELOG" == "y" || "$ADD_CHANGELOG" == "yes" ]]; then
echo ""
echo "Enter changelog (press Ctrl+D or type 'END' on a new line when finished):"
echo "---"
CHANGELOG=""
while IFS= read -r line; do
if [[ "$line" == "END" ]]; then
break
fi
if [ -n "$CHANGELOG" ]; then
CHANGELOG+=$'\n'
fi
CHANGELOG+="$line"
done
if [ -n "$CHANGELOG" ]; then
echo "---"
echo "Changelog captured successfully!"
return 0
else
echo "No changelog entered."
return 1
fi
else
return 1
fi
}
# Function to prompt for Telegram posting
prompt_telegram_post() {
echo ""
read -p "Post to Telegram groups? (y/N): " POST_TO_TELEGRAM
POST_TO_TELEGRAM=${POST_TO_TELEGRAM,,} # Convert to lowercase
if [[ "$POST_TO_TELEGRAM" == "y" || "$POST_TO_TELEGRAM" == "yes" ]]; then
return 0
else
return 1
fi
}
build_modules() {
rm -rf "$BUILD_DIR"/*
read -p "Enter Version (e.g., V1.0): " VERSION
while true; do
read -p "Enter Build Type (LAB/RELEASE): " BUILD_TYPE
BUILD_TYPE=${BUILD_TYPE^^}
if [[ "$BUILD_TYPE" == "LAB" || "$BUILD_TYPE" == "RELEASE" ]]; then
break
fi
echo "Invalid input. Please enter LAB or RELEASE."
done
cd "$MODULES_DIR" || exit 1
MODULE_ID=$(grep "^id=" "module.prop" | cut -d'=' -f2 | tr -d '[:space:]')
# Fix: Use sed without attempting to preserve permissions
# Create a temporary file for the sed operation
if [ -f "module.prop" ]; then
cp "module.prop" "module.prop.tmp"
sed "s/^version=.*$/version=$VERSION/" "module.prop.tmp" > "module.prop"
rm "module.prop.tmp"
fi
if [ -f "customize.sh" ]; then
cp "customize.sh" "customize.sh.tmp"
sed "s/^ui_print \"Version : .*$/ui_print \"Version : $VERSION\"/" "customize.sh.tmp" > "customize.sh"
rm "customize.sh.tmp"
fi
ZIP_NAME="${MODULE_ID}-${VERSION}-${BUILD_TYPE}.zip"
ZIP_PATH="../$BUILD_DIR/$ZIP_NAME"
zip -q -r "$ZIP_PATH" ./*
echo "Created: $ZIP_NAME"
cd ..
# Check if Telegram is enabled
if [ "$TELEGRAM_ENABLED" = true ]; then
# Prompt for Telegram posting
if prompt_telegram_post; then
# Prompt for changelog
HAS_CHANGELOG=false
if prompt_changelog; then
HAS_CHANGELOG=true
fi
# Select groups to post to
if select_telegram_groups; then
echo ""
echo "Uploading to selected Telegram groups..."
# Create a summary message
SUMMARY_MESSAGE="π *Yamada Module Build Complete*%0A%0A"
SUMMARY_MESSAGE+="π¦ *Module:* $MODULE_ID%0A"
SUMMARY_MESSAGE+="π·οΈ *Version:* $VERSION%0A"
SUMMARY_MESSAGE+="π§ *Build Type:* $BUILD_TYPE%0A"
# Add changelog if provided
if [ "$HAS_CHANGELOG" = true ] && [ -n "$CHANGELOG" ]; then
# URL encode the changelog for Telegram
ENCODED_CHANGELOG=$(echo "$CHANGELOG" | sed 's/%/%25/g; s/ /%20/g; s/!/%21/g; s/"/%22/g; s/#/%23/g; s/\$/%24/g; s/&/%26/g; s/'\''/%27/g; s/(/%28/g; s/)/%29/g; s/\*/%2A/g; s/+/%2B/g; s/,/%2C/g; s/-/%2D/g; s/\./%2E/g; s/\//%2F/g; s/:/%3A/g; s/;/%3B/g; s/</%3C/g; s/=/%3D/g; s/>/%3E/g; s/?/%3F/g; s/@/%40/g; s/\[/%5B/g; s/\\/%5C/g; s/\]/%5D/g; s/\^/%5E/g; s/_/%5F/g; s/`/%60/g; s/{/%7B/g; s/|/%7C/g; s/}/%7D/g; s/~/%7E/g')
# Replace newlines with %0A for Telegram
ENCODED_CHANGELOG=$(echo "$ENCODED_CHANGELOG" | tr '\n' ' ' | sed 's/ /%0A/g')
SUMMARY_MESSAGE+=%0A%0A"π *Changelog:*%0A$ENCODED_CHANGELOG"
fi
SUMMARY_MESSAGE+=%0A%0A"File uploading below... β¬οΈ"
local upload_success=0
local upload_total=${#SELECTED_GROUPS[@]}
# Loop through selected groups
for i in "${!SELECTED_GROUPS[@]}"; do
local chat_id="${SELECTED_GROUPS[$i]}"
local group_name="${SELECTED_GROUP_NAMES[$i]}"
echo ""
echo "π€ Posting to: $group_name"
# Send summary message first
send_message_to_telegram "$SUMMARY_MESSAGE" "$chat_id"
# Upload the zip file
if [ -f "$BUILD_DIR/$ZIP_NAME" ]; then
caption="π± $MODULE_ID - $VERSION ($BUILD_TYPE)"
if send_to_telegram "$BUILD_DIR/$ZIP_NAME" "$caption" "$chat_id"; then
((upload_success++))
# Send completion message
COMPLETION_MESSAGE="β
*Upload Complete!*%0A%0AModule uploaded successfully to $group_name."
send_message_to_telegram "$COMPLETION_MESSAGE" "$chat_id"
else
# Send failure message
FAILURE_MESSAGE="β *Upload Failed*%0A%0AThere was an issue uploading the module to $group_name."
send_message_to_telegram "$FAILURE_MESSAGE" "$chat_id"
fi
else
echo "Error: ZIP file not found at $BUILD_DIR/$ZIP_NAME"
fi
done
echo ""
echo "π Upload Summary:"
echo "β
Successful: $upload_success/$upload_total groups"
echo "β Failed: $((upload_total - upload_success))/$upload_total groups"
else
echo "Telegram upload cancelled."
fi
else
echo "Skipping Telegram upload."
fi
else
echo ""
echo "Post to telegram disabled, please setup megumi.sh and configure TELEGRAM_GROUPS array"
fi
}
welcome
SECONDS=0 # Start timing
build_modules
success