#!/usr/bin/env bash set -euo pipefail # Kidcam Deployment Script for Ubuntu 20.04 (Jetson Nano) readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" readonly KIDCAM_USER="kidcam" readonly INSTALL_DIR="/opt/kidcam" readonly CONFIG_DIR="/etc/kidcam" readonly VENV_DIR="${INSTALL_DIR}/venv" log_info() { echo "[INFO] $*" >&2 } log_error() { echo "[ERROR] $*" >&2 } log_success() { echo "[SUCCESS] $*" >&2 } check_system() { log_info "Checking system requirements..." if [[ "$(uname -s)" != "Linux" ]]; then log_error "This script requires Linux" exit 1 fi if [[ "$(uname -m)" != "aarch64" ]]; then log_error "This script is designed for aarch64 (ARM64) architecture" log_error "Found: $(uname -m)" exit 1 fi if ! grep -q "Ubuntu" /etc/os-release; then log_error "This script is designed for Ubuntu" log_error "Found: $(cat /etc/os-release | grep PRETTY_NAME)" exit 1 fi if [[ $EUID -ne 0 ]]; then log_error "This script must be run as root (use sudo)" exit 1 fi log_success "System requirements verified" } create_user() { if id "${KIDCAM_USER}" &>/dev/null; then log_info "User '${KIDCAM_USER}' already exists" else log_info "Creating user '${KIDCAM_USER}'..." useradd --system --no-create-home --shell /usr/sbin/nologin \ --comment "Kidcam service account" "${KIDCAM_USER}" usermod -aG video "${KIDCAM_USER}" log_success "User '${KIDCAM_USER}' created" fi } install_system_dependencies() { log_info "Installing system dependencies..." apt-get update apt-get install -y \ python3 \ python3-pip \ python3-venv \ python3-dev \ libopencv-dev \ python3-opencv \ gstreamer1.0-tools \ gstreamer1.0-plugins-base \ gstreamer1.0-plugins-good \ gstreamer1.0-plugins-bad \ gstreamer1.0-plugins-ugly \ gstreamer1.0-libav \ libgstreamer1.0-dev \ libgstreamer-plugins-base1.0-dev \ v4l-utils \ build-essential \ cmake \ pkg-config log_success "System dependencies installed" } setup_directories() { log_info "Setting up directories..." mkdir -p "${INSTALL_DIR}" mkdir -p "${CONFIG_DIR}" mkdir -p "${INSTALL_DIR}/_/tmp" log_info "Copying project files..." rsync -av --exclude='_/' --exclude='.git' --exclude='*.pyc' \ "${PROJECT_ROOT}/" "${INSTALL_DIR}/" chown -R "${KIDCAM_USER}:${KIDCAM_USER}" "${INSTALL_DIR}" chown -R "${KIDCAM_USER}:${KIDCAM_USER}" "${CONFIG_DIR}" chmod 755 "${INSTALL_DIR}" chmod 750 "${CONFIG_DIR}" log_success "Directories configured" } setup_python_environment() { log_info "Setting up Python virtual environment..." if [[ ! -d "${VENV_DIR}" ]]; then sudo -u "${KIDCAM_USER}" python3 -m venv "${VENV_DIR}" log_success "Virtual environment created" else log_info "Virtual environment already exists" fi log_info "Installing Python packages..." sudo -u "${KIDCAM_USER}" "${VENV_DIR}/bin/pip" install --upgrade pip wheel setuptools # Install packages with careful dependency management sudo -u "${KIDCAM_USER}" "${VENV_DIR}/bin/pip" install \ ultralytics \ python-telegram-bot \ opencv-python \ torch torchvision --index-url https://download.pytorch.org/whl/cpu \ numpy \ pillow log_success "Python packages installed" } create_config() { local config_file="${CONFIG_DIR}/config.env" if [[ -f "${config_file}" ]]; then log_info "Config file already exists at ${config_file}" log_info "Skipping config creation. Review existing configuration." else log_info "Creating example configuration..." cat > "${config_file}" <<'EOF' # Kidcam Configuration # Copy this file to /etc/kidcam/config.env and edit with your values # Camera device (default: /dev/video0) CAMERA_DEVICE=/dev/video0 # RTSP stream port (default: 8554) STREAM_PORT=8554 # Person detection confidence threshold (0.0-1.0, default: 0.5) DETECTION_CONFIDENCE=0.5 # Cooldown period in minutes after detection (default: 5) COOLDOWN_MINUTES=5 # Telegram Bot Configuration # Get your bot token from @BotFather on Telegram TELEGRAM_BOT_TOKEN=your-bot-token-here # Get your chat ID by messaging your bot and checking: # https://api.telegram.org/bot/getUpdates TELEGRAM_CHAT_ID=your-chat-id-here # Python path for imports PYTHONPATH=/opt/kidcam EOF chown "${KIDCAM_USER}:${KIDCAM_USER}" "${config_file}" chmod 640 "${config_file}" log_success "Example config created at ${config_file}" log_info "IMPORTANT: Edit ${config_file} with your Telegram credentials" fi } install_systemd_service() { log_info "Installing systemd service..." cp "${SCRIPT_DIR}/kidcam.service" /etc/systemd/system/ chmod 644 /etc/systemd/system/kidcam.service # Update ExecStart to use virtualenv python sed -i "s|ExecStart=/usr/bin/python3|ExecStart=${VENV_DIR}/bin/python3|" \ /etc/systemd/system/kidcam.service systemctl daemon-reload systemctl enable kidcam.service log_success "Systemd service installed and enabled" } main() { log_info "Starting Kidcam deployment..." check_system create_user install_system_dependencies setup_directories setup_python_environment create_config install_systemd_service log_success "Deployment complete!" echo "" log_info "Next steps:" echo " 1. Edit /etc/kidcam/config.env with your Telegram credentials" echo " 2. Test camera access: v4l2-ctl --list-devices" echo " 3. Start service: sudo systemctl start kidcam" echo " 4. Check logs: sudo journalctl -u kidcam -f" echo "" log_info "For more information, see ${INSTALL_DIR}/Biz/Kidcam/README.md" } main "$@"