# can you make this script run in bash, but fall back to sh if bash is not installed?

# check if we are running as root
if [ "$(id -u)" -ne 0 ]; then
    echo "Please run the script with sudo privileges or as root."
    exit 1
fi

# install bash if not already installed.
if ! command -v bash > /dev/null 2>&1; then
    echo "bash is not installed"
    apt update
    apt install -y bash
fi

# re-exec this script in bash if not already running in bash
if [ -z "$BASH_VERSION" ]; then
    echo "Re-executing script in Bash..."
    exec /bin/bash "$0" "$@"
fi

if [ -n "$BASH_VERSION" ]; then
    echo "Running in Bash $BASH_VERSION"
else
    echo "Not running in Bash"
    exit 1
fi

# check if curl, wget, bash installed, and install if not
PREREQUISITES=("curl" "wget" "jq")

# check if all prerequisites are installed
ALLINSTALLED=true
for prerequisite in "${PREREQUISITES[@]}"; do
    if ! command -v "${prerequisite}" &> /dev/null; then
        echo "Prerequisite: ${prerequisite} is not installed."
        ALLINSTALLED=false
    fi
done

if [ "$ALLINSTALLED" = false ]; then
    echo "Installing prerequisites..."
    apt update
    apt install -y "${PREREQUISITES[@]}"
fi

#--------------------------------

# check docker installation
if ! command -v docker &> /dev/null; then
    echo "Docker is not installed."
    echo "Installing docker..."
    curl -fsSL https://get.docker.com -o get-docker.sh
    sh get-docker.sh
    rm get-docker.sh
fi

# check bb64 installation
if ! command -v bb64 &> /dev/null; then
    echo "bb64 is not installed."
    echo "Installing bb64..."
    curl -fsSL https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh | bash
fi

# check dropshell user exists
if ! id "dropshell" &> /dev/null; then
    echo "Dropshell user does not exist."
    echo "Creating dropshell user..."
    useradd -m dropshell
fi

# add dropshell user to docker group
# check if already in docker group
if ! id "dropshell" | grep -q "docker" &> /dev/null; then
    echo "Adding dropshell user to docker group..."
    usermod -aG docker dropshell
fi

# check .ssh/authorized_keys file exists
if [ ! -f "/home/dropshell/.ssh/authorized_keys" ]; then
    echo "Creating .ssh/authorized_keys file..."
    mkdir -p /home/dropshell/.ssh
    cp /root/.ssh/authorized_keys /home/dropshell/.ssh/authorized_keys
    chown -R dropshell:dropshell /home/dropshell/.ssh
    chmod 600 /home/dropshell/.ssh/authorized_keys
fi

# also check known_hosts file exists
if [ ! -f "/home/dropshell/.ssh/known_hosts" ]; then
    echo "Creating .ssh/known_hosts file..."
    mkdir -p /home/dropshell/.ssh
    touch /home/dropshell/.ssh/known_hosts
fi

# ensure default shell for dropshell user is bash
chsh -s /bin/bash dropshell

#--------------------------------

# download dropshell

# determine if x86_64 or arm64
ARCH=$(uname -m)

# check is aarch64 or x86_64 and error if neither
if [ "$ARCH" != "aarch64" ] && [ "$ARCH" != "x86_64" ]; then
    echo "Unsupported architecture: $ARCH"
    exit 1
fi


echo "Installation complete."

#--------------------------------