Oy gevalt you goys (-_-") I wasted half the day trying to figure this out. I was using all kinds of dhcp servers and proxy servers and made like 3 templates for config files. Wrote 300+ lines of bash and basically wasted my time.
Well, i now know how not to do it. Turns out you get a similar effect with a relatively simple iptables config, and it also turns out that nmcli, the basic ubuntu network manager has an option where you can specify where your PAC script is. No DHCP needed.
I'll probably just refactor and simplify my shell script. But i've had enough crash course *nix networking for now.