#!/bin/sh -e

. /usr/share/debconf/confmodule

chmod a+x /var/lib/portmaster/portmaster-start

log() {
    echo "\e[1mportmaster: \e[0m $@"
}

print_dl_help() {
    log ""
    log "\e[33;1mWARN: $1\e[0m"
    log "downloading modules can be initiated by starting the Portmaster service or by running:"
    log "/var/lib/portmaster/portmaster-start --data=/var/lib/portmaster update"
    log ""
}

#
# Skip downloading updates if there's already
# stable.json available.
#
if [ ! -f "/var/lib/portmaster/updates/stable.json" ]
then
    if [ -z "${PM_SKIP_DOWNLOAD}" ]
    then
        log "downloading modules, this may take a while"
        PMSTART_UPDATE_AGENT=${PMSTART_UPDATE_AGENT:=Start}
        /var/lib/portmaster/portmaster-start --data=/var/lib/portmaster update --update-agent ${PMSTART_UPDATE_AGENT}  || \
            (
                print_dl_help "Downloading modules failed!"
                log "installation successfull"
            )
    else
        print_dl_help "skipped downloading modules!"
    fi
fi

# with 0.4.0 portmaster-control has ben renamed to portmaster-start
# and is not placed into /usr/bin anymore.
rm /usr/bin/portmaster-control 2>/dev/null >&2 || true

#DEBHELPER#
