# +---------------------------------------------------+
# Configure Webmin
# +---------------------------------------------------+

function func_webmin() {
  func_echo-header

  local ASKWEBMIN
  local webminpwd1
  local webminpwd2
  local flag
  local flag2

  echo -e "$green[eFa-NG]$clean Webmin is a user friendly web management tool for linux that runs on port 10000"
  
  flag2=0
  while [ $flag2 -eq 0 ]; do
      echo -e ""
      echo -e -n "$green[eFa-NG]$clean Would you like to $green Enable $clean Webmin? [Y/n/c]: "
      read ASKWEBMIN
      if [[ $ASKWEBMIN =~ ^[yY]$ || -z $ASKWEBMIN ]]; then
        flag2=1

        curl -o /var/eFa/lib/setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
        sh /var/eFa/lib/setup-repos.sh --force
        rm -f /var/eFa/lib/setup-repos.sh
        
        # Install webmin
        dnf -y install webmin

        # Open port 10000
        firewall-cmd --add-port 10000/tcp --permanent
        firewall-cmd --reload

        # Set webmin password
        flag=0
        while [ $flag -eq 0 ]; do
          echo -e ""
          echo -e -n "$green[eFa-NG]$clean Please enter a root password to use for webmin: "
          read -s webminpwd1
          echo -e ""
          echo -e -n "$green[eFa-NG]$clean Please re-enter a root password to use for webmin: "
          read -s webminpwd2
          echo -e ""

          if [[ -z $webminpwd1 || "$webminpwd1" != "$webminpwd2" ]]; then
            echo "$green[eFa-NG]$clean Passwords do not match, please try again."
          else
            flag=1
          fi
        done

        # set password
        /usr/libexec/webmin/changepass.pl /etc/webmin root $webminpwd1
        webminpwd1=
        webminpwd2=

        # new password is not used if service is not restarted
        systemctl restart webmin

        echo -e "$green[eFa-NG]$clean Webmin is now enabled."

        pause
      elif [[ $ASKWEBMIN =~ ^[nN]$ ]]; then
        flag2=1
        dnf -y remove webmin
        
        firewall-cmd --remove-port 10000/tcp --permanent
        firewall-cmd --reload

        echo -e "$green[eFa-NG]$clean Webmin is now disabled"
        pause
      elif [[ $ASKWEBMIN =~ ^[cC]$ ]]; then
        flag2=1
        echo -e "$green[eFa-NG]$clean No action taken"
        pause
      else
        echo -e "Choice $green\"$ASKWEBMIN\"$clean is not a valid choice."
        echo -e ""
      fi
  done
}

# +----------------------------------------------
