#!/bin/sh

set -e
case "$1" in
    configure)
        # Create a temporary file with the modified configuration
        SMB_CONF="/etc/samba/smb-ogclient.conf"
        OLD_FILE="/etc/samba/smb.conf"
        NEW_FILE=$(mktemp)

        # Ensure the template file exists
        if [ ! -f "$SMB_CONF" ]; then
            echo "Config file missing!"
            exit 1
        fi

        # Check if our share section already exists
        if grep -q "include = /etc/samba/smb-ogclient.conf" "$OLD_FILE"; then
            # Section already exists, do nothing
            rm -f "$NEW_FILE"
        else
            # Copy the original file
            cp -a "$OLD_FILE" "$NEW_FILE"

            # Append our configuration
            echo "include = /etc/samba/smb-ogclient.conf" >> "$NEW_FILE"

            # Use ucf to handle the file update
            ucf --debconf-ok "$NEW_FILE" "$OLD_FILE"

            # Clean up
            rm -f "$NEW_FILE"

            # Reload Samba
            if command -v systemctl >/dev/null 2>&1; then
                systemctl reload smbd.service
            else
                service smbd reload || true
            fi
        fi
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

#DEBHELPER#

exit 0
