#!/usr/bin/env bash
# Copyright (c) 2021-2026 community-scripts ORG
# Author: Michel Roegl-Brunner (michelroegl-brunner)
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE

color() {
  return
}
catch_errors() {
  set -Eeuo pipefail
  trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
}

# This function handles errors
error_handler() {
  local line_number="$1"
  local command="$2"
  SCRIPT_NAME=$(basename "$0")
  local error_message="$SCRIPT_NAME: Failure in line $line_number while executing command $command"
  echo -e "\n$error_message"
  exit 0
}
verb_ip6() {
  STD=""
  return
}

msg_info() {
  local msg="$1"
  echo -ne "${msg}\n"
}

msg_ok() {
  local msg="$1"
  echo -e "${msg}\n"
}

msg_error() {

  local msg="$1"
  echo -e "${msg}\n"
}

RETRY_NUM=10
RETRY_EVERY=3
i=$RETRY_NUM

setting_up_container() {
  while [ $i -gt 0 ]; do
    if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" != "" ]; then
      break
    fi
    echo 1>&2 -en "No Network! "
    sleep $RETRY_EVERY
    i=$((i - 1))
  done

  if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" = "" ]; then
    echo 1>&2 -e "\n No Network After $RETRY_NUM Tries"
    echo -e "Check Network Settings"
    exit 1
  fi
  msg_ok "Set up Container OS"
  msg_ok "Network Connected: $(hostname -i)"
}

network_check() {
  RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
  if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi
  set -e
}

update_os() {
  msg_info "Updating Container OS"
  $STD apk -U upgrade
  msg_ok "Updated Container OS"
}

motd_ssh() {
  return
}

customize() {
  return
}
