#!/bin/bash

set -e

# source debconf library
. /usr/share/debconf/confmodule

function maybe_add_hooks_symlink() {
  if [ -e /etc/nvidia-mig-manager/hooks.yaml ]; then
    return
  fi

  if ! which nvidia-smi > /dev/null 2>&1; then
    ln -s hooks-default.yaml /etc/nvidia-mig-manager/hooks.yaml
    return
  fi

  local compute_cap=$(nvidia-smi -i 0 --query-gpu=compute_cap --format=csv,noheader)
  if [ "${compute_cap/./}" -ge "90" ] 2> /dev/null; then
    ln -s hooks-minimal.yaml /etc/nvidia-mig-manager/hooks.yaml
  else
    ln -s hooks-default.yaml /etc/nvidia-mig-manager/hooks.yaml
  fi
}

function maybe_add_config_symlink() {
  if [ -e /etc/nvidia-mig-manager/config.yaml ]; then
    return
  fi
  ln -s config-default.yaml /etc/nvidia-mig-manager/config.yaml
}

case "${1}" in
  configure)
    maybe_add_hooks_symlink
    maybe_add_config_symlink
  ;;

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

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

esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

db_stop

exit 0
