#!/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 2.6.33.4-1 #1 SMP Thu May 13 08:53:23 CEST 2010 i686 Intel(R)_Xeon(R)_CPU___________E5345__@_2.33GHz Wed Aug 25 20:22:57 UTC 2010 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 C version 3.3.6 (PLD Linux) (i386-pld-linux) : compiled by GNU C version 3.3.6 (PLD Linux). : GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 : options passed: -D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=6 : -march=i386 -march=i586 -mcpu=pentiumpro -auxbase-strip -O2 -Wall : -fverbose-asm : options enabled: -fdefer-pop -foptimize-sibling-calls -fcse-follow-jumps : -fcse-skip-blocks -fexpensive-optimizations -fthread-jumps : -fstrength-reduce -fpeephole -fforce-mem -ffunction-cse : -fkeep-static-consts -fcaller-saves -fpcc-struct-return -fgcse -fgcse-lm : -fgcse-sm -floop-optimize -fcrossjumping -fif-conversion -fif-conversion2 : -frerun-cse-after-loop -frerun-loop-opt -fdelete-null-pointer-checks : -fschedule-insns2 -fsched-interblock -fsched-spec -fbranch-count-reg : -freorder-blocks -freorder-functions -fcprop-registers -fcommon : -fverbose-asm -fgnu-linker -fregmove -foptimize-register-move : -fargument-alias -fstrict-aliasing -fmerge-constants : -fzero-initialized-in-bss -fident -fpeephole2 -fguess-branch-probability : -fmath-errno -ftrapping-math -m80387 -mhard-float -mno-soft-float : -mieee-fp -mfp-ret-in-387 -maccumulate-outgoing-args -mcpu=pentiumpro : -march=i586"

 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;
