#!/bin/sh
set -e

# Upstream's go.mod declares the import path github.com/alecthomas/kingpin/v2,
# which this package has adopted since 2.4.0-1. Previous versions shipped the
# source under gopkg.in/alecthomas/kingpin.v2 and provided the github.com path
# only as a compat symlink pointing into that tree:
#
#   /usr/share/gocode/src/github.com/alecthomas/kingpin -> ../../gopkg.in/alecthomas/kingpin.v2
#
# Since 2.4.0-1 the github.com path is a real directory holding the package
# files. On upgrade dpkg would unpack the new files *through* the still-present
# compat symlink, silently placing them inside the gopkg.in tree (piuparts:
# "installs objects over existing directory symlinks"). Remove the stale
# symlink here, before unpacking, so dpkg creates a real directory instead.
case "$1" in
    upgrade)
        link=/usr/share/gocode/src/github.com/alecthomas/kingpin
        # Only drop the symlink the old package shipped; leave any other
        # directory or symlink the admin may have placed here alone.
        if [ -L "$link" ]; then
            case "$(readlink "$link")" in
                */gopkg.in/alecthomas/kingpin.v2)
                    rm -f "$link"
                    ;;
            esac
        fi
        ;;
esac

#DEBHELPER#

exit 0
