#!/bin/bash
# Info: Script to install/create/delete/update coredns from file settings 
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-11-2024

USAGE="install-coredns.sh install | update | remvoe"
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

[ -r "env-coredns" ] && . ./env-coredns

OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
CMD_TSKSRVC=${1:-install}

HOSTNAME=$(hostname)
export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

[ ! -d "/etc/coredns" ] && sudo mkdir  /etc/coredns
ROOT=$(dirname "$0")

_init() {
  [ -z "$COREDNS_VERSION" ] || [ -z "$ARCH" ] && exit 1
  local has_coredns
  local curr_vers
  has_coredns=$(type -P coredns)
  [ -n "$has_coredns" ] && curr_vers=$(coredns -version 2>/dev/null | grep CoreDNS | cut -f2 -d"-" | sed  's/ //g')
  [ "$curr_vers" == "$COREDNS_VERSION" ] && return
  [ -n "$has_coredns" ] && sudo timeout -k 10 20 systemctl stop coredns
  [ ! -d "tmp" ] && mkdir tmp
  rm -f "tmp/coredns_${COREDNS_VERSION}_${OS}_${ARCH}.tgz"
  if ! curl -fsSL https://github.com/coredns/coredns/releases/download/v${COREDNS_VERSION}/coredns_${COREDNS_VERSION}_${OS}_${ARCH}.tgz -o "tmp/coredns_${COREDNS_VERSION}_${OS}_${ARCH}.tgz" ; then 
    echo "Error downloading coredns_${COREDNS_VERSION}_${OS}_${ARCH}.tgz"
    exit 1
  fi
  if ! tar xzf "tmp/coredns_${COREDNS_VERSION}_${OS}_${ARCH}.tgz" -C tmp ; then
    echo "Error extracting coredns_${COREDNS_VERSION}-${ARCH}.tar.gz"
    exit 1
  fi
  rm -f "tmp/coredns_${COREDNS_VERSION}_${OS}_${ARCH}.tgz"
  [ ! -r "tmp/coredns" ] && echo "Error extracting coredns" && exit 1
  chmod +x tmp/coredns
  sudo mv tmp/coredns /usr/local/bin
  rm -r "tmp"
}
_config_coredns() {
  [ ! -d "/etc/coredns" ] && sudo mkdir /etc/coredns

  has_user=$(sudo grep coredns /etc/passwd)
  [ -z "$has_user" ] && sudo useradd -d /var/lib/coredns -m coredns

  # [ ! -d "/etc/ssl/coredns" ] && sudo mkdir -p /etc/ssl/coredns
  sudo cp "$ROOT"/Corefile  /etc/coredns 2>/dev/null
  sudo cp "$ROOT"/resources/*  /etc/coredns 2>/dev/null
  sudo rm -f /etc/coredns/*.j2
  sudo chown -R coredns:coredns /etc/coredns

  if  [ ! -L "/etc/systemd/system/coredns.service" ] ; then 
    sudo cp coredns.service /lib/systemd/system/coredns.service
    sudo timeout -k 10 20 systemctl daemon-reload >/dev/null 2>&1
    #[ ! -L "/etc/systemd/system/coredns.service" ] && sudo ln -s /lib/systemd/system/coredns.service /etc/systemd/system
  fi
  sudo timeout -k 10 20 systemctl enable --now coredns >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl restart coredns >/dev/null 2>&1
}
_stop_resolved() {
  sudo timeout -k 10 20 systemctl stop coredns >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable coredns >/dev/null 2>&1
  }
_remove_coredns() {
  sudo timeout -k 10 20 systemctl stop coredns >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable coredns >/dev/null 2>&1
}
_start_coredns() {
  sudo timeout -k 10 20 systemctl enable coredns >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl start coredns >/dev/null 2>&1
}
_restart_coredns() {
  sudo timeout -k 10 20 systemctl restart coredns >/dev/null 2>&1
}
if [ "$CMD_TSKSRVC" == "config" ] ; then
  _config_coredns
  exit
fi
if [ "$CMD_TSKSRVC" == "remove" ] ; then
  _remove_coredns
  exit
fi
if ! _init ; then
  echo "error coredns init"
  exit 1
fi
if ! _config_coredns ; then
  echo "error coredns config"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_coredns && exit 0
if ! _stop_resolved ; then 
  echo "error coredns stop"
  exit 1
fi
if ! _start_coredns ; then 
  echo "error coredns start"
  exit 1
fi