#!/bin/sh

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

# check that common/init/$curOS.sh exists
if [[ ! -e $CurrentMediaDir/inst/common/init/${curOS}.sh ]]; then
  echo "Error: This media cannot be installed on this operating system '${curOS}'."
  echo "This media can be installed only 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"
echo "DSY_OS=\""$DSY_OS"\""
export DSY_OS
echo "DSY_LIBPATH_VARNAME=\""$DSY_LIBPATH_VARNAME"\""
export DSY_LIBPATH_VARNAME
umask 0022

# check that common/${DSY_OS}.installos exists, that is, the DSY_OS is authorized
if [[ ! -e $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 "This media can be installed only on the following operating system(s):"
  if [[ -d $CurrentMediaDir/inst/common ]]; then
    for osfile in "$CurrentMediaDir"/inst/common/*.installos
    do
      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
    typeset -i error_prereq
    error_prereq=0
    for fic in `ls $CurrentMediaDir/inst/common/checkers/$curOS/CheckPrereq_*.sh 2>/dev/null`
    do
      echo $fic
      eval var='$'DSY_IgnoreError_`basename $fic | awk -F. '{ print $1}'`
      if [[ -f $fic ]]; then
        . $fic
        rc=$?
        echo "check_rc="$rc
        if [[ $var != "1" ]] ; then
          error_prereq=error_prereq+1
        fi
      fi
    done
    if [[ $error_prereq != 0 ]] ; then
      exit 10
    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 "CheckPrereq was skipped DSY_Skip_CheckPrereq="$DSY_Skip_CheckPrereq
fi
