#!/usr/bin/env bash
#
# Copyright (c) 2024 Canaima_Deploy Contributors
#
# This file is part of Canaima_Deploy (see https://canaima_deploy.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Fetch x509 fingerprint
x509_fingerprint=$(openssl x509 -in /etc/canaima_deploy/certs/canaima_deploy.org/crt.pem -noout -fingerprint -sha256 | cut -d= -f2)


# Fetch SSH fingerprints
i=0
for keyfile in /etc/ssh/ssh_host_{ed25519,rsa,ecdsa}_key.pub; do
    if [ ! -f "$keyfile" ]; then
        continue
    fi
    output=$(ssh-keygen -l -f "$keyfile")
    fingerprint[i]=" - $(echo "$output" | cut -d' ' -f2) $(echo "$output" | cut -d' ' -f4)"
    i=$((i + 1))
done

#
# Build the logo
#

LOGO=$(cat << 'EOF'
  ____   _    _   _    _    ___ __  __    _    
 / ___| / \  | \ | |  / \  |_ _|  \/  |  / \   
| |    / _ \ |  \| | / _ \  | || |\/| | / _ \  
| |___/ ___ \| |\  |/ ___ \ | || |  | |/ ___ \ 
 \____/_/   \_\_| \_/_/   \_\___|_|  |_/_/   \_\
                                               
 ____  _____ ____  _     _____   __
|  _ \| ____|  _ \| |   / _ \ \ / /
| | | |  _| | |_) | |  | | | \ V / 
| |_| | |___|  __/| |__| |_| || |  
|____/|_____|_|   |_____\___/ |_|  
EOF
)

# ' Put a quote in comment to make vim happy about syntax highlighting :s

# Reminder for default login / password when postinstall is not done yet
if [ ! -f /etc/canaima_deploy/installed ]
then
   default_credentials_reminder="Para iniciar sesi\u00f3n en esta consola o v\u00eda SSH, el usuario y contrase\u00f1a por defecto son 'root' y 'canaima_deploy' (hasta que se complete la post-instalaci\u00f3n)."
else
   default_credentials_reminder=""
fi

#
# Build the actual message
#

sleep 5
# Get local IP
# (we do this after the sleep 5 to have
# better chances that the network is up)
local_ip=$(hostname --all-ip-address | awk '{print $1}')

LOGO_AND_FINGERPRINTS=$(cat << EOF

$LOGO

 IP Local: ${local_ip:-(no se detect\u00f3 IP?)}
 Huella X509 de CA SSL Local:
   ${x509_fingerprint}
 Huellas SSH:
 ${fingerprint[0]}
 ${fingerprint[1]}
 ${fingerprint[2]}

 ${default_credentials_reminder}

EOF
)

echo "$LOGO_AND_FINGERPRINTS" > /etc/issue

if ! groups | grep -q all_users && [[ ! -f /etc/canaima_deploy/installed ]]
then
    # chvt 2 eliminada para evitar ocultar el entorno de escritorio en m\u00e1quinas gr\u00e1ficas

    # Formatting
    [[ -n "$local_ip" ]] && local_ip=$(echo -e "https://$local_ip/") || local_ip="(no se detect\u00f3 IP?)"

    echo "$LOGO_AND_FINGERPRINTS"
    cat << EOF
===============================================================================
Ahora debes proceder con la post-instalaci\u00f3n de Canaima_Deploy. Aqu\u00ed se te
pedir\u00e1:
  - el dominio principal de tu servidor;
  - el usuario y contrase\u00f1a para el primer administrador

Puedes realizar este paso:
  - desde tu navegador web, accediendo a: https://canaima_deploy.local/ o ${local_ip}
  - o en esta terminal respondiendo 's\u00ed' a la siguiente pregunta

Si esta es tu primera vez con Canaima_Deploy, te recomendamos fuertemente que te tomes
el tiempo de leer la documentaci\u00f3n de administrador, en particular las secciones
'Finalizando tu configuraci\u00f3n' y 'Conociendo Canaima_Deploy'.
===============================================================================
${default_credentials_reminder}

EOF

    read -rp "Presiona cualquier tecla para continuar " -n 1

    chvt 3

    exit 0
fi
