#!/bin/sh
#----------------------------------------------------------------------------
#  Name:     xdlocation
#  Project:  Xdiary
#
#  Description:
#    Tries to "guess" the location of a calendar. This shell script is here
#    for you to modify it! Change to whatever fits your installation of
#    XDiary.
#
#    The result from this script is a string to standard outout with the
#    COMPLETE directory name for the calendar (e.g. /u/rbl/Diary). If the
#    calendar could not be found, the empty string ("") should be written
#    to standard output.
#
#    The parameters passed to this script are as follows:
#
#      <calendar> - Calendar which you should try to locate.
#
#  Filename:         xdlocation
#
#  Authors:          Roger Larsson, Ulrika Bornetun
#  Creation date:    1992-10-28
#
#  Modifications:
#
#   Who            When
#   What
#   -----------------------------
#   <>             <>
#   <>
#
#----------------------------------------------------------------------------
#
# SCCSID = @(#) Module: xdlocation.script, Version: 1.2, Date: 95/06/21 20:49:38

program=xdlocation

CAT=/bin/cat
NISCAT=niscat
TAIL=/bin/tail
YPCAT=ypcat


# System location for calendars.
sysUserRoot=/g/diary/users
sysGroupRoot=/g/diary/groups

# Get the parameters.
calendar="$1"
shift

# If no calendar, just return.
if [ -z "$calendar" ]; then
  echo ""
  exit 1
fi

# Assume that a calendar with a @ is a mail address.
echo "$calendar" | grep "@" >/dev/null
if [ $? -eq 0 ]; then
  echo ""
  exit 1
fi


# Assumption: Look for system user and group calendars.
#

if [ -r $sysUserRoot/$calendar ]; then
  echo "$sysUserRoot/$calendar"
  exit 0
fi

if [ -r $sysGroupRoot/$calendar ]; then
  echo "$sysGroupRoot/$calendar"
  exit 0
fi


# Assumption: 
#   The calendar is in the directory Diary in the home directory.
#   The name of the calendar is the same as the login name for the user.
#
result=""

# Look in NIS+.
if [ -z "$result" ]; then
  result=`$NISCAT passwd.org_dir 2>/dev/null | grep "^${calendar}:" | $TAIL -1`
fi

# Look in YP.
if [ -z "$result" ]; then
  result=`$YPCAT passwd 2>/dev/null | grep "^${calendar}:" | $TAIL -1`
fi

# Look in /etc/passwd.
if [ -z "$result" ]; then
  result=`$CAT /etc/passwd | grep "^${calendar}:" | $TAIL -1`
fi

# Anything?
if [ -n "$result" ]; then

  # Check the login directory.
  loginDir="`echo $result | cut -f6 -d:`"
  if [ -n "$loginDir" ]; then
    echo "$loginDir/Diary"
    exit 0
  fi

fi

echo ""

exit 1
