###############################################################
# GEMMA TRANSLATOR - RASPBERRY PI 5 CHEAT SHEET
# DroneBot Workshop
#
# Hardware: Raspberry Pi 5 (8GB), USB microphone, USB speaker,
#           HDMI display, USB keyboard
# OS:       Raspberry Pi OS (64-bit) Bookworm, already installed
#
# All lines starting with # are comments - copy only the commands.
###############################################################

### STAGE 1 - UPDATE THE OS AND INSTALL REQUIRED PACKAGES  #######

sudo apt update && sudo apt full-upgrade -y
sudo apt install -y git python3-venv python3-pip ffmpeg libasound2-dev pulseaudio-utils alsa-utils lsof netcat-openbsd nodejs npm

### STAGE 2 - SWITCH FROM WAYLAND TO X11 ######################

sudo raspi-config nonint do_wayland W1
sudo reboot

### STAGE 3 - VERIFY AUDIO DEVICES ############################

arecord -l
aplay -l

# Record (speak, then press Ctrl+C to stop):
pw-record test.wav
 
# Play it back:
pw-play test.wav

# Clean up the test file:
rm test.wav

### STAGE 4 - CLONE AND DEPLOY THE PROJECT ####################

cd ~
git clone https://github.com/google-gemma/gemma-translator.git
cd gemma-translator
chmod +x setup.sh download_model.sh start.sh deploy-pi.sh

# BUG FIX (required as of Aug 2026): setup.sh passes
# --require-hashes to pip, but requirements.txt contains no
# hashes, so pip aborts with:
#   "ERROR: Hashes are required in --require-hashes mode..."
# Remove the flag (packages remain pinned to exact versions):

sed -i 's/--require-hashes //' setup.sh

# One-command deployment (7 stages, approx. 20-40 minutes,
# dominated by the ~2.6GB model download and the npm build):

./deploy-pi.sh

# When it finishes, reboot. The Pi should boot straight into
# Chromium kiosk mode showing the translator UI:

sudo reboot

### STAGE 5 - SCALE THE UI FOR AN HDMI MONITOR ################
# The UI is hardcoded to 480x320 pixels. On a 1080p monitor it
# appears as a small box. Fix: add a scale factor to the kiosk
# launch line, then reboot.

sed -i 's|@chromium |@chromium --force-device-scale-factor=3 |' ~/.config/lxsession/rpd-x/autostart
sudo reboot


### STAGE 6 - ADDITIONAL LANGUAGE (FRENCH) ################
cd ~/gemma-translator

# Add French to the frontend language list (after Spanish)
sed -i 's|{ code: "es", name: "Spanish", voice: "tts", ttsLang: "es" },|{ code: "es", name: "Spanish", voice: "tts", ttsLang: "es" },\n  { code: "fr", name: "French", voice: "tts", ttsLang: "fr" },|' frontend/src/TranslatorApp.jsx

# Add French to the backend text-to-speech map (after Spanish)
sed -i 's|"es": "es-es",|"es": "es-es",\n    "fr": "fr-fr",|' backend/server.py

# Rebuild the UI and restart the service
npm --prefix frontend run build
sudo systemctl restart gemma-translator



