Files
JWeBot/deploy/scripts/switch-wechat-local-ocr.sh
T

56 lines
1.6 KiB
Bash

#!/bin/sh
set -eu
defaults=/etc/default/wechat-local-ocr
backup=$(mktemp /root/wechat-local-ocr-default.XXXXXX)
old_service=wechat-openclaw-bridge.service
recorder_service=wechat-local-ocr-recorder.service
responder_service=wechat-local-ocr-responder.service
success=0
cp -a "$defaults" "$backup"
rollback() {
status=$?
if [ "$success" -eq 0 ]; then
systemctl disable --now "$responder_service" >/dev/null 2>&1 || true
cp -a "$backup" "$defaults"
systemctl restart "$recorder_service" >/dev/null 2>&1 || true
systemctl enable --now "$old_service" >/dev/null 2>&1 || true
fi
rm -f "$backup"
exit "$status"
}
trap rollback EXIT HUP INT TERM
test "$(find /var/lib/wechat-local-ocr/queue -maxdepth 1 -type f | wc -l)" -eq 0
test "$(find /var/lib/wechat-local-ocr/failed -maxdepth 1 -type f | wc -l)" -eq 0
systemctl is-active --quiet "$recorder_service"
systemctl is-active --quiet "$old_service"
printf '%s\n' \
'WECHAT_QUEUE_ENABLED=1' \
'WECHAT_ROUTER_ENABLED=1' \
> "$defaults"
chmod 0644 "$defaults"
systemctl disable --now "$old_service"
systemctl restart "$recorder_service"
sleep 2
systemctl is-active --quiet "$recorder_service"
systemctl enable --now "$responder_service"
sleep 2
systemctl is-active --quiet "$responder_service"
test "$(systemctl is-active "$old_service")" = inactive
test "$(find /var/lib/wechat-local-ocr/queue -maxdepth 1 -type f | wc -l)" -eq 0
success=1
trap - EXIT HUP INT TERM
rm -f "$backup"
systemctl is-active "$recorder_service"
systemctl is-active "$responder_service"
systemctl is-active "$old_service" || true