#!/bin/bash
#
# This is an sdm plugin for: apt-file
#
# The plugin is called three times: for Phase 0, Phase 1, and post-install.
#

function loadparams() {
    source $SDMPT/etc/sdm/sdm-readparams
}

# $1 is the phase: "0", "1", or "post-install"
# $2 is the argument list: arg1=val1:arg2=val2:arg3=val3: ...

#
# Main code for the script
#
phase=$1
pfx="$(basename $0)"     #For messages
loadparams

if [ "$phase" == "0" ]
then
    #
    # In Phase 0 all references to directories in the image must be preceded by $SDMPT
    #
    logtoboth "* Plugin $pfx: Start Phase 0"

# INSERT Plugin Phase 0 code here
    
    logtoboth "* Plugin $pfx: Complete Phase 0"

elif [ "$phase" == "1" ]
then
    #
    # Phase 1 (in nspawn)
    #
    logtoboth "* Plugin $pfx: Start Phase 1"
    #logfreespace "at start of Plugin $pfx Phase 1"
    #
    logtoboth "> Plugin $pfx: Install apt-file"
    installpkgsif apt-file
    #
    #logfreespace "at end of $pfx Phase 1"
    logtoboth "* Plugin $pfx: Complete Phase 1"
else
    #
    # Plugin Post-install edits
    #
    logtoboth "* Plugin $pfx: Start Phase post-install"
    #logfreespace "at start of Plugin $pfx Phase post-install"
    #
    logtoboth "> Plugin $pfx: Generate apt-file database"
    if type -P apt-file > /dev/null
    then
	doapt update $showapt apt-file || logapterror
    else
	logtobothex "? Plugin $pfx: apt-file appears to not be installed"
    fi

    #
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
