description "cjdns dynamic DNS peer resolver startup script"

author "Adam Novak <interfect@gmail.com>"
# Based on cjsdns Upstart config by Sergey Davidoff <shnatsel@gmail.com>
# Requires that you be using Upstart to control cjdns

start on started cjdns
stop on stopping cjdns

# respawn if it crashes, but no more than once in 180 seconds
respawn
respawn limit 2 300

pre-start script
    if ! [ -e /etc/cjdns-dynamic.conf ]; then
        ( # start a subshell to avoid side effects of umask later on
            umask 077 # to create the file with 600 permissions without races

            # Make an empty config file; nodes to contact go here.
            touch /etc/cjdns-dynamic.conf

            # By default we install a config, so this only happens if the user
            # deletes it
        ) # exit subshell; umask no longer applies
        echo 'WARNING: A new cjdns-dynamic.conf file has been generated.'
    fi

    if ! [ -e /etc/cjdns-dynamic.cjdnsadmin ]; then
        ( # start a subshell to avoid side effects of umask later on
            umask 077 # to create the file with 600 permissions without races

            # Make a cjdnsadmin file from the cjdroute.conf. We're not using
            # /root/.cjdnsadmin because that would be silly.

            # TODO: Make the cjdns Upstart job put something in /var that other
            # root processes can read.
            /usr/lib/cjdns/cjdnsadminmaker.py /etc/cjdns-dynamic.cjdnsadmin
        ) # exit subshell; umask no longer applies
        echo 'WARNING: A new cjdns-dynamic.cjdnsadmin file has been generated.'
    fi
end script

exec /usr/lib/cjdns/dynamicEndpoints.py /etc/cjdns-dynamic.conf --adminInfo /etc/cjdns-dynamic.cjdnsadmin
