#!/bin/bash

BUNDLE_PACKAGE_PATH="../Resources/Packages/Bundle/Install Tuxera NTFS.mpkg"
FLAT_PACKAGE_PATH="../Resources/Packages/Flat/Install Tuxera NTFS.mpkg"

OS_MAJOR_VERSION="$(uname -r | cut -d . -f 1)"
OS_MIDDLE_VERSION="$(uname -r | cut -d . -f 2)"
OS_MINOR_VERSION="$(uname -r | cut -d . -f 3)"

if [ "${OS_MAJOR_VERSION}" -gt 11 ]; then
    PACKAGEPATH="$(dirname "$0")/${FLAT_PACKAGE_PATH}"
elif [ "${OS_MAJOR_VERSION}" -eq 11 ]; then
    if [ "${OS_MIDDLE_VERSION}" -gt 4 ]; then
        PACKAGEPATH="$(dirname "$0")/${FLAT_PACKAGE_PATH}"
    elif ( [ "${OS_MIDDLE_VERSION}" -eq 4 ] && [ "${OS_MINOR_VERSION}" -ge 2 ] ); then
        PACKAGEPATH="$(dirname "$0")/${FLAT_PACKAGE_PATH}"
    else
        PACKAGEPATH="$(dirname "$0")/${BUNDLE_PACKAGE_PATH}"
    fi
else
    PACKAGEPATH="$(dirname "$0")/${BUNDLE_PACKAGE_PATH}"
fi

open "${PACKAGEPATH}"
OPEN_RESULT=$?
if [ ${OPEN_RESULT} -ne 0 ]; then
    echo "Error ${OPEN_RESULT} while launching Tuxera NTFS for Mac installer package (\"${PACKAGEPATH}\")."

    # Display graphical dialog in the Finder.
    osascript - "Error ${OPEN_RESULT} while launching Tuxera NTFS for Mac installer package (\"${PACKAGEPATH}\")." <<-"EOF" > /dev/null &
      on run argv
        tell application "Finder" to display dialog item 1 of argv
      end run
EOF
fi
