+
+[[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
+
+x="$(readlink -f "$BASH_SOURCE")"; source "${x%/*}/bash-trace"
+
+
+usage() {
+ cat <<EOF
+Usage: ${0##*/} [OPTIONS] [HOST] [TYPE]
+Configure dnsmasq boot options and fai-chboot if appropriate. This is
+not general purpose, it has code specific to dhcp servers I run.
+
+Without TYPE, disable server and fai server. In that case, HOST is only
+needed for fsf office network.
+
+HOST A hostname known to the dhcp server, or default for all.
+TYPE One of arch, parabola, plain, fai.
+
+-a Don't setup pxe, just Wait for 2 dhcp acks, then disable the pxe
+ server after a delay. First ack is for pxe boot, 2nd ack is
+ for os boot. Sometimes on debian, there is a 3rd one shortly
+ after the 2nd. I can't remember exactly why this caused a
+ problem, but I'm hoping the sleep will take care of it.
+-d Don't alter dhcp config. Only make sense for fai type, and on network
+ other than home or fsf.
+-k Pass -k to myfai-chboot.
+-r Don't redeploy fai config. For example, if there is a different host
+ that is mid-install.
+
+-S sets FAI_ACTION=sysinfo, see myfai-chboot for more info.
+-w Setup pxe, then wait like -a.
+-h|--help Print help and exit
+
+
+Note, when switching between plain and arch or parabola, you will need to
+do something like:
+ssh wrt
+cd /mnt/usb
+rm tftpboot
+ln -s <arch/parabola/debian iso dir> tftpboot
+
+
+Notes on debugging pxe dhcp tftp:
+
+For debugging dhcp, add to /etc/dnsmasq.conf: log-dhcp
+
+Newer openwrt runs dnsmasq with a whitelist of readable files and dirs:
+
+ps ww :
+/sbin/ujail -t 5 -n dnsmasq -u -l -r /bin/ubus -r /etc/TZ -r /etc/dnsmasq.conf -r /etc/ethers -r /etc/group -r /etc/hosts -r /etc/passwd -w /tmp/dhcp.leases -r /tmp/dnsmasq.d -r /tmp/hosts -r /tmp/resolv.conf.d -r /usr/bin/jshn -r /usr/lib/dnsmasq/dhcp-script.sh -r /usr/share/dnsmasq/dhcpbogushostname.conf -r /usr/share/dnsmasq/rfc6761.conf -r /usr/share/dnsmasq/trust-anchors.conf -r /usr/share/libubox/jshn.sh -r /var/etc/dnsmasq.conf.cfg01411c -w /var/run/dnsmasq/ -- /usr/sbin/dnsmasq -C /var/etc/dnsmasq.conf.cfg01411c -k -x /var/run/dnsmasq/dnsmasq.cfg01411c.pid
+
+logging tftp requests:
+/etc/default/tftpd-hpa:
+add -vv:
+TFTP_OPTIONS="--secure -vv"
+jr -u tftpd-hpa -f
+
+Note: Uses GNU getopt options parsing style
+EOF
+ exit $1
+}
+
+pre="${0##*/}:"
+m() { printf "$pre %s\n" "$*"; "$@"; }
+e() { printf "$pre %s\n" "$*"; }
+err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
+
+PATH="/a/exe:$PATH"
+
+##### begin command line parsing ########
+
+dhcp=true
+redep=true
+acks=2
+wait=false
+fsf=false
+
+case $HOSTNAME in
+ x3|kw) fsf=true ;;
+esac
+
+chboot_args=()
+temp=$(getopt -l help adkrSwh "$@") || usage 1
+eval set -- "$temp"
+while true; do
+ case $1 in
+ -a) wait=true; set=false; shift ;;
+ -d) dhcp=false; shift ;;
+ -k) chboot_args+=(-k); shift ;;
+ -r) redep=false; shift ;;
+ -S) chboot_args+=(-S); shift ;;
+ -w) wait=true; set=true; shift ;;
+ -h|--help) usage ;;
+ --) shift; break ;;
+ *) echo "$0: Internal error!" ; exit 1 ;;
+ esac
+done
+
+read -r host type <<<"$@"
+
+case $# in
+ [01]);;
+ 2)
+ case $type in
+ arch|parabola) cmd=archlike ;;
+ fai) cmd=fai ;;
+ *)
+ echo "$0: error expected type of arch|parabola|fai"
+ echo
+ usage 1
+ ;;
+ esac
+ ;;
+ *)
+ echo "$0: error: expected 0-2 arguments"
+ echo
+ usage 1
+ ;;
+esac
+
+
+if $wait && ! $dhcp; then
+ echo "$0: error -w conflicts with -d, choose one or other"
+ exit 1
+fi
+
+
+if [[ $host && $host != default ]]; then
+ host_tag="tag:$host,"