#!/data/data/com.termux/files/usr/bin/bash # termux_setup.sh — Deploy the eSIM provisioning server on Android via Termux. # Run from the repo root: bash android/termux_setup.sh set -euo pipefail INSTALL_DIR="$HOME/esim-provisioning" FLASK_PORT=5000 NGINX_PORT=8080 # Default Android hotspot gateway — adjust if your device differs AP_IP="${ESIM_AP_IP:-192.168.43.1}" SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)" echo "=== eSIM Provisioning — Termux Setup ===" echo " Install dir: $INSTALL_DIR" echo " AP IP: $AP_IP" echo "" # ---- 1. Update and install packages ---- echo "[1/5] Installing Termux packages..." pkg update -y pkg install -y python python-pip nginx # ---- 2. Install Python dependencies ---- echo "[2/5] Installing Python dependencies..." pip install --quiet flask cryptography pymongo pyyaml # ---- 3. Deploy server files ---- echo "[3/5] Deploying server files..." mkdir -p "$INSTALL_DIR"/{server/core_adapters,portal/{css,js,assets},scripts,tests,data} cp -r "$SCRIPT_DIR/server/"* "$INSTALL_DIR/server/" cp -r "$SCRIPT_DIR/portal/"* "$INSTALL_DIR/portal/" cp -r "$SCRIPT_DIR/scripts/"* "$INSTALL_DIR/scripts/" cp "$SCRIPT_DIR/requirements.txt" "$INSTALL_DIR/" # Write a localised config cat > "$INSTALL_DIR/config.yaml" << EOF network: mcc: "302" mnc: "720" plmn: "302720" op_key: "63bfa50ee6523365ff14c1f45f88737d" wifi: ssid: "Emergency-5G" ip: "$AP_IP" server: host: "0.0.0.0" port: $FLASK_PORT debug: false database: path: "$INSTALL_DIR/data/subscribers.db" rate_limiting: max_profiles_per_mac: 1 window_hours: 1 core: type: "none" profile: carrier_name: "Ad-Hoc Network" profile_name: "Emergency 5G" apn: "internet" smdp_address: "esim.local" format: "v2" EOF # ---- 4. Configure nginx ---- echo "[4/5] Configuring nginx..." mkdir -p "$PREFIX/etc/nginx/conf.d" cat > "$PREFIX/etc/nginx/nginx.conf" << EOF worker_processes 1; events { worker_connections 64; } http { include mime.types; server { listen $NGINX_PORT default_server; server_name _; location = / { return 302 http://$AP_IP:$NGINX_PORT/activate; } location /activate { alias $INSTALL_DIR/portal/; index index.html; } location /css/ { alias $INSTALL_DIR/portal/css/; } location /js/ { alias $INSTALL_DIR/portal/js/; } location /api/ { proxy_pass http://127.0.0.1:$FLASK_PORT; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; } location / { return 302 http://$AP_IP:$NGINX_PORT/activate; } } } EOF # ---- 5. Initialise database and create start script ---- echo "[5/5] Initialising database and writing start script..." CONFIG_PATH="$INSTALL_DIR/config.yaml" python "$INSTALL_DIR/scripts/init_db.py" cat > "$INSTALL_DIR/start.sh" << 'STARTEOF' #!/data/data/com.termux/files/usr/bin/bash cd "$HOME/esim-provisioning" echo "Starting nginx..." nginx echo "Starting Flask provisioning server..." CONFIG_PATH="$HOME/esim-provisioning/config.yaml" python -m server.app & echo "" echo "Server running. Connect to Android hotspot then open:" echo " http://192.168.43.1:8080/activate" echo "" echo "To stop: pkill nginx; pkill -f server.app" STARTEOF chmod +x "$INSTALL_DIR/start.sh" # Termux:Boot integration BOOT_DIR="$HOME/.termux/boot" mkdir -p "$BOOT_DIR" cp "$INSTALL_DIR/start.sh" "$BOOT_DIR/esim-provisioning.sh" echo "" echo "=== Setup Complete ===" echo "" echo "Next steps:" echo " 1. Enable WiFi Hotspot in Android Settings (any SSID, no password)" echo " 2. Run: ~/esim-provisioning/start.sh" echo " 3. Clients connect to hotspot and visit:" echo " http://$AP_IP:$NGINX_PORT/activate" echo "" echo "Install Termux:Boot (from F-Droid) for auto-start on device reboot."