Run through shellcheck.net. Untested as of yet.

The most important thing that I don't know if it works is where I added quotes to ${adapter} (in three places).