#!/bin/csh -f
#(ie run the cshell on this but don't read the .cshrc)

if ($#argv == 0) then
   echo version = 1.10 of lk 2012 Nov 09
   # 2012 Nov 09, 1.10 force link ... no, cannot do that because ln objects.
   # 2012 Jun 18, 1.09 exit if only one argument!
   # 2011 Sep 04, 1.08 update documentation
   # 2011 Jul 05, 1.07 hard links
   # 2001 Nov  5, 1.06 object if file exists
   # 2000 Nov 30, 1.05 allow for spaces in manes (passed in quotes)
   # 2000 Oct 19, 1.04 protect if file is there
   # 1989 November 6, 1.00 origin
   echo 'usage: lk [from-name] [to-file] [-hard]'
   echo 'link NAME to FILE'
   echo ' '
   echo "Link the first argument to the name given"
   echo "in the second argument:"
   echo ' '
   echo 'lk NAME (to) FILE'
   echo ' '
   echo 'You can make links if the file has spaces by surrounding'
   echo 'the names with quotes:'
   echo ' '
   echo 'lk "Multipart NAME" "multipart FILE"'
   echo ' '
   echo 'If there is a third argument, "-hard", a hard link is made.'
   exit

# Note: stat [file] indicates in the third column the number of hard
# links to a file, including the file itself.  That is, a plain file
# has '1', a file with a hard link to it has '2' (and both are then
# links to the same disk space).  So it is 'safe' to remove files with
# '2' because there is another copy elsewhere.

# Thomas D. Schneider, Ph.D.
# toms@alum.mit.edu
# toms@alum.mit.edu (permanent)
# https://alum.mit.edu/www/toms (permanent)

endif

if ($#argv == 1) then
   echo 'Two arguments needed\!'
   exit
endif

if (-e "$1") then
   echo 'lk: refuse to link: '$1' exists\!'
   exit
endif

# set force = 0
# if ($#argv == 3) then
#    if ("$3" == '-force') then
#       set force = 1
#    endif
# endif

#f !($force) then
   if (! -f "$2" && ! -d "$2") then
      echo "WARNING: file or directory "$2" DOES NOT EXIST (yet?)"
      exit
   endif
#ndif

if ($#argv == 3) then
   # make a hard link
   set softlink = ''
else
   set softlink = '-s'
endif

ln $softlink "$2" "$1"
