#!/usr/bin/bash

# An SSH command may arrive through sshd or as Bash's -c argument.
CMD=""
if [ -n "${SSH_ORIGINAL_COMMAND:-}" ]; then
    CMD="$SSH_ORIGINAL_COMMAND"
elif [ "$#" -ge 2 ] && [ "$1" = "-c" ]; then
    CMD="$2"
fi

# 1. Real interactive TTY login -> Launch appctl menu
if [ -t 0 ] && [ -z "$CMD" ]; then
    /usr/bin/appctl
    exec /bin/bash -l
fi

# 2. If an explicit SSH command was passed (e.g., 'date' or '/usr/bin/appctl --help')
if [ -n "$CMD" ]; then
    exec /bin/bash -c "$CMD"
fi

# 3. SCP / SFTP / Pipeline Fallback
# If there's no TTY and no explicit command, preserve the SSH arguments.
exec /bin/bash "$@"
