#!/bin/sh
# scripts/ogle.  Generated from ogle.in by configure.

ostype=`uname`
d_suf="_debug"
debug="0"
nav_debug=""
audio_debug=""
vout_debug=""
cli_debug=""
ctrl_debug=""
gui_debug=""
mpeg_ps_debug=""
mpeg_vs_debug=""

check="0"
param=""


while [ $# -gt 0 ]
  do
  case "$1" in
      --debug)
          debug="1"
	  if [ -z "$OGLE_DLEVEL" ]; then
	      OGLE_DLEVEL=5
	  fi
          DVDCSS_VERBOSE=2
          export DVDCSS_VERBOSE
	  DVDREAD_VERBOSE=2
	  export DVDREAD_VERBOSE
          ;;
      --check)
          debug="1"
          check="1"
          ;;
      --debug-*)
          case "$1" in
              --debug-nav)
                  nav_debug=$d_suf
                  ;;
              --debug-audio)
                  audio_debug=$d_suf
                  ;;
              --debug-vout)
                  vout_debug=$d_suf
                  ;;
              --debug-cli)
                  cli_debug=$d_suf
                  ;;
              --debug-ctrl)
                  ctrl_debug=$d_suf
                  ;;
              --debug-gui)
                  gui_debug=$d_suf
                  ;;
              --debug-mpeg_ps)
                  mpeg_ps_debug=$d_suf
                  ;;
              --debug-mpeg_vs)
                  mpeg_vs_debug=$d_suf
                  ;;
              --debug-all)
                  nav_debug=$d_suf
                  audio_debug=$d_suf
                  vout_debug=$d_suf
                  cli_debug=$d_suf
                  ctrl_debug=$d_suf
                  gui_debug=$d_suf
                  mpeg_ps_debug=$d_suf
                  mpeg_vs_debug=$d_suf
                  ;;
          esac
          ;;
      *)
          break
          ;;
  esac
  shift
done

if [ -z "$OGLE_DLEVEL" ]; then
    OGLE_DLEVEL=3
fi

export OGLE_DLEVEL


if [ "$debug" = "1" ]; then
 # information on the build 
 echo "Build: Linux 4.9.208-1 #1 SMP Sat Jan 4 19:36:41 CET 2020 i686 Intel(R)_Xeon(R)_CPU___________X5470__@_3.33GHz Mon May 18 09:43:33 CEST 2020 mmx Xv oss alsa"
 # and the os version we are running on
 runtime="Runtime: `uname -m -r -s -v`"
 runtime="${runtime} `uname -p 2> /dev/null`"
 echo "$runtime"
 # gcc options used at compile time
 echo "gcc options: : GNU C17 (PLD-Linux) version 9.3.0 20200313 (release) (i686-pld-linux) : compiled by GNU C version 9.3.0 20200313 (release), GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP : GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 : options passed: -D_FORTIFY_SOURCE=2 gcctest.c -march=i586 : -mtune=pentiumpro -auxbase-strip gcctest.s -gdwarf-4 -g2 -gdwarf-4 -g2 : -O2 -Wformat=1 -Werror=format-security -Werror=trampolines -Wall -fwrapv : -fstack-protector-strong -fomit-frame-pointer : -fasynchronous-unwind-tables -fno-debug-types-section : -fvar-tracking-assignments -fverbose-asm --param ssp-buffer-size=4 : options enabled: -faggressive-loop-optimizations -falign-functions : -falign-jumps -falign-labels -falign-loops -fassume-phsa : -fasynchronous-unwind-tables -fauto-inc-dec -fbranch-count-reg : -fcaller-saves -fcode-hoisting -fcombine-stack-adjustments -fcommon : -fcompare-elim -fcprop-registers -fcrossjumping -fcse-follow-jumps : -fdefer-pop -fdelete-null-pointer-checks -fdevirtualize : -fdevirtualize-speculatively -fdwarf2-cfi-asm -fearly-inlining : -feliminate-unused-debug-types -fexpensive-optimizations : -fforward-propagate -ffp-int-builtin-inexact -ffunction-cse -fgcse : -fgcse-lm -fgnu-runtime -fgnu-unique -fguess-branch-probability : -fhoist-adjacent-loads -fident -fif-conversion -fif-conversion2 : -findirect-inlining -finline -finline-atomics : -finline-functions-called-once -finline-small-functions -fipa-bit-cp : -fipa-cp -fipa-icf -fipa-icf-functions -fipa-icf-variables -fipa-profile : -fipa-pure-const -fipa-ra -fipa-reference -fipa-reference-addressable : -fipa-sra -fipa-stack-alignment -fipa-vrp -fira-hoist-pressure : -fira-share-save-slots -fira-share-spill-slots : -fisolate-erroneous-paths-dereference -fivopts -fkeep-static-consts : -fleading-underscore -flifetime-dse -flra-remat -flto-odr-type-merging : -fmath-errno -fmerge-constants -fmerge-debug-strings : -fmove-loop-invariants -fomit-frame-pointer -foptimize-sibling-calls : -foptimize-strlen -fpartial-inlining -fpcc-struct-return -fpeephole : -fpeephole2 -fplt -fprefetch-loop-arrays -free -freorder-blocks : -freorder-blocks-and-partition -freorder-functions -frerun-cse-after-loop : -fsched-critical-path-heuristic -fsched-dep-count-heuristic : -fsched-group-heuristic -fsched-interblock -fsched-last-insn-heuristic : -fsched-rank-heuristic -fsched-spec -fsched-spec-insn-heuristic : -fsched-stalled-insns-dep -fschedule-fusion -fschedule-insns2 : -fsemantic-interposition -fshow-column -fshrink-wrap : -fshrink-wrap-separate -fsigned-zeros -fsplit-ivs-in-unroller : -fsplit-wide-types -fssa-backprop -fssa-phiopt -fstack-protector-strong : -fstdarg-opt -fstore-merging -fstrict-aliasing : -fstrict-volatile-bitfields -fsync-libcalls -fthread-jumps : -ftoplevel-reorder -ftrapping-math -ftree-bit-ccp -ftree-builtin-call-dce : -ftree-ccp -ftree-ch -ftree-coalesce-vars -ftree-copy-prop -ftree-cselim : -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre : -ftree-loop-if-convert -ftree-loop-im -ftree-loop-ivcanon : -ftree-loop-optimize -ftree-parallelize-loops= -ftree-phiprop -ftree-pre : -ftree-pta -ftree-reassoc -ftree-scev-cprop -ftree-sink -ftree-slsr : -ftree-sra -ftree-switch-conversion -ftree-tail-merge -ftree-ter : -ftree-vrp -funit-at-a-time -funwind-tables -fvar-tracking : -fvar-tracking-assignments -fverbose-asm -fwrapv : -fzero-initialized-in-bss -m32 -m80387 -m96bit-long-double : -maccumulate-outgoing-args -malign-stringops -mfancy-math-387 : -mfp-ret-in-387 -mglibc -mieee-fp -mlong-double-80 -mno-red-zone : -mno-sse4 -mpush-args -msahf -mstv -mtls-direct-seg-refs -mvzeroupper : gcctest.c:1: int main(){return(0);}"

 case $ostype in
     "Linux")

         #cpu information
	 cpuinfo=`cat /proc/cpuinfo | sed s/\.*/\&,/`
	 echo "cpuinfo:"
	 echo $cpuinfo
	 echo ""
	 
         #check for bad kernels
	 linux_version="`uname -r`"
	 if [ "$linux_version" = "2.4.10" ]; then
	     echo ""
	     echo "WARNING: This kernel ($linux_version) is known to have problems with DVD:s."
	     echo ""
	 fi
	 echo $linux_version | grep "2.4.9-" >/dev/null
	 if [ "$?" = "0" ]; then
	     echo ""
	     echo "WARNING: There has been some DVD-troubles with modified 2.4.9-kernels."
	     echo ""
	 fi
	 
         #check for dvd drives
	 PROCFILE="/proc/sys/dev/cdrom/info"
	 if [ ! -r "$PROCFILE" ]; then
	     echo "File not found: $PROCFILE"
	     echo "No CD or DVD devices on this system or cdrom module not loaded?"
	 else
	     DEVLIST=`cat $PROCFILE | grep 'drive name:' | cut -d':' -f 2-`
	     
	     ISDVDLIST=`cat $PROCFILE \
                        | grep 'Can read DVD:' \
                        | cut -d':' -f 2-`
	     
	     dvd_dev_f() {
	         for dev in $DEVLIST; do
                     if [ "$1" = "1" ]; then
                         DVD_DEV="$DVD_DEV $dev"
		     fi
		     shift
                 done
             }
	     
             dvd_dev_f $ISDVDLIST

             #is /dev/dvd set?
	     dev="/dev/dvd"
	     while [ -h $dev ]; do
		 nextdev=`ls -l $dev | cut -d'>' -f 2`
		 nextdev="`echo $nextdev`"
		 echo  "$dev -> $nextdev"
		 abspath="`echo $nextdev | cut -b1`"
		 if [ "$abspath" = "/" ]; then
		     dev="`echo $nextdev`"
		 else
		     dev="`dirname $dev`/$nextdev"
		 fi
	     done
	     if [ ! -r "$dev" ]; then
		 echo "`ls -l $dev`"
		 echo "WARNING: $dev is not readable"
		 echo ""
	     fi
	     
             #ide devices
	     ide_devices="`ls /proc/ide | grep hd`"
             #ide cd/dvd devices
	     for dev in $ide_devices; do
		 driver="`cat /proc/ide/$dev/driver | cut -d' ' -f 1`"
		 if [ "$driver"  = "ide-cd" ]; then
		     ide_cd_dev="$ide_cd_dev $dev"
		 elif [ "$driver" = "ide-scsi" ]; then
		     ide_scsi_cd_dev="$ide_scsi_cd_dev $dev"
		 fi
	     done
             #the devices that can read DVD
	     echo  -n "Valid DVD devices:"
	     if [ -z "$DVD_DEV" ]; then
		 echo ""
		 echo "WARNING: No DVD devices found"
	     fi
	     for dev in $DVD_DEV; do
		 scsi_dev="`echo $dev | grep sr`"
		 if [ -n "$scsi_dev" ]; then
		     scsi_nr="`echo $dev | cut -d'r' -f 2`"
		     if [ -b "/dev/sr$scsi_nr" ]; then
			 echo -n " /dev/sr$scsi_nr"
		     elif [ -b "/dev/scd$scsi_nr" ]; then
			 echo -n " /dev/scd$scsi_nr"
		     else
			 echo -n " ** block device is missing: /dev/sr$scsi_nr **"
		     fi
		 else
		     dev="/dev/$dev"
		     if ! [ -a $dev ]; then
			 echo -n "** block device is missing: $dev **"
		     elif ! [ -h $dev ]; then
			 echo -n " $dev"
		     fi
		     while [ -h $dev ]; do
			 nextdev=`ls -l $dev | cut -d'>' -f 2`
			 nextdev="`echo $nextdev`"
			 echo  " $dev -> $nextdev"
			 abspath="`echo $nextdev | cut -b1`"
			 if [ "$abspath" = "/" ]; then
			     dev="`echo $nextdev`"
			 else
			     dev="`dirname $dev`/$nextdev"
			 fi
		     done
		     
		 fi
	     done
	     echo ""
	     for dev in $ide_scsi_cd_dev; do
		 echo "/dev/$dev is running ide-scsi (`cat /proc/ide/$dev/model`)"
		 if ! [ -a /dev/$dev ]; then
		     echo " ** block device is missing: $dev **"
		     echo " This may be because you are running devfs,"
		     echo " in this case to turn on/off dma with hdparm"
		     echo " you should use the corresponding ide device node"
		     echo " /dev/ide/hostX/busY/targetZ/lun0/generic,"
		     echo " where you set X,Y,Z to the apropriate numbers."
		     echo " /dev/hdb would be: /dev/ide/host0/bus0/target1/lun0/generic"
		     echo " hdc: /dev/ide/host0/bus1/target0/..."
		     echo " hdd: /dev/ide/host0/bus1/target1/..."
		 fi
		 
		 if ! [ -a /proc/ide/$dev/settings ]; then
		     echo "/proc/ide/$dev/settings does not exist"
		 elif [ -r /proc/ide/$dev/settings ]; then
		   dma_use="`cat /proc/ide/$dev/settings | grep using_dma`"
		   dma_use="`echo $dma_use | cut -d ' ' -f 2`"

		   if [ -z "$dma_use" ]; then
		       echo "WARNING: Could not get DMA state for /dev/$dev"
		   elif [ "$dma_use" = "1" ]; then
		       echo "DMA is on for /dev/$dev"
		   else
		       echo ""
		       echo "WARNING: DMA is OFF for /dev/$dev"
		       echo ""
		   fi
		 else
		     echo "WARNING: Cannot read /proc/ide/$dev/settings, need to be root to do that?"
		 fi
	     done
	     
             #ide device information
	     hdparm_path="/sbin /usr/sbin"
	     hdparm_found=0
	     hpath_empty=0
	     hdparm_version=`hdparm -V 2> /dev/null`
	     if [ "$?" != "0" ]; then
		 for hpath in $hdparm_path; do
		     if $hpath/hdparm -V 2>/dev/null; then
			 hdparm_found=1
			 break
		     fi
		 done
	     else
		 hdparm_found=1
		 hpath_empty=1
		 echo "$hdparm_version"
	     fi

	     if [ "$hpath_empty" = "1" ]; then
		 hcmd="hdparm"
	     else
		 hcmd="$hpath/hdparm"
	     fi

	     if [ "$hdparm_found" = "1" ]; then
		 for dev in $DVD_DEV; do
		     ide_dev="`echo $dev | grep hd`"
		     if [ -n "$ide_dev" ]; then
			 dmainfo="`$hcmd /dev/$dev 2>/dev/null | grep using_dma | cut -d'=' -f 2 | cut -d\( -f 1`"
			 dmainfo="`echo $dmainfo`"
			 if [ ! -r /dev/$dev ]; then
			     echo "WARNING: Cannot read /dev/$dev, need to be root to do that?"
			 fi
			 if [ -z "$dmainfo" ]; then
			     echo "WARNING: Could not get DMA state for /dev/$dev"
			 elif [ "$dmainfo" = "1" ]; then
			     echo "DMA is on for /dev/$dev"
			 else
			     echo ""
			     echo "WARNING: DMA is OFF for /dev/$dev"
			     echo ""
			 fi
			 $hcmd /dev/$dev 2>/dev/null
		     fi
		 done
		 for dev in $ide_scsi_cd_dev; do
		     dmainfo="`$hcmd /dev/$dev 2>/dev/null | grep using_dma | cut -d'=' -f 2 | cut -d\( -f 1`"
		     dmainfo="`echo $dmainfo`"
		     if [ ! -r /dev/$dev ]; then
			 echo "WARNING: Cannot read /dev/$dev, need to be root to do that?"
		     fi
		     if [ -z "$dmainfo" ]; then
			 echo "WARNING: Could not get DMA state for /dev/$dev"
		     elif [ "$dmainfo" = "1" ]; then
			 echo "DMA is on for /dev/$dev"
		     else	       
			 echo ""
			 echo "WARNING: DMA is OFF for /dev/$dev"
			 echo ""
		     fi
		     $hcmd /dev/$dev 2>/dev/null
		 done
	     else
		 echo "WARNING: hdparm not found"
	     fi  
	 fi
	 
	 ;;
     "SunOS")
	 sysinfo -class device
	 fpversion
	 ;;
 esac
fi

if [ "$check" = "1" ]; then
    exit
fi
# This is were all the helper programs are placed
prefix=/usr
exec_prefix=/usr
DVDP_ROOT=/usr/lib/ogle

export DVDP_ROOT
DVDP_CTRL=$DVDP_ROOT/ogle_ctrl$ctrl_debug
export DVDP_CTRL
DVDP_DEMUX=$DVDP_ROOT/ogle_mpeg_ps$mpeg_ps_debug
export DVDP_DEMUX
DVDP_CLI_UI=$DVDP_ROOT/ogle_cli$cli_debug
export DVDP_CLI_UI
if [ -z "$DVDP_UI" -o ! -x "$DVDP_UI" ]; then
if [ -x $DVDP_ROOT/ogle_gui$gui_debug ]; then
DVDP_UI=$DVDP_ROOT/ogle_gui$gui_debug
else
DVDP_UI=$exec_prefix/bin/ogle_gui$gui_debug
fi
fi
export DVDP_UI
#DVDP_AC3=$DVDP_ROOT/ogle_ac3_p
#DVDP_AC3=$DVDP_ROOT/ogle_a52
DVDP_AC3=$DVDP_ROOT/ogle_audio$audio_debug
export DVDP_AC3
DVDP_LPCM=$DVDP_ROOT/ogle_audio$audio_debug
export DVDP_LPCM
DVDP_MPEGAUDIO=$DVDP_ROOT/ogle_audio$audio_debug
export DVDP_MPEGAUDIO
DVDP_DTS=$DVDP_ROOT/ogle_audio$audio_debug
export DVDP_DTS
DVDP_VIDEO=$DVDP_ROOT/ogle_mpeg_vs$mpeg_vs_debug
export DVDP_VIDEO
DVDP_VMG=$DVDP_ROOT/ogle_nav$nav_debug
#DVDP_VMG=RUNNING
export DVDP_VMG
if [ -z "$DVDP_SPU" -o ! -x "$DVDP_SPU" ]; then
if [ -x $DVDP_ROOT/ogle_vout$vout_debug ]; then
DVDP_SPU=$DVDP_ROOT/ogle_vout$vout_debug
else
DVDP_SPU=$exec_prefix/bin/ogle_vout$vout_debug
fi
fi
export DVDP_SPU
if [ -z "$DVDP_VIDEO_OUT" -o ! -x "$DVDP_VIDEO_OUT" ]; then
if [ -x $DVDP_ROOT/ogle_vout$vout_debug ]; then
DVDP_VIDEO_OUT=$DVDP_ROOT/ogle_vout$vout_debug
else
DVDP_VIDEO_OUT=$exec_prefix/bin/ogle_vout$vout_debug
fi
fi
export DVDP_VIDEO_OUT
if [ -x "$DVDP_UI" ]; then
  $DVDP_CTRL -u gui "$@";
else
  $DVDP_CTRL -u cli "$@";
fi;
