#!/bin/sh

. $IPKG_INSTROOT/lib/functions.sh

[ -x "$(command -v dockerd)" ] && chmod +x /etc/init.d/dockerman && /etc/init.d/dockerman enable >/dev/null 2>&1
sed -i 's/self:cfgvalue(section) or {}/self:cfgvalue(section) or self.default or {}/' /usr/lib/lua/luci/view/cbi/dynlist.htm
/etc/init.d/uhttpd restart >/dev/null 2>&1
rm -fr /tmp/luci-indexcache /tmp/luci-modulecache >/dev/null 2>&1
touch /etc/config/dockerd
ls /etc/rc.d/*dockerd &> /dev/null && uci -q set dockerd.globals.auto_start="1" || uci -q set dockerd.globals.auto_start="0"
uci -q batch <<-EOF >/dev/null
	set uhttpd.main.script_timeout="3600"
	commit uhttpd
	set dockerd.dockerman=dockerman
	set dockerd.dockerman.socket_path='/var/run/docker.sock'
	set dockerd.dockerman.status_path='/tmp/.docker_action_status'
	set dockerd.dockerman.debug='false'
	set dockerd.dockerman.debug_path='/tmp/.docker_debug'
	set dockerd.dockerman.remote_endpoint='0'

	del_list dockerd.dockerman.ac_allowed_interface='br-lan'
	add_list dockerd.dockerman.ac_allowed_interface='br-lan'

	commit dockerd
EOF
# remove dockerd firewall
config_load dockerd
remove_firewall(){
	cfg=${1}
	uci_remove dockerd ${1}
}
config_foreach remove_firewall firewall
# Convert ac_allowed_container to ac_allowed_ports
(sleep 30s && /etc/init.d/dockerman convert;/etc/init.d/dockerman restart) &

/etc/init.d/dockerd restart &
exit 0
