#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
# title
#   translate netscape message server directory name to user ID
# description
#   take a message store server user directory
#   translate the hex characters back to ascii
#   strip off the first characters
# author
#   idc@planetlarg.net   08/03/2001
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
#
   usage_message="
   usage:
      $0 -d user_dir
   example:
      the command
         $0 -d 86/16/=zzzusr01@isp01%dcom
      returns
         zzzusr01@isp01.com
   description
      Translates a Netscape mailbox directory back to the user ID.
      Any directory names are removed. The resulting user ID is
      printed to STDOUT.
   "

# no options provided? tell the punter how to use this utility
   if [ $# -eq 0 ]; then
      echo "no options were provided."
      echo "$usage_message" 1>&2
      exit 1
   fi

# our default values, in case the punter doesn't supply any
   user_dir='NOT PROVIDED'

# read in the values
   while getopts d: arg
   do
      case $arg in
         d)
            # the punter provided an optional parameter
            user_dir=$OPTARG
            ;;
         :)
            # a valid option was supplied, but the argument is missing
            echo "missing argument"
            echo "$usage_message" 1>&2
            exit 1
            ;;
         \?)
            # an invalid option was supplied
            echo "ignoring the option -$arg "
            ;;
      esac
   done

# check the values
   if [ "$user_dir" = 'NOT PROVIDED' ]; then
      echo "option -d was not provided."
      echo "$usage_message" 1>&2
      exit 1
   fi

# remove the file and parent directories
   # eg from ./72/=tim%dwatkins@isp01%dcom/store.exp to =tim%dwatkins@isp01%dcom
   path=`dirname $user_dir`
   base=`basename $path`

# decode the %...
   # I don't know what encoding system is being used, but it isn't
   # hexadecimal ascii (eg. "%2E" is a "."). This normal decode won't work:
   #    $string =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge;
   # I can only find one Netscape proprietary weird translation.
   #
   uid=`/export/home/fusion/bin/perl/perl               \
      -e '$string = substr $ARGV[0], 1;   # remove the first character ' \
      -e '$string =~ s/%d/./g;            # %d => . '   \
      -e 'print $string; '                              \
      $base `

# print the result
   echo $uid

