THIS_JAVA='java32-6-jdk'
NAME='32-bit JDK 6'

all_off="$(tput sgr0)"
bold="${all_off}$(tput bold)"
blue="${bold}$(tput setaf 4)"
yellow="${bold}$(tput setaf 3)"

note() {
    printf "${blue}::${yellow} archlinux-java32:${bold} $1${all_off}\n"
}

update_desktop() {
    update-desktop-database -q
    update-mime-database /usr/share/mime >/dev/null
    xdg-icon-resource forceupdate &>/dev/null
}

post_install() {
    default=$(archlinux-java32 get)
    case ${default} in
        "")
            archlinux-java32 set ${THIS_JAVA}
            note "${NAME} ('${THIS_JAVA}') set as default Java environment."
            ;;
        ${THIS_JAVA})
            note "${NAME} ('${THIS_JAVA}') set as default Java environment."
            ;;
        *)
            note "Default Java environment already set to '${default}'.
                     To use ${NAME} ('${THIS_JAVA}'), run:
                       # archlinux-java32 set ${THIS_JAVA}
                     For status, use:
                       $ archlinux-java32 status"
            ;;
    esac

    if [[ ! -f /etc/ssl/certs/java/cacerts ]]; then
        update-ca-trust
    fi

    update_desktop
}

post_upgrade() {
    post_install
}

pre_remove() {
    default=$(archlinux-java32 get)
    if [[ ${default} = ${THIS_JAVA} ]]; then
        archlinux-java32 unset
    fi
}

post_remove() {
    default=$(archlinux-java32 get) # reassign
    if [[ ${default} ]]; then
        note "Default Java environment is '${default}'."
    else
        archlinux-java32 fix
        default=$(archlinux-java32 get) # re-reassign
        if [[ ${default} ]]; then
            note "Default Java environment left as '${default}'."
        else
            note "Java environment unset."
        fi
    fi

    update_desktop
}