#!/usr/bin/env bash # === CONFIG === # Path to the provider templates directory TEMPLATE_DIR="/var/lib/3cxpbx/Instance1/Data/Http/Templates/provider" # Filenames ORIGINAL="voipms.pv.xml" CUSTOM="voipms-smscustom.pv.xml" # DB config DB_NAME="database_single" DB_USER="postgres" # === SCRIPT === # Full paths ORIGINAL_PATH="$TEMPLATE_DIR/$ORIGINAL" CUSTOM_PATH="$TEMPLATE_DIR/$CUSTOM" echo "Using template directory: $TEMPLATE_DIR" # Safety check if [ ! -f "$ORIGINAL_PATH" ]; then echo "Error: Original template $ORIGINAL_PATH not found!" exit 1 fi echo "Copying $ORIGINAL_PATH to $CUSTOM_PATH..." cp "$ORIGINAL_PATH" "$CUSTOM_PATH" echo "Updating template header name..." # Replace the in the header section (first only) sed -i '0,/VoIP.MS<\/name>/s//VoIP.MS-SMS custom<\/name>/' "$CUSTOM_PATH" echo "Updating MessagingUrl block..." # Replace the MessagingUrl line with the new block awk ' BEGIN { repl = " \n" \ " PROVIDER_URL_KEY\n" \ " \n" \ " \n" \ " " } /]*>https:\/\/voip\.ms\/api\/3cx\/msg<\/field>/ { print repl next } { print } ' "$CUSTOM_PATH" > "${CUSTOM_PATH}.tmp" && mv "${CUSTOM_PATH}.tmp" "$CUSTOM_PATH" chown phonesystem:phonesystem $CUSTOM_PATH echo "Custom template created at: $CUSTOM_PATH" echo "Updating trunk model in database..." # Run the psql update — using sudo for the postgres user sudo -u postgres psql -d "$DB_NAME" -c "UPDATE gateway SET model = '$CUSTOM' WHERE model = '$ORIGINAL';" echo "Database model updated for all matching trunks!" 3CXStopServices 3CXStartServices