#!/bin/bash

interface=$(echo 'show State:/Network/Global/IPv4' | scutil | grep PrimaryInterface | awk -F': ' '{print $2}')

networkservice=$(networksetup -listnetworkserviceorder | grep -B 1 -m 1 "Device: $interface"| grep -m 1 '([[:digit:]+]) ' | cut -c 5-)

dnsServers=$(networksetup -getdnsservers "$networkservice")
if [[ "$dnsServers" == "There aren't any DNS Servers"*  ]]; then
	echo "No custom DNS servers set; exiting."
	exit 0
fi

newdns=''
for d in $dnsServers; do
	if [ "$d" == '18.70.0.160' ]; then
		converted="18.0.70.160"
	elif [ "$d" == '18.71.0.151' ]; then
		converted="18.0.71.151" 
	elif [ "$d" == '18.72.0.3' ]; then
		converted="18.0.72.3" 
	else
		converted="$d" 
	fi
	newdns="${newdns}$converted "
	if [ "$converted" == "$d" ]; then
		echo $d
	else
		echo $d '->' $converted
	fi
done

networksetup -setdnsservers "$networkservice" $newdns