#!/bin/sh

# Copyright 2025 Genesis Corporation
#
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

mount -t proc      proc      /proc
mount -t sysfs     sysfs     /sys
mount -t tmpfs     tmpfs     /tmp
mount -t devtmpfs  devtmpfs  /dev

export PYTHONHOME=/
export PYTHON_VER="3.10"
export PYTHONPATH="$PYTHONPATH:/lib/:/lib/python$PYTHON_VER/site-packages/genesis_seed"
export SEED_OS_AGENT="/lib/python$PYTHON_VER/site-packages/genesis_seed/genesis_seed/cmd/agent.py"
export SSL_CERT_FILE="/etc/ssl/certs/ca-certificates.crt"

# Check if running in autonomous mode
AUTONOMOUS_MODE="0"
if grep -q "autonomous=1" /proc/cmdline 2>/dev/null; then
    AUTONOMOUS_MODE="1"
    echo "Autonomous mode detected"
fi

# Function to mount data disk
mount_data_disk() {
    local mount_point="/mnt/autonomous"
    mkdir -p "$mount_point"

    # Try common disk partitions (vdb1, sdb1, nvme1n1p1)
    for disk in /dev/vdb1 /dev/sdb1 /dev/nvme1n1p1 /dev/xvdb1; do
        if [ -e "$disk" ]; then
            echo "Trying to mount $disk..."
            if mount "$disk" "$mount_point" 2>/dev/null; then
                echo "Successfully mounted $disk at $mount_point"
                return 0
            fi
        fi
    done

    return 1
}

# Function to apply netplan configuration from disk
apply_netplan_from_disk() {
    local mount_point="/mnt/autonomous"
    local netplan_dir="$mount_point/netplan"

    if [ ! -d "$netplan_dir" ]; then
        echo "Netplan directory not found on data disk: $netplan_dir"
        return 1
    fi

    # Apply netplan configuration using Python script with iproute2
    echo "Applying netplan configuration using iproute2..."
    python3 -c "from genesis_seed.cmd.netplan_apply import main; main()"

    return $?
}

# Function to copy agent working files from mounted disk and unmount
copy_agent_files_and_umount() {
    local mount_point="/mnt/autonomous"
    local work_dir="/autonomous"

    mkdir -p "$work_dir"

    for f in update.json private_key; do
        if [ -e "$mount_point/$f" ]; then
            cp "$mount_point/$f" "$work_dir/$f"
            echo "Copied $f to $work_dir"
        fi
    done

    umount "$mount_point" && echo "Unmounted $mount_point" || echo "WARNING: Failed to unmount $mount_point"
}

# Network configuration
if [ "$AUTONOMOUS_MODE" = "1" ]; then
    echo "Configuring autonomous mode..."

    # Mount data disk
    if mount_data_disk; then
        # Apply netplan configuration from disk (also brings up interfaces)
        apply_netplan_from_disk
        # Copy agent working files and unmount the data disk
        copy_agent_files_and_umount
    else
        echo "WARNING: Failed to mount data disk in autonomous mode"
    fi
else
    # Standard mode: use DHCP
    echo "Configuring standard mode with DHCP..."
    for iface_path in /sys/class/net/eth*; do
        if [ ! -e "$iface_path" ]; then
            continue
        fi
        iface=$(basename "$iface_path")
        ifconfig "$iface" up
        udhcpc -i "$iface" -n -s /bin/ifup.sh
    done
fi

echo "GENESIS SEED OS !!!"
python3 $SEED_OS_AGENT
sh
