#!/bin/bash
# This script can be called in the following ways:
#
# After the package was installed:
#       <postinst> configure <old-version>
#
#
# If prerm fails during upgrade or fails on failed upgrade:
#       <old-postinst> abort-upgrade <new-version>
#
# If prerm fails during deconfiguration of a package:
#       <postinst> abort-deconfigure in-favour <new-package> <version>
#                  removing <old-package> <version>
#
# If prerm fails during replacement due to conflict:
#       <postinst> abort-remove in-favour <new-package> <version>

set -e

function plank_cleanup () {
    # following script cleans up our /etc/skel version of the plank configuration
    # this is now (and correctly) installed as part of the login sequence
    # We delete only the files we initially installed.  It doesnt matter if
    # someone has modified a file - we will be overwriting it anyway when the
    # user logs in for the first time.

    BASE="/etc/skel/.config/plank"
    SUBDIR="/dock1/launchers"
    TEMPLATES=(budgie-welcome.dockitem
    libreoffice-calc.dockitem
    org.gnome.Software.dockitem
    chromium-browser.dockitem
    firefox.dockitem
    libreoffice-writer.dockitem
    rhythmbox.dockitem
    com.gexperts.Terminix.dockitem
    nautilus.dockitem
    com.gexperts.Tilix.dockitem
    org.gnome.Nautilus.dockitem
    )

    for ((i = 0; i < ${#TEMPLATES[@]}; i++))
    do
        filetoremove="$BASE/$SUBDIR/${TEMPLATES[$i]}"
        [ -f "$filetoremove" ] && rm "$filetoremove"
    done

    if [ -d "$BASE" ]; then
        find "$BASE" -type d -empty -delete
    fi
}

function template_cleanup () {
    # following script cleans up our /etc/skel version of the Templates configuration
    # this is now (and correctly) installed as part of the login sequence
    # We delete only the files we initially installed.  It doesnt matter if
    # someone has modified a file - we will be overwriting it anyway when the
    # user logs in for the first time.

    BASE="/etc/skel/Templates"
    TEMPLATES=(Empty\ File
    LibreOffice\ Impress.odp
    MS\ PowerPoint.pptx
    LibreOffice\ Calc.ods
    LibreOffice\ Writer.odt
    MS\ Word.docx
    LibreOffice\ Draw.odg
    MS\ Excel.xlsx
    Text\ File.txt
    )

    for ((i = 0; i < ${#TEMPLATES[@]}; i++))
    do
        filetoremove="$BASE/${TEMPLATES[$i]}"
        [ -f "$filetoremove" ] && rm "$filetoremove"
    done

    if [ -d "$BASE" ]; then
        find "$BASE" -type d -empty -delete
    fi
}

case "$1" in
    configure)

    plank_cleanup
    template_cleanup

    filetoremove="/etc/skel/.config/autostart/budgie-welcome.desktop"
    if [ -f "$filetoremove" ]; then
        rm "$filetoremove"
    fi

    ;;

    abort-upgrade|abort-deconfigure|abort-remove)
    ;;

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

#DEBHELPER#
