#!/bin/bash
#
# This is an sdm plugin for: wsdd
#
# ** Only needed to install wsdd on buster and bullseye. Next major Debian release will apparently have wsdd in apt
#
# 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 Plugin
#
phase=$1
pfx="$(basename $0)"     #For messages
args="$2"
vargs="|wsddswitches|localsrc|"
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"

    wsddswitches=""
    plugin_getargs $pfx "$args" "$vargs" || exit
    plugin_printkeys

    if [ ${raspiosver} -ge 12 ]
    then
	logtoboth "% Plugin $pfx: wsdd is available via apt in this RasPiOS release"
	logtoboth "  This plugin will install it in Phase 1"
	exit 0
    fi

    if [ "$localsrc" != "" ]
    then
	logtoboth "> Plugin $pfx: Copy wsdd from '$localsrc'"
	cp -a $localsrc/wsdd.py $SDMPT/usr/bin/wsdd
	setfileownmode $SDMPT/usr/bin/wsdd 755
	cp -a $localsrc/wsdd.8 $SDMPT/usr/share/man/man8/wsdd.i
	cp -a $localsrc/wsdd.defaults $SDMPT/etc/default/wsdd
	cp -a $localsrc/wsdd.service $SDMPT/etc/systemd/system/wsdd.service
    else
	logtoboth "> Plugin $pfx: download wsdd from GitHub"

	logtoboth "   curl -L https://github.com/christgau/wsdd/raw/master/src/wsdd.py -o $SDMPT/usr/bin/wsdd"
	curl -L https://github.com/christgau/wsdd/raw/master/src/wsdd.py -o $SDMPT/usr/bin/wsdd

	logtoboth "   curl -L https://github.com/christgau/wsdd/raw/master/man/wsdd.8 -o $SDMPT/usr/share/man/man8/wsdd.8"
	curl -L https://github.com/christgau/wsdd/raw/master/man/wsdd.8 -o $SDMPT/usr/share/man/man8/wsdd.8

	logtoboth "   curl -L https://github.com/christgau/wsdd/raw/master/etc/systemd/wsdd.defaults -o $SDMPT/etc/default/wsdd"
	curl -L https://github.com/christgau/wsdd/raw/master/etc/systemd/wsdd.defaults -o $SDMPT/etc/default/wsdd

	logtoboth "   curl -L https://github.com/christgau/wsdd/raw/master/etc/systemd/wsdd.service -o $SDMPT/etc/systemd/system/wsdd.service"
	curl -L https://github.com/christgau/wsdd/raw/master/etc/systemd/wsdd.service -o $SDMPT/etc/systemd/system/wsdd.service
    fi
    chmod 755 $SDMPT/usr/bin/wsdd
    logtoboth "> Plugin $pfx: Configure '/etc/default/wsdd'"
    sed -i "s/WSDD_PARAMS=\"\"/WSDD_PARAMS=\"--user wsdd:wsdd --shortlog $wsddswitches\"/" $SDMPT/etc/default/wsdd
    logtoboth "* Plugin $pfx: Complete Phase 0"

elif [ "$phase" == "1" ]
then
    #
    # Phase 1 (in nspawn)
    #
    logtoboth "* Plugin $pfx: Start Phase 1"
    if [ ${raspiosver} -ge 12 ]
    then
	logtoboth "> Plugin $pfx: Install wsdd"
	installpkgsif wsdd
	exit 0
    else
	logtoboth "* Plugin $pfx: Add unprivileged user 'wsdd' in group 'wsdd'"
	groupadd --system wsdd
	useradd --no-create-home --system --gid wsdd wsdd
	logtoboth "* Plugin $pfx: Enable wsdd service"
	systemctl enable wsdd > /dev/null 2>&1
	logtoboth "* Plugin $pfx: Complete Phase 1"
    fi
else
    #
    # Plugin Post-install edits
    #
    logtoboth "* Plugin $pfx: Start Phase post-install"

    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
