#!/bin/sh

set -eu

command=$1
shift

case "${command}" in
    configure)
        upstream_dir=$1

        ln -fns "$upstream_dir" /opt/next-exam-puavo-teacher
        appimage_path='/opt/next-exam-puavo-teacher/Next-Exam-Puavo-Teacher.AppImage'
        ln -fns "${upstream_dir}/next-exam-puavo-teacher.png" /usr/share/icons/next-exam-puavo-teacher.png
        cat <<EOF > /usr/share/applications/next-exam-puavo-teacher.desktop
[Desktop Entry]
Name=Next-Exam Teacher
Exec=${appimage_path}
Type=Application
Terminal=false
Icon=next-exam-puavo-teacher
StartupNotify=true
Categories=Internet;
EOF

        # A bit ugly because we are changing the group ownership
        # and permissions of a file owned by "puavo-pkg"-user.
        # Do here because this can not be done in the "unpack"-phase.
        chmod 700 "$appimage_path"
        setfacl -m g:puavo-role-admin:rx   "$appimage_path"
        setfacl -m g:puavo-role-teacher:rx "$appimage_path"
        ;;
    unconfigure)
        rm -f /opt/next-exam-puavo-teacher \
              /usr/share/applications/next-exam-puavo-teacher.desktop \
              /usr/share/icons/next-exam-puavo-teacher.png
        ;;
    unpack)
        upstream_pack=$1
        upstream_dir=$2

        cp "$upstream_pack" "${upstream_dir}/Next-Exam-Puavo-Teacher.AppImage"
        chmod 700 "${upstream_dir}/Next-Exam-Puavo-Teacher.AppImage"
        cp -p next-exam-puavo-teacher.png "$upstream_dir"
        ;;
    *)
        ;;
esac
