#!/bin/bash
#
# This is an sdm plugin for: apt-cacher-ng
#
# 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
args="$2"
vldargs="|gentargetmode|bindaddress|cachedir|port|tunnelenable|proxy|"
rqdargs=""
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"
    plugin_getargs $pfx "$args" "$vldargs" "$rqdargs" || exit

# 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"
    #
    plugin_getargs $pfx "$args" "|gentargetmode|bindaddress|cachedir|port|tunnelenable|proxy|"
    plugin_printkeys

    #  Possible choices FOR gentargetmode: Set up once, Set up now and update later, No automated setup
    #  Other settings: "keep" keeps the value from the default install

    [ "$gentargetmode" == "" ] && gentargetmode="No automated setup"
    [ "$bindaddress" == "" ] && bindaddress="0.0.0.0"
    [ "$cachedir" == "" ] && cachedir="keep"
    [ "$port" == "" ] && port="keep"
    [ "$tunnelenable" == "" ] && tunnelenable="false"
    [ "$proxy" == "" ] && proxy="keep"

    debconf-set-selections <<< "apt-cacher-ng apt-cacher-ng/gentargetmode string $gentargetmode"
    debconf-set-selections <<< "apt-cacher-ng apt-cacher-ng/bindaddress string $bindaddress"
    debconf-set-selections <<< "apt-cacher-ng apt-cacher-ng/cachedir string $cachedir"
    debconf-set-selections <<< "apt-cacher-ng apt-cacher-ng/port string $port"
    debconf-set-selections <<< "apt-cacher-ng apt-cacher-ng/tunnelenable string $tunnelenable"
    debconf-set-selections <<< "apt-cacher-ng apt-cacher-ng/proxy string $proxy"

    logtoboth "> Plugin $pfx: Install apt-cacher-ng server"
    logtoboth "> Plugin $pfx:   With arguments:"
    logtoboth "> Plugin $pfx:                gentargetmode: $gentargetmode"
    logtoboth "> Plugin $pfx:                bindaddress:   $bindaddress"
    logtoboth "> Plugin $pfx:                cachedir:      $cachedir"
    logtoboth "> Plugin $pfx:                port:          $port"
    logtoboth "> Plugin $pfx:                tunnelenable:  $tunnelenable"
    logtoboth "> Plugin $pfx:                proxy:         $proxy"
    installpkgsif apt-cacher-ng

    #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"
    #
    plugin_getargs $pfx "$args" "$vldargs" "$rqdargs"
    plugin_printkeys

    #  Possible choices FOR gentargetmode: Set up once, Set up now and update later, No automated setup
    #  Other settings: "keep" keeps the value from the default install

    [ "$gentargetmode" == "" ] && gentargetmode="No automated setup"
    [ "$bindaddress" == "" ] && bindaddress="keep"
    [ "$cachedir" == "" ] && cachedir="keep"
    [ "$port" == "" ] && port="keep"
    [ "$tunnelenable" == "" ] && tunnelenable="false"
    [ "$proxy" == "" ] && proxy="keep"

    logtoboth "> Plugin $pfx: Set apt-cacher-ng to enable and enable apt to use it (127.0.0.1) after sdm FirstBoot"
    acs01="/etc/sdm/0piboot/099-enable-apt-cacher-ng.sh"
    cat > $acs01 <<EOF
#!/bin/bash
rm -f /etc/apt/apt.conf.d/02proxy
echo "Acquire::http::proxy \"http://127.0.0.1:3142\";" >> /etc/apt/apt.conf.d/02proxy
systemctl enable apt-cacher-ng > /dev/null 2>&1
EOF

    logtoboth "> Plugin $pfx: Create cacher reset script /usr/local/bin/reset-apt-cacher"
    cat > /usr/local/bin/reset-apt-cacher <<EOF
#!/bin/bash

echo "Stop apt-cacher-ng"
sudo systemctl stop apt-cacher-ng
echo "Clear apt-cacher-ng cache"
sudo rm -rf /var/cache/apt-cacher-ng
echo "Create apt-cacher-ng directory and set ownership"
sudo mkdir -p /var/cache/apt-cacher-ng
sudo chown -R apt-cacher-ng:apt-cacher-ng /var/cache/apt-cacher-ng
echo "Start apt-cacher-ng"
sudo systemctl start apt-cacher-ng
echo "Done"
EOF
    setfileownmode /usr/local/bin/reset-apt-cacher 0755
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
