#!/usr/bin/sh
# Copyright (c) 1998 GEAR Software

PATH=/sbin:/usr/bin:/usr/sbin:/usr/bsd:.TH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/bsd:.
export PATH

Init ()
{
  old=`pwd`
  cd `dirname $0`
  geardir=`pwd`
  cd $old
  appname="gearuser"
  initdir=/etc/init.d
  geargd=$initdir/GEARgd
  gearogm="GEARogm"
  clear
}

Quit ()
{
  echo ""
  printf "Press RETURN "
  read a
  exit
}

CheckRoot ()
{
  userid=`id | sed -e 's/uid=\([0-9][0-9]*\).*/\1/'`
  if [ "$userid" != 0 ] ; then
    echo ""
    echo "You need root privilages to run this script"
    Quit
  fi
}

OctalToPerms ()
{
  octal="$1"

  length=`echo $octal | awk '{print length ($0)}'`
  if [ $length -eq 10 ] ; then
    octal=`echo $octal | awk '{ print substr ($0, 2, 9) }'`
  fi

  mode=""
  owner=`echo $octal | awk '{ print substr ($0, 1, 3) }'`
  case "$owner" in
    rwx | rw-)
      mode="yes"
    ;;
    *)
      mode="no "
    ;;
  esac
  group=`echo $octal | awk '{ print substr ($0, 4, 3) }'`
  case "$group" in
    rwx | rw-)
      mode="${mode}yes"
    ;;
    *)
      mode="${mode}no "
    ;;
  esac
  echo "$mode"
}

FindBuses ()
{
  dir="$1"
  file="$2"

  template=`printf "$file" "%s" "." "."`
  BUSES=""
  for i in 0 1 2 3 4 5 6 7 ; do
    path=`printf "$template" "$i"`
    awk_cmd="/$path/ { print \$0 }"
    bus=`eval "ls -1 "$dir" | awk '$awk_cmd'"`
    if [ -n "$bus" ] ; then
      BUSES="$BUSES $i"
    fi
  done
}

FindTargets ()
{
  busid="$1"
  dir="$2"
  file="$3"

  template=`printf "$file" "$busid" "%s" "."`
  TARGETS=""
  for i in 0 1 2 3 4 5 6 ; do
    path=`printf "$template" "$i"`
    awk_cmd="/$path/ { print \$0 }"
    target=`eval "ls -1 "$dir" | awk '$awk_cmd'"`
    if [ -n "$target" ] ; then
      TARGETS="$TARGETS $i"
    fi
  done
}

FindLuns ()
{
  busid="$1"
  targetid="$2"
  dir="$3"
  file="$4"

  template=`printf "$file" "$busid" "$targetid" "%s"`
  LUNS=""
  for i in 0 1 2 3 4 5 7 ; do
    path=`printf "$template" "$i"`
    awk_cmd="/$path/ { print \$0 }"
    lun=`eval "ls -1 "$dir" | awk '$awk_cmd'"`
    if [ -n "$lun" ] ; then
      LUNS="$LUNS $i"
    fi
  done
}

GetSelection ()
{
  prompt="$1"
  answers="$2"
  default="$3"
  one="$4"

  nr_answers=`echo $answers | wc -w`
  if [ $nr_answers -gt 1 ] ; then
    ask="y"
  elif [ $nr_answers -eq 0 ] && [ "$one" = "n" ] ; then
    answers="$default"
    ask="y"
  else
    ask="n"
  fi

  if [ "$ask" = "y" ] ; then
    while true ; do
      echo ""
      printf "$prompt" "$answers, "
      read answer
      if [ "$answer" = "q" ] ; then
        Quit
      else
        for ii in `echo "$answers"` ; do 
          if [ "$ii" = "$answer" ] ; then
            ID="$ii"
            return
          fi
        done
      fi
    done
  else
    ID=`echo "$answers" | awk '{print $1}'`
  fi
}

GetBusTargetLun ()
{
  dir="$1"
  file="$2"
  one="$3"

  BUSID=""
  TARGETID=""
  LUNID=""
  FindBuses "$dir" "$file"
  GetSelection "Bus id of the bus the recorder is connected to (%sq = quit) " \
               "$BUSES" "0 1 2 3 4 5 6 7" "$one"
  BUSID="$ID"
  FindTargets "$BUSID" "$dir" "$file"
  GetSelection "Target id of the recorder (%sq = quit) " \
               "$TARGETS" "0 1 2 3 4 5 6" "$one"
  TARGETID="$ID"
  FindLuns "$BUSID" "$TARGETID" "$dir" "$file"
  GetSelection "Lun of the recorder (%sq = quit) " \
               "$LUNS" "0 1 2 3 4 5 6 7" "$one"
  LUNID="$ID"
}

GetInterface  ()
{
  if [ ! -f "$geardir"/../.gearrc ] ; then
    echo ""
    echo "File '`dirname $geardir`/.gearrc' not found"
    Quit
  fi

  INTERFACE=`sed -n -e '/CDRInterface/ s/^ *CDRInterface *= *\([0-9]\) *$/\1/p' < "$geardir"/../.gearrc`
  case "$INTERFACE" in
    "0")
      DIR=""
      FILE=""
    ;;
    "1")
      DIR="/dev/rgd"
      FILE="c%st%sl%ss0"
    ;;
    "2")
      DIR="/dev/scsi"
      FILE="sc%sd%sl%s"
    ;;
    *)
      echo ""
      echo "Canot read file '`dirname $geardir`/.gearrc'."
      Quit
    ;;
  esac
}

CurrentUserGroup ()
{
  echo ""
  printf "Current driver   : "
  case "$INTERFACE" in
    "0")
      echo "Null device"
      Quit
    ;;
    "1")
      echo "Tracer driver"
    ;;
    "2")
      echo "Standard SCSI driver"
    ;;
    *)
      echo "Unknown"
      Quit
    ;;
  esac
  DEVFILE=`printf "$DIR/$FILE" "$BUSID" "$TARGETID" "$LUNID"`
  info=`ls -l $DEVFILE 2>/dev/null`
  if [ -z "$info" ] ; then
    DEVFILE=`sed -n -e '/devfile=/ s/devfile=//p' < $initdir/$gearogm | \
             sed -e 's/"//g'`
    if [ -z "$DEVFILE" ] ; then
      GetBusTargetLun "$DIR" "$FILE" "n"
      DEVFILE=`printf "$DIR/$FILE" "$BUSID" "$TARGETID" "$LUNID"`
    fi
    CUR_OWNER=`sed -n -e '/owner=/ s/owner=//p' < $initdir/$gearogm | \
               sed -e 's/"//g'`
    if [ -z "$CUR_OWNER" ] ; then
      CUR_OWNER=root
    fi
    CUR_GROUP=`sed -n -e '/group=/ s/group=//p' < $initdir/$gearogm | \
               sed -e 's/"//g'`
    if [ -z "$CUR_GROUP" ] ; then
      CUR_GROUP=sys
    fi
    CUR_MODE=`sed -n -e '/mode=/ s/mode=//p' < $initdir/$gearogm | \
              sed -e 's/"//g'`
    if [ -z "$CUR_MODE" ] ; then
      CUR_MODE="yesno "
    else
      CUR_MODE=`OctalToPerms "$CUR_MODE"`
    fi
  else
    CUR_OWNER=`echo "$info" | awk '{print $3}'`
    CUR_GROUP=`echo "$info" | awk '{print $4}'`
    CUR_MODE=`echo "$info" | awk '{print $1}'`
    CUR_MODE=`OctalToPerms "$CUR_MODE"`
  fi
  owner_perm=`echo "$CUR_MODE" | awk '{print substr ($1, 1, 3)}'`
  group_perm=`echo "$CUR_MODE" | awk '{print substr ($1, 4, 3)}'`
  echo   "File             : $DEVFILE"
  printf "Current owner    : %-10s access: %-4s\n" "$CUR_OWNER" "$owner_perm"
  printf "Current group    : %-10s access: %-4s\n" "$CUR_GROUP" "$group_perm"
  echo ""
  echo "Do you want to change the owner, group and permissions of"
  echo " $DEVFILE"
  echo ""
  printf "Type (y or n) [n]: "
  read a
  if [ "$a" != "y" ] ; then
    Quit
  fi
}

GetOwnerGroup ()
{
  echo ""
  printf "Give the new owner of $DEVFILE [root]: "
  read answer
  if [ -z "$answer" ] ; then
    OWNERID=root
  else
    OWNERID="$answer"
  fi
  owner_perm="rw-"
  set_owner_perm="u=rw"
  echo ""
  printf "Give the new group of $DEVFILE [sys]: "
  read answer
  if [ -z  "$answer" ] ; then
    GROUPID="sys"
  else
    GROUPID="$answer"
  fi
  echo ""
  printf "Do you want to give ${GROUPID} access (y/n) [n]: "
  read answer
  case "$answer" in
    y )
      group_perm="rw-"
      set_group_perm="g=rw"
    ;;
    *)
      set_group_perm="g="
    ;;
  esac
  MODE="${owner_perm}${group_perm}---"
  SET_MODE="${set_owner_perm},${set_group_perm},o="
}

CheckOK ()
{
  ok=1
  if [ "$INTERFACE" = "1" ] && [ ! -c "$DEVFILE" ] ; then
    echo ""
    echo "Device file $DEVFILE not found."
    echo "The next time you reboot with"
    echo "a recorder connected the Tracer"
    echo "device files must be there with"
    echo "the proper owner, group and"
    echo "permissions."
    return
  elif [ ! -c "$DEVFILE" ] ; then
    echo ""
    echo "Device file $DEVFILE not found."
    return
  elif [ "$INTERFACE" = "1" ] && [ ! -f "$geargd" ] ; then
    echo ""
    echo "File '$geargd' not found."
    echo "Tracer device driver not properly installed."
    return
  fi
  info=`ls -l $DEVFILE`
  NEW_OWNER=`echo "$info" | awk '{print $3}'`
  NEW_GROUP=`echo "$info" | awk '{print $4}'`
  NEW_MODE=`echo "$info" | awk '{print $1}'`
  NEW_MODE=`OctalToPerms $NEW_MODE"`
  if [ "$NEW_OWNER" != "$OWNERID" ] ; then
    ok=0
  fi
  if [ "$NEW_GROUP" != "$GROUPID" ] ; then
    ok=0
  fi
  perms_mode=`OctalToPerms "$MODE"`
  if [ "$NEW_MODE" != "$perms_mode" ] ; then
    ok=0
  fi
  echo ""
  printf "Changing owner, group and permissions "
  if [ $ok = 1 ] ; then
    echo "was successful"
  else
    echo "has failed"
  fi
}

ChangeDevFiles ()
{
  if [ "$INTERFACE" = "1" ] ; then
    ChangeTracerDevFiles
  else
    ChangeSCSIDevFiles
  fi
}

ChangeSCSIDevFiles ()
{
  cd /dev
  if [ ! -c "$DEVFILE" ] ; then
    ./MAKEDEV > /dev/null 2>&1
  fi
  if [ -c "$DEVFILE" ] ; then
    chown "$OWNERID"  "$DEVFILE" > /dev/null 2>&1
    chgrp "$GROUPID"  "$DEVFILE" > /dev/null 2>&1
    chmod "$SET_MODE" "$DEVFILE" > /dev/null 2>&1
  fi
}

ChangeTracerDevFiles ()
{
  if [ ! -f "$geardir/$gearogm" ] ; then
    return
  fi
  eval "cmd1='/devfile=/ s@^.*\$@devfile=$DEVFILE@'"
  eval "cmd2='/owner=/ s@^.*\$@owner=$OWNERID@'"
  eval "cmd3='/group=/ s@^.*\$@group=$GROUPID@'"
  eval "cmd4='/mode=/ s@^.*\$@mode=$SET_MODE@'"
  cat "$geardir/$gearogm" 2>/dev/null | \
  sed -e "$cmd1" | \
  sed -e "$cmd2" | \
  sed -e "$cmd3" | \
  sed -e "$cmd4" > "$initdir"/$gearogm 2>/dev/null
  chmod 0755 "$initdir/$gearogm"
  if [ ! -f "$geargd" ] ; then
    return
  fi
  present=`grep -l "$gearogm" "$geargd"`
  if [ -z "$present" ] ; then
    ed "$geargd" <<! > /dev/null 2>&1
/^[ 	]*\$GDCONFIG/
a
	$initdir/$gearogm rgd
.
1,\$p
w
q
!
  fi
  if [ -c "$DEVFILE" ] ; then
    chown "$OWNERID"  "$DEVFILE" > /dev/null 2>&1
    chgrp "$GROUPID"  "$DEVFILE" > /dev/null 2>&1
    chmod "$SET_MODE" "$DEVFILE" > /dev/null 2>&1
  fi
}

Init "$0"
CheckRoot
GetInterface
GetBusTargetLun "$DIR" "$FILE" "y"
CurrentUserGroup
GetOwnerGroup
ChangeDevFiles
CheckOK
Quit
