# +---------------------------------------------------+
# Function to set the new IP settings
# +---------------------------------------------------+
func_setipsettings(){

  # Back up hosts file, just in case
  if [[ -f /etc/hosts.bak ]]; then
    rm -f /etc/host.bak
    cp -f /etc/hosts /etc/hosts.bak
  else
    cp -f /etc/hosts /etc/hosts.bak
  fi

  # Write new hosts file
  echo "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4" > /etc/hosts
  echo "::1         localhost localhost.localdomain localhost6 localhost6.localdomain6" >> /etc/hosts
  if [[ -n $IP4 ]]; then
    echo "$IP4    $HOSTNAME.$DOMAINNAME   $HOSTNAME" >> /etc/hosts
  fi
  if [[ -n $IP6 ]]; then
    echo "$IP6    $HOSTNAME.$DOMAINNAME   $HOSTNAME" >> /etc/hosts
  fi

  # Enable or disable ipv6 dns
  if [[ "$IPV6DNS" == "yes" ]]; then
    sed -i '/^\tdo-ip6:/ c\\t# do-ip6: yes' /etc/unbound/unbound.conf
  else
    sed -i '/^\t# do-ip6:/ c\\tdo-ip6: no' /etc/unbound/unbound.conf
  fi

  # Write resolv file
  # new unbound stuff... 
  if [[ "$RECURSIVEDNS" == "DISABLED" ]]; then
    echo "forward-zone:" > /etc/unbound/conf.d/forwarders.conf
    echo '  name: "."' >> /etc/unbound/conf.d/forwarders.conf
    [[ -n $DNS1 ]] && echo "  forward-addr: $DNS1" >> /etc/unbound/conf.d/forwarders.conf
    [[ -n $DNS2 ]] && echo "  forward-addr: $DNS2" >> /etc/unbound/conf.d/forwarders.conf
  else
    echo "forward-zone:" > /etc/unbound/conf.d/forwarders.conf
    echo '  name: "."' >> /etc/unbound/conf.d/forwarders.conf
    echo "  forward-first: yes" >> /etc/unbound/conf.d/forwarders.conf
  fi

  NAME=$(nmcli -t c show --active | grep ":$INTERFACE" | grep "802-3-ethernet" | awk -F':' '{print $1}')

  # Set ip settings
  if [[ "$IPV4METHOD" == "auto" ]]; then
    nmcli connection modify $NAME ipv4.method "auto" ipv4.address "" ipv4.gateway "" ipv4.dns "127.0.0.1"
    IP4=""
    NM4=""
    GW4=""
  else
    nmcli connection modify $NAME ipv4.method "manual"
  fi
  
  if [[ "$IPV6METHOD" == "auto" ]]; then
    nmcli connection modify $NAME ipv6.method "auto" ipv6.address "" ipv6.gateway "" ipv6.dns "::1"
    IP6=""
    PF6=""
    GW6=""
  elif [[ $IPV6METHOD == "ignore" ]]; then
    nmcli connection modify $NAME ipv6.method "ignore" ipv6.address "" ipv6.gateway ""
    IP6=""
    PF6=""
    GW6=""
  else 
    nmcli connection modify $NAME ipv6.method "manual"
  fi

  if [[ -n $IP4 ]]; then
    nmcli connection modify $NAME ipv4.address "$IP4/$NM4" ipv4.gateway "$GW4" ipv4.dns "127.0.0.1"
  fi

  if [[ -n $IP6 ]]; then
    nmcli connection modify $NAME ipv6.address "$IP6/$PF6" ipv6.gateway "$GW6" ipv6.dns "::1"
  fi

  # Write change to eFa-Config
  sed -i "/^IPV4ADDRESS:/ c\IPV4ADDRESS:$IP4" /etc/eFa/eFa-Config
  sed -i "/^IPV4NETMASK:/ c\IPV4NETMASK:$NM4" /etc/eFa/eFa-Config
  sed -i "/^IPV4GATEWAY:/ c\IPV4GATEWAY:$GW4" /etc/eFa/eFa-Config
  sed -i "/^IPV6ADDRESS:/ c\IPV6ADDRESS:$IP6" /etc/eFa/eFa-Config
  sed -i "/^IPV6MASK:/ c\IPV6MASK:$PF6" /etc/eFa/eFa-Config
  sed -i "/^IPV6GATEWAY:/ c\IPV6GATEWAY:$GW6" /etc/eFa/eFa-Config
  sed -i "/^DNSIP1:/ c\DNSIP1:$DNS1" /etc/eFa/eFa-Config
  sed -i "/^DNSIP2:/ c\DNSIP2:$DNS2" /etc/eFa/eFa-Config
  sed -i "/^IPV6DNS:/ c\IPV6DNS:$IPV6DNS" /etc/eFa/eFa-Config
  sed -i "/^RECURSION:/ c\RECURSION:$RECURSIVEDNS" /etc/eFa/eFa-Config

  # Grab current FQDN
  OLDHOSTNAME=$(grep "^HOSTNAME" /etc/eFa/eFa-Config | sed 's/^.*://')
  OLDDOMAINNAME=$(grep "^DOMAINNAME" /etc/eFa/eFa-Config | sed 's/^.*://')

  # Detect hostname/domainname change
  if [[ $OLDHOSTNAME != $HOSTNAME || $OLDDOMAINNAME != $DOMAINNAME ]]; then
    echo "$HOSTNAME.$DOMAINNAME" > /etc/hostname

    # Change mailscanner config
    sed -i "/^define('MAILWATCH_FROM_ADDR',/ c\define('MAILWATCH_FROM_ADDR', 'no-reply@$HOSTNAME.$DOMAINNAME');" /var/www/html/mailscanner/conf.php

    # Issue 195 -- Update MySQL database hostname fields
    MAILWATCHPWD=$(grep "^MAILWATCHSQLPWD" /etc/eFa/MailWatch-Config | sed 's/^.*://')

    systemctl stop msmilter
    systemctl stop mailscanner

    mysql -u mailwatch --password=$MAILWATCHPWD mailscanner -e "\
    LOCK TABLES maillog WRITE; \
    ALTER TABLE maillog MODIFY COLUMN \`last_update\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP; \
    UPDATE maillog SET hostname='$HOSTNAME.$DOMAINNAME' WHERE hostname='$OLDHOSTNAME.$OLDDOMAINNAME'; \
    ALTER TABLE maillog MODIFY COLUMN \`last_update\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; \
    LOCK TABLES mtalog WRITE; \
    UPDATE mtalog SET host='$HOSTNAME' WHERE host='$OLDHOSTNAME'; \
    UNLOCK TABLES;"

    postconf -e "error_notice_recipient = root@\$myhostname"

    # Issue #155 Change domainname in opendmarc reports
    DBHOST='localhost'
    DBUSER='opendmarc'
    DBPASS="`grep ^DMARCSQLPWD /etc/eFa/openDMARC-Config | sed 's/^.*://'`"
    DBNAME='opendmarc'
    DMARCRepDomain="`grep DOMAINNAME /etc/eFa/eFa-Config | sed 's/^.*://'`"
    DMARCRepEmail="no-reply@$DMARCRepDomain"
    /usr/sbin/opendmarc-reports -dbhost=${DBHOST} -dbuser=${DBUSER} -dbpasswd=${DBPASS} -dbname=${DBNAME} -interval=86400 -report-email $DMARCRepEmail -report-org $DMARCRepDomain -smtp-server=127.0.0.1

    sed -i "/^HOSTNAME:/ c\HOSTNAME:$HOSTNAME" /etc/eFa/eFa-Config
    sed -i "/^DOMAINNAME:/ c\DOMAINNAME:$DOMAINNAME" /etc/eFa/eFa-Config

    # Change hostname in apache
    sed -i "/^ServerName / c\ServerName $HOSTNAME.$DOMAINNAME:80"  /etc/httpd/conf/httpd.conf
    sed -i "/^ServerName / c\ServerName $HOSTNAME.$DOMAINNAME:443" /etc/httpd/conf.d/ssl.conf

    echo -e ""
    echo -e "Hostname or domainname changed.  Note that you may need to generate new SSL certificates to match the new hostname"
    echo -e ""
    echo -e "$red [eFa] Your system will now reboot. Press any key to continue. $clean"
    pause
    reboot
  fi

  nmcli c down $INTERFACE
  nmcli c up $INTERFACE
  systemctl restart unbound

  echo -e "All done"
  pause
}
# +---------------------------------------------------+