#! /bin/sh

# Copyright (c) 2003-2011
# Distributed Systems Software.  All rights reserved.
# See the file LICENSE for redistribution information.
#
# $Id: setaccess-sh 2528 2011-09-23 21:54:05Z brachman $

# Set mode and owner/group for files by prompting/caching
# If a cache file doesn't exist, prompt the user for a name
# The "reset" argument, which must be first, clears the caches
# "mode" is the mode for all files
# -R does the operations recursively within each pathname argument
#
# Usage: setaccess-sh [reset] [-R] mode pathname ...
#        setaccess-sh reset

# Cache files
ownername="../src/.ownername"
groupname="../src/.groupname"
defaults="../src/.defaults"

# Source this file; it should have been created by configure
if test -r $defaults
then
  . $defaults
fi

owner=
group=
recurse=

# Assume these are available on the path
chown_cmd=chown
chgrp_cmd=chgrp
chmod_cmd=chmod

# This program should be run only from the src or src/conftools directory
# XXX we could look at argv[0]
if test -r ./echo_n-sh
then
  echo_n="./echo_n-sh"
elif test -r ../src/conftools/echo_n-sh
then
  echo_n="../src/conftools/echo_n-sh"
else
  echo_n="echo"
fi

if test -r ./printpath-sh
then
  pp="./printpath-sh"
elif test -r ../src/conftools/printpath-sh
then
  pp="../src/conftools/printpath-sh"
else
  pp=""
fi

if test "x$pp" != "x"
then
  groups_cmd=`$pp -P "/usr/ucb:/usr/bin:/bin" groups`
  if test $? != 0
  then
    groups_cmd=
  fi
fi

do_reset()
{

  rm -f "$ownername" "$groupname"
}

get_ownername()
{

  if test -r "$ownername"
  then
    owner=`cat "$ownername"`
  else
    if test "${default_owner}"x = x
    then
      default_owner="${USER:=nobody}"
    fi
    $echo_n "Which user name/id should own DACS files [$default_owner]? "
    read owner
    if test -z "$owner"
    then
      owner="$default_owner"
    fi
	echo "$owner" > "$ownername"
  fi
}

get_groupname()
{

  if test -r "$groupname"
  then
    group=`cat "$groupname"`
  else
    if test "$default_group"x = x
    then
      if test "$groups_cmd"x = x
      then
        default_group="${USER:=nobody}"
      else
        default_group="${USER:=nobody}"
        set `$groups_cmd`
        if test "$1"x != x
        then
          default_group="$1"
        fi
      fi
    fi
    $echo_n "Which group name/id should own DACS files [$default_group]? "
    read group
    if test -z "$group"
    then
      group="$default_group"
    fi
	echo "$group" > "$groupname"
  fi
}

checkmodearg()
{

# Look for a file mode and at least one pathname
	if test "$1x" = "x" -o "$2x" = "x"
    then
		echo "Usage: setaccess-sh [reset] [-R] mode pathname ..."
		echo "       setaccess-sh reset"
		exit 1
	fi
}

# A "reset" flag must appear first
if test x"$1" = "xreset"
then
  do_reset
  shift
  if test -z "$1"
  then
    exit 0
  fi
fi

if test x"$1" = "x-R"
then
  recurse="-R"
  shift
  if test -z "$1"
  then
    exit 0
  fi
fi

mode="$1"
checkmodearg "$1" "$2"
shift

get_ownername
get_groupname

for i
do
	$chown_cmd $recurse "$owner" "$i"
	if test $? != 0
    then
		echo "Error: chown $recurse $owner \"$i\""
		exit 1
	fi
	$chgrp_cmd $recurse "$group" "$i"
	if test $? != 0
    then
		echo "Error: chgrp $recurse $group \"$i\""
		exit 1
	fi
	$chmod_cmd $recurse "$mode" "$i"
	if test $? != 0
    then
		echo "Error: chmod $recurse $mode \"$i\""
		exit 1
	fi
done

exit 0
