#/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
####################################################################
# for configuring /etc/resolv.conf dns
# written by George Liu (eva2000) centminmod.com
# setup google, level3 and comodo secure dns resolvers
####################################################################
DT=$(date +"%d%m%y-%H%M%S")
CENTMINLOGDIR='/root/centminlogs'

####################################################################
resolv_setup() {
  # if detected dhclient-script generated /etc/resolv.conf, then modify it
  if [[ "$(grep -w 'dhclient-script' /etc/resolv.conf)" && -d /etc/dhcp/dhclient.d && ! -f /etc/dhcp/dhclient.d/rotate.sh ]] && [[ ! "$(lsattr /etc/resolv.conf | grep '\-i\-')" ]]; then
cat > "/etc/dhcp/dhclient.d/rotate.sh" <<EOF
rotate_config() {
    echo '# generated by /usr/sbin/dhclient-script' > /etc/resolv.conf
    echo '# generated by centminmod setup' >> /etc/resolv.conf
    echo 'options rotate' >> /etc/resolv.conf
    echo 'options timeout:1' >> /etc/resolv.conf
    echo 'nameserver 208.67.220.220' >> /etc/resolv.conf
    echo 'nameserver 208.67.222.222' >> /etc/resolv.conf
    echo 'nameserver 1.1.1.1' >> /etc/resolv.conf
    echo 'nameserver 1.0.0.1' >> /etc/resolv.conf
    echo 'nameserver 8.8.8.8' >> /etc/resolv.conf
    echo 'nameserver 4.2.2.2' >> /etc/resolv.conf
}

rotate_restore() {
    :
}
EOF
    chmod +x /etc/dhcp/dhclient.d/rotate.sh
cat > "/etc/resolv.conf" <<EFF
# generated by /usr/sbin/dhclient-script
# generated by centminmod setup
options rotate
options timeout:1
nameserver 208.67.220.220
nameserver 208.67.222.222
nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 8.8.8.8
nameserver 4.2.2.2
EFF
  elif [[ ! "$(grep -w 'linode' /etc/resolv.conf)" ]] && [[ ! "$(grep -w '1.0.0.1' /etc/resolv.conf)" || ! "$(grep -w '1.1.1.1' /etc/resolv.conf)" || ! "$(grep -w '208.67.222.222' /etc/resolv.conf)" || ! "$(grep -w '8.8.8.8' /etc/resolv.conf)" || ! "$(grep -w '4.2.2.2' /etc/resolv.conf)" || ! "$(grep -w '208.67.220.220' /etc/resolv.conf)" ]] && [[ ! "$(lsattr /etc/resolv.conf | grep '\-i\-')" ]]; then
    # if not linode server based, update /etc/resolv.conf. linode servers don't need updating as they have a more relaible setup
cat > "/etc/resolv.conf" <<EFF
# generated by centminmod setup
options rotate
options timeout:1
nameserver 208.67.220.220
nameserver 208.67.222.222
nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 8.8.8.8
nameserver 4.2.2.2
EFF
  elif [[ "$(grep -w 'linode' /etc/resolv.conf)" ]] && [[ ! "$(grep -w '8.8.8.8' /etc/resolv.conf)" ]] && [[ ! "$(grep -w '4.2.2.2' /etc/resolv.conf)" || ! "$(grep -w '208.67.222.222' /etc/resolv.conf)" ]] && [[ ! "$(lsattr /etc/resolv.conf | grep '\-i\-')" ]]; then
    # insert 8.8.8.8 to existing linode /etc/resolv.conf configs
cat > "/etc/resolv-tmp.conf" <<EFF
# generated by centminmod setup
options rotate
options timeout:1
EFF
    cat /etc/resolv.conf >> /etc/resolv-tmp.conf
    sed -i '/search .*/a nameserver 8.8.8.8'  /etc/resolv-tmp.conf
    \cp -a /etc/resolv-tmp.conf /etc/resolv.conf
  fi
  cat /etc/resolv.conf
  rm -rf /etc/resolv-tmp.conf
}

{
resolv_setup
} 2>&1 | tee "${CENTMINLOGDIR}/resolv-setup_${DT}.log"