Files
picUploadApi/demo/install.sh

121 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# install.sh - Set up PicUpper webcam capture on Raspberry Pi
# Creates cron job for 1 picture per minute
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CAPTURE_SCRIPT="${SCRIPT_DIR}/capture-upload.sh"
CONFIG_FILE="${SCRIPT_DIR}/picupper.conf"
LOG_FILE="${SCRIPT_DIR}/picupper.log"
echo "=== PicUpper Installer ==="
echo ""
# Check dependencies
echo "Checking dependencies..."
if ! command -v fswebcam &>/dev/null; then
echo "❌ fswebcam not installed"
echo " Run: sudo apt update && sudo apt install -y fswebcam"
exit 1
fi
echo "✓ fswebcam"
if ! command -v curl &>/dev/null; then
echo "❌ curl not installed"
echo " Run: sudo apt update && sudo apt install -y curl"
exit 1
fi
echo "✓ curl"
# Check config
if [[ ! -f "$CONFIG_FILE" ]]; then
echo ""
echo "❌ Configuration file not found!"
echo " Please create it first:"
echo " cp picupper.conf.example picupper.conf"
echo " nano picupper.conf"
exit 1
fi
echo "✓ Config file exists"
# Check API key is configured
# shellcheck source=/dev/null
source "$CONFIG_FILE"
if [[ -z "${API_KEY:-}" || "$API_KEY" == "your-api-key-here" ]]; then
echo ""
echo "❌ API key not configured in picupper.conf"
echo " Edit the file and set your API key"
exit 1
fi
echo "✓ API key configured"
# Make scripts executable
chmod +x "$CAPTURE_SCRIPT"
chmod +x "${SCRIPT_DIR}/uninstall.sh" 2>/dev/null || true
echo "✓ Scripts are executable"
# Touch log file to ensure it exists
touch "$LOG_FILE" 2>/dev/null || true
echo "✓ Log file: $LOG_FILE"
# Set up cron job (script handles its own logging, no redirect needed)
CRON_JOB="* * * * * $CAPTURE_SCRIPT"
echo ""
echo "Setting up cron job for 1 picture per minute..."
# Check if cron job already exists
if crontab -l 2>/dev/null | grep -q "$CAPTURE_SCRIPT"; then
echo "⚠ Cron job already exists, updating..."
# Remove old entry and add new one
(crontab -l 2>/dev/null | grep -v "$CAPTURE_SCRIPT"; echo "$CRON_JOB") | crontab -
else
# Add to crontab
(crontab -l 2>/dev/null || true; echo "$CRON_JOB") | crontab -
fi
echo "✓ Cron job configured"
echo ""
echo "=== Installation Complete ==="
echo ""
echo "Your Raspberry Pi will now capture a picture every minute."
echo ""
echo "Useful commands:"
echo " View logs: tail -f $LOG_FILE"
echo " Test capture: $CAPTURE_SCRIPT --test"
echo " Run manually: $CAPTURE_SCRIPT"
echo " Uninstall: ${SCRIPT_DIR}/uninstall.sh"
echo ""
# Offer optional optimizations
echo "=== Optional: System Optimizations ==="
echo ""
read -p "Disable swap & apt-updates to preserve SD card? [y/N] " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Disabling swap..."
sudo swapoff -a 2>/dev/null || true
echo "Disabling apt-daily timers..."
sudo systemctl disable apt-daily.timer apt-daily-upgrade.timer 2>/dev/null || true
echo "✓ SD card preservation enabled"
fi
echo ""
read -p "Set up auto-reboot every 6 hours for stability? [y/N] " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
REBOOT_JOB="0 */6 * * * /sbin/reboot"
if sudo crontab -l 2>/dev/null | grep -q "/sbin/reboot"; then
echo "⚠ Reboot cron already exists, skipping"
else
(sudo crontab -l 2>/dev/null || true; echo "$REBOOT_JOB") | sudo crontab -
echo "✓ Auto-reboot scheduled (00:00, 06:00, 12:00, 18:00)"
fi
fi
echo ""
echo "Setup complete! First capture will happen at the next minute."