#!/data/data/com.termux/files/usr/bin/bash # # Changemaker SMS Server — One-Command Phone Setup # # Usage: # bash setup.sh YOUR_API_KEY # # This script: # 1. Installs required packages (python, termux-api, flask) # 2. Saves the API key to ~/.bashrc # 3. Requests SMS & Contacts permissions # 4. Sets up Termux:Boot auto-start (if installed) # 5. Starts the SMS server # set -e # --- Colors --- RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' BOLD='\033[1m' NC='\033[0m' step() { echo -e "\n${CYAN}[$1/6]${NC} ${BOLD}$2${NC}"; } ok() { echo -e " ${GREEN}✓${NC} $1"; } warn() { echo -e " ${YELLOW}!${NC} $1"; } fail() { echo -e " ${RED}✗${NC} $1"; exit 1; } # --- Banner --- echo "" echo -e "${CYAN}═══════════════════════════════════════════════════${NC}" echo -e "${BOLD} Changemaker SMS Server — Phone Setup${NC}" echo -e "${CYAN}═══════════════════════════════════════════════════${NC}" # --- Validate API key --- API_KEY="$1" if [ -z "$API_KEY" ]; then echo "" echo -e "${RED}Usage: bash setup.sh YOUR_API_KEY${NC}" echo "" echo "Get the API key from the admin dashboard:" echo " SMS Setup → Step 3 → Generate API Key → Copy" exit 1 fi if [ ${#API_KEY} -lt 32 ]; then fail "API key looks too short (expected 64 hex characters). Check you copied the full key." fi # --- Step 1: Install packages --- step 1 "Installing packages" pkg update -y 2>/dev/null || true pkg install -y python git termux-api openssh 2>/dev/null if ! command -v python &>/dev/null; then fail "Python failed to install" fi ok "python, git, termux-api, openssh installed" if ! pip show flask &>/dev/null; then pip install flask 2>/dev/null fi ok "Flask installed" # --- Step 2: Save API key --- step 2 "Saving API key" export SMS_API_SECRET="$API_KEY" SHELL_RC="$HOME/.bashrc" [ -f "$HOME/.zshrc" ] && ! [ -f "$HOME/.bashrc" ] && SHELL_RC="$HOME/.zshrc" # Remove any existing SMS_API_SECRET lines if grep -q "SMS_API_SECRET" "$SHELL_RC" 2>/dev/null; then sed -i '/SMS_API_SECRET/d' "$SHELL_RC" warn "Replaced existing API key" fi echo "" >> "$SHELL_RC" echo "# Changemaker SMS Server" >> "$SHELL_RC" echo "export SMS_API_SECRET=\"$API_KEY\"" >> "$SHELL_RC" ok "Key saved to $SHELL_RC" ok "Key active in current session" # --- Step 3: Grant permissions --- step 3 "Requesting Android permissions" echo -e " ${YELLOW}Tap 'Allow' if Android shows permission prompts${NC}" # These trigger permission dialogs; capture output to suppress noise termux-sms-list -l 1 >/dev/null 2>&1 && ok "SMS permission granted" || warn "SMS permission — check Android Settings → Termux:API → Permissions" termux-contact-list >/dev/null 2>&1 && ok "Contacts permission granted" || warn "Contacts permission — check Android Settings → Termux:API → Permissions" termux-battery-status >/dev/null 2>&1 && ok "Battery status accessible" || true # --- Step 4: Create logs directory --- step 4 "Setting up directories" mkdir -p ~/logs ok "~/logs created" # --- Step 5: Termux:Boot auto-start --- step 5 "Configuring auto-start" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" if dpkg -l 2>/dev/null | grep -q termux-boot 2>/dev/null || [ -d "$HOME/.termux/boot" ]; then mkdir -p ~/.termux/boot echo '#!/data/data/com.termux/files/usr/bin/bash' > ~/.termux/boot/start-sms-server echo 'termux-wake-lock' >> ~/.termux/boot/start-sms-server echo "cd $SCRIPT_DIR" >> ~/.termux/boot/start-sms-server echo 'nohup bash sms-watchdog.sh >> ~/logs/watchdog.log 2>&1 &' >> ~/.termux/boot/start-sms-server chmod +x ~/.termux/boot/start-sms-server ok "Boot script created at ~/.termux/boot/start-sms-server" ok "Server will auto-start on phone reboot" else warn "Termux:Boot not detected — install from F-Droid for auto-start on reboot" warn "You can re-run this script after installing Termux:Boot" fi # --- Step 6: Start the server --- step 6 "Starting SMS server" cd "$SCRIPT_DIR" # Kill any existing instance pkill -f termux-sms-api-server.py 2>/dev/null || true pkill -f sms-watchdog.sh 2>/dev/null || true sleep 1 # Acquire wake lock termux-wake-lock 2>/dev/null || true # Start via watchdog (handles restarts) nohup bash sms-watchdog.sh >> ~/logs/watchdog.log 2>&1 & WATCHDOG_PID=$! sleep 3 # Check if it's running if curl -s --max-time 5 http://127.0.0.1:5001/health >/dev/null 2>&1; then ok "Server is running! (watchdog PID: $WATCHDOG_PID)" else warn "Server starting up... check ~/logs/sms-api.log if issues persist" fi # --- Summary --- echo "" echo -e "${CYAN}═══════════════════════════════════════════════════${NC}" echo -e "${GREEN}${BOLD} Setup complete!${NC}" echo -e "${CYAN}═══════════════════════════════════════════════════${NC}" echo "" # Get device IP DEVICE_IP=$(ip -4 addr show 2>/dev/null | grep -oP '(?<=inet\s)(?!127\.)\d+\.\d+\.\d+\.\d+' | head -1) if [ -n "$DEVICE_IP" ]; then echo -e " Phone URL: ${BOLD}http://${DEVICE_IP}:5001${NC}" fi echo -e " Health: ${BOLD}http://127.0.0.1:5001/health${NC}" echo -e " Logs: ${BOLD}tail -f ~/logs/sms-api.log${NC}" echo "" echo -e " ${YELLOW}Next:${NC} Go back to the admin dashboard SMS Setup wizard" echo -e " and enter this phone's URL in Step 2 (Connect)." echo "" # Show Tailscale IP if available TS_IP=$(ip -4 addr show tailscale0 2>/dev/null | grep -oP '(?<=inet\s)\d+\.\d+\.\d+\.\d+' || true) if [ -n "$TS_IP" ]; then echo -e " ${GREEN}Tailscale IP: ${BOLD}http://${TS_IP}:5001${NC} (recommended)" echo "" fi