from __future__ import annotations
import pathlib
import re
import sys
main_path = pathlib.Path(sys.argv[1])
manifest_path = pathlib.Path(sys.argv[2])
build_path = pathlib.Path(sys.argv[3])
main_text = main_path.read_text(
encoding="utf-8",
)
marker = (
"SHIRE-MOBILE-SENTINEL-LIVE-ALERTS-0730"
)
anchor = (
" ApprovalNotifications.checkNow(this)"
)
addition = """ ApprovalNotifications.checkNow(this)
// SHIRE-MOBILE-SENTINEL-LIVE-ALERTS-0730
if (
android.os.Build.VERSION.SDK_INT >= 33 &&
checkSelfPermission(
android.Manifest.permission.POST_NOTIFICATIONS
) != android.content.pm.PackageManager.PERMISSION_GRANTED
) {
requestPermissions(
arrayOf(
android.Manifest.permission.POST_NOTIFICATIONS
),
7301
)
}
SentinelMobileAlerts.initialise(this)"""
if marker not in main_text:
if anchor not in main_text:
raise SystemExit(
"MainActivity notification anchor was not found."
)
main_text = main_text.replace(
anchor,
addition,
1,
)
main_path.write_text(
main_text,
encoding="utf-8",
)
manifest = manifest_path.read_text(
encoding="utf-8",
)
permission_anchor = (
' '
)
permission_block = """
"""
if (
"android.permission.FOREGROUND_SERVICE_SPECIAL_USE"
not in manifest
):
if permission_anchor not in manifest:
raise SystemExit(
"Manifest permission anchor was not found."
)
manifest = manifest.replace(
permission_anchor,
permission_block,
1,
)
component_marker = (
"SHIRE-MOBILE-SENTINEL-LIVE-ALERTS-0730"
)
components = """
"""
if component_marker not in manifest:
if " " not in manifest:
raise SystemExit(
"Manifest application closing tag was not found."
)
manifest = manifest.replace(
" ",
components + "\n ",
1,
)
manifest_path.write_text(
manifest,
encoding="utf-8",
)
build_text = build_path.read_text(
encoding="utf-8",
)
match = re.search(
r"versionCode\s*=\s*(\d+)",
build_text,
)
if not match:
raise SystemExit(
"Android versionCode was not found."
)
new_code = int(match.group(1)) + 1
build_text = re.sub(
r"versionCode\s*=\s*\d+",
f"versionCode = {new_code}",
build_text,
count=1,
)
build_text = re.sub(
r'versionName\s*=\s*"[^"]+"',
'versionName = "0.7.3-sentinel-mobile-alerts"',
build_text,
count=1,
)
build_path.write_text(
build_text,
encoding="utf-8",
)