# get the current operating system
curOS=$(uname -s)
echo " INFO: Current operating system: \"${curOS}\""

# check that common/init/$curOS.sh exists
if [ ! -f "${CurrentMediaDir}/inst/common/init/${curOS}.sh" ]; then
  echo "ERROR: This media cannot be installed on this operating system: '${curOS}'"
  echo "       It can only be installed on the following operating system(s):"
  ls "${CurrentMediaDir}/inst/common/init/" | awk -F. '{ print "       - " $1 }'
  exit 6
fi

# check that common/init/$curOS.sh has the execution permission
if [ ! -x "${CurrentMediaDir}/inst/common/init/${curOS}.sh" ]; then
  echo "ERROR: This media has been unzipped or copied in such a way that the permissions"
  echo "       of some files have been lost. Please unzip or copy the media again."
  exit 61
fi

# get DSY_OS
. "${CurrentMediaDir}/inst/common/init/${curOS}.sh"

if [ -n "${DSY_Force_OS}" ]; then
  DSY_OS=${DSY_Force_OS}
  echo " INFO: DSY_Force_OS=\"${DSY_Force_OS}\", use it for DSY_OS"
fi

echo " INFO: DSY_OS=\"${DSY_OS}\""
export DSY_OS
export DSY_LIBPATH_VARNAME
umask 0022

# check that common/${DSY_OS}.installos exists, that is, the DSY_OS is authorized
if [ ! -f "${CurrentMediaDir}/inst/common/${DSY_OS}.installos" ]; then
  echo "ERROR: This media cannot be installed on this operating system: '${DSY_OS}' (${curOS} ${DSY_OS_Release})"
  echo "       It can only be installed on the following operating system(s):"
  if [ -d "${CurrentMediaDir}/inst/common" ]; then
    for osfile in "${CurrentMediaDir}"/inst/common/*.installos
    do
      echo "       -" $(basename -s .installos "${osfile}")
    done
  fi
  exit 7
fi

DSY_exe="${CurrentMediaDir}/inst/common/${DSY_OS}.installos"

# skip check prereq if variable DSY_Skip_CheckPrereq is set to 1
if [ "${DSY_Skip_CheckPrereq}" != "1" ]; then
  # check prereq 'curOS' if any
  if [ -f "${CurrentMediaDir}/inst/common/checkers/${curOS}/CheckPrereq.sh" ]; then
    . "${CurrentMediaDir}/inst/common/checkers/${curOS}/CheckPrereq.sh"
    # ignore error if variable DSY_IgnoreError_CheckPrereq is set to 1
    if [ "$?" != 0 ] && [ "${DSY_IgnoreError_CheckPrereq}" != "1" ]; then
      exit 9
    fi
  fi

  # check prereq 'DSY_OS' if any
  if [ -f "${CurrentMediaDir}/inst/common/checkers/${DSY_OS}/CheckPrereq.sh" ]; then
    . "${CurrentMediaDir}/inst/common/checkers/${DSY_OS}/CheckPrereq.sh"
    # ignore error if variable DSY_IgnoreError_CheckPrereq is set to 1
    if [ "$?" != 0 ] && [ "${DSY_IgnoreError_CheckPrereq}" != "1" ]; then
      exit 11
    fi   
  fi
else
  echo " INFO: CheckPrereq was skipped (DSY_Skip_CheckPrereq=${DSY_Skip_CheckPrereq})"
fi
