# arg 1:  the new package version
# arg 2:  the old package version

KERNEL_NAME=-raspberrypi
KERNEL_VERSION=4.9.11-1-ARCH

disable_cma() {
  if [[ -f /boot/config.txt && $(grep '^cma' /boot/config.txt) != '' ]]; then
    echo ">>> You appear to have dynamic memory (CMA) enabled, which is currently broken"
    echo ">>>   in newer kernels and will cause many issues if left enabled."
    echo ">>> Disabling that for you now."
    sed -i 's/^cma_/#cma_/g' /boot/config.txt
  fi
}

post_install () {
  # updating module dependencies
  echo ">>> Updating module dependencies. Please wait ..."
  depmod ${KERNEL_VERSION}
  disable_cma
}

post_upgrade() {
  if grep "^[^#]*[[:space:]]/boot" etc/fstab 2>&1 >/dev/null; then
    if ! grep "[[:space:]]/boot" etc/mtab 2>&1 >/dev/null; then
      echo "WARNING: /boot appears to be a seperate partition but is not mounted."
      echo "         You probably just broke your system. Congratulations."
    fi
  fi

  # updating module dependencies
  echo ">>> Updating module dependencies. Please wait ..."
  depmod ${KERNEL_VERSION}

  disable_cma

  if [ "$(vercmp $2 3.18.3)" -lt 0 ]; then
    echo "________________________________________________________________________________"
    echo ""
    echo "WARNING: This kernel provides updated config.txt and cmdline.txt files."
    echo "         Check for pacnew files in /boot and merge or replace the existing files"
    echo "         with the new versions."
    echo "________________________________________________________________________________"
    echo ""
  fi
}

post_remove() {
  rm -f boot/initramfs-linux.img
}