#!/bin/bash

set -e

OLD_VENDOR_DIR=/usr/share/perl6/vendor

case "$1" in
    configure)
        if [ -d $OLD_VENDOR_DIR ]
        then
            echo "postinst: cleaning up old vendor dir ($OLD_VENDOR_DIR)"
            rm -rf $OLD_VENDOR_DIR
        fi
        do_recompile=true
        if [ -f /var/lib/perl6/previous-compiler-id ]; then
            this_compiler_id=$(/usr/share/perl6/rakudo-helper.pl --compiler-id)
            previous_compiler_id=$(cat /var/lib/perl6/previous-compiler-id)
            if [ "$this_compiler_id" = "$previous_compiler_id" ]; then
                do_recompile=false
            fi
            rm -f /var/lib/perl6/previous-compiler-id
        fi
        if dpkg --compare-versions "$2" lt "2019.07.1-6"; then
            do_recompile=true
        fi

        if [ "$do_recompile" = true ]; then
            echo "  rakudo-helper.pl: Reinstalling all perl6 modules ..." >&2
            /usr/share/perl6/rakudo-helper.pl --reinstall-all
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
        # nothing to do
    ;;

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

#DEBHELPER#

exit 0
