#!/bin/sh
#
# lock -- mutually exclusive file locking
# (C) Christian Peper, 11/1998, v1.0
#   Usage: lock file 
#   A non-existing file is not processed. If the file is locked by another 
#   user 'user', the file name suffix is extended to 'file.mudit.user'.
#   Note, that 
#     - you must have read/write permission to both file and directory, 
#     - the file you wish to lock must already exist 

continue=:
while $continue; do
  case $1 in
#    -ed)
#      shift
#      ed=$1
#      shift
#      ;;
    *)
      efile=$1
      continue=false
      ;;
  esac
done

ext1=lock 
ext2=$USER
efmvd=$efile.$ext1
efcpd=$efile.$ext1.$ext2

if [ $efile ]
then                                            # The mv operation is atomic !
  if mv $efile $efmvd >/dev/null 2>/dev/null

  then                     # START CRITICAL SECTION
      cp  $efmvd $efcpd        # $efmvd contains old rights.
      echo $efcpd locked.
  else echo 'lock: File <'$efile'> is locked or does not exist.'
  fi
else echo 'lock: No file specified. Usage: lock filename'
fi
