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

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

function converty() {
    [ -v enablesvc ] && enablesvc=y
}

# $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="|enablesvc|workgroup|dhcp|do_debconf|shares|smbconf|"
loadparams
assetdir="$SDMPT/etc/sdm/assets/samba"

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" || exit
    plugin_printkeys

    mkdir -p $assetdir

    if [ "$smbconf" != "" ]
    then
	if [ ! -f $smbconf ]
	then
	    logtobothex "? Plugin $pfx: Cannot find samba smbconf file '$smbconf'"
	else
	    logtoboth "> Plugin $pfx: Copy samba smbconf '$smbconf' into IMG $assetdir/$(basename $smbconf)"
	    rm -f $assetdir/$(basename $smbconf)
	    cp -a $smbconf $assetdir/$(basename $smbconf)
	fi
    fi
    if [ "$shares" != "" ]
    then
	if [ ! -f $shares ]
	then
	    logtoboth "% Plugin $pfx: Cannot find samba shares file '$shares'; ignoring"
	else
	    logtoboth "> Plugin $pfx: Copy samba shares file '$shares' into IMG $assetdir/$(basename $shares)"
	    rm -f $assetdir/$(basename $shares)
	    cp -a $shares $assetdir/$(basename $shares)
	fi
    fi
    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" "$vldargs"
    [ "$workgroup" == "" ] && workgroup="WORKGROUP"
    [ "$dhcp" == "" ] && dhcp="false"
    [ "$do_debconf" = "" ] && do_debconf="true"

    #hostname $thishost  # For some reason, debconf needs a valid hostname (on the network?) V strange
    debconf-set-selections <<< "samba-common samba-common/workgroup string  $workgroup"
    debconf-set-selections <<< "samba-common samba-common/dhcp boolean $dhcp"
    debconf-set-selections <<<  "samba-common samba-common/do_debconf boolean $do_debconf"
    logtoboth "> Plugin $pfx: Install samba"
    installpkgsif samba
    if [ "$smbconf" != "" ]
    then
	if [ -f $assetdir/$(basename $smbconf) ]
	then
	    logtoboth "> Plugin $pfx: Replace /etc/samba/smb.conf with $assetdir/$(basename $smbconf)"
	    mv /etc/samba/smb.conf /etc/samba/smb.conf.sdm
	    cp -a $assetdir/$(basename $smbconf) /etc/samba/smb.conf
	    setfileownmode /etc/samba/smb.conf 600
	fi
    fi
    if [ "$shares" != "" ]
    then
	if [ -f $assetdir/$(basename $shares) ]
	then
	    logtoboth "> Plugin $pfx: Append shares from $assetdir/$(basename $shares) to /etc/samba/smb.conf"
	    cp -a /etc/samba/smb.conf /etc/samba/smb.conf.sdm-shares
	    setfileownmode /etc/samba/smb.conf.sdm-shares 600
	    cat >> /etc/samba/smb.conf <<EOF 

#** Following share definitions added by sdm samba plugin from $shares

EOF
	    cat $assetdir/$(basename $shares) >> /etc/samba/smb.conf
	fi
    fi
    #hostname sdm

    #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"
    [ "$workgroup" == "" ] && workgroup="WORKGROUP"
    [ "$dhcp" == "" ] && dhcp="false"
    [ "$do_debconf" = "" ] && do_debconf="true"

    plugin_addnote ""
    plugin_addnote "*** samba plugin service notes"
    plugin_addnote ""
    plugin_addnote " * Ensure that /etc/samba/smb.conf is as you want it"
    plugin_addnote " * Add users and passwords using 'sudo smbpasswd'"
    logtoboth "> Plugin $pfx: Disable nmbd and samba-ad-dc services; probably not needed"
    systemctl disable -q nmbd
    systemctl disable -q samba-ad-dc
    [ "$enablesvc" == "y" ] && systemctl enable -q smbd
    if [[ "$smbconf" == "" ]] && [[ "$shares" == "" ]]
    then
	systemctl disable smbd > /dev/null 2>&1
	plugin_addnote " * Use 'sudo systemctl enable --now smbd' to enable/start the service when configuration complete"
    else
	plugin_addnote " * The smb service should be enabled when the system boots; review the system logs for possible errors"
    fi
    plugin_addnote ""
    #logfreespace "at end of $pfx Custom Phase post-install"
    logtoboth "* Plugin $pfx: Complete Phase post-install"
fi
