Linux default gateway failover

I have an “unreliable” (I sometimes just take it with me) mifi box with fast LTE connection, shared to internal network via separate gateway, and a reliable but slow ADSL connection. I use the following script to switch to the slower connection when the default one is not working and automatically switch back when possible. […]