#!/bin/bash
# --------------------------------------------------------------------------- #
# ln3 :    Make a link from a source code file of WAVEWATCH III to the work   #
#          directory. Now also looks in bin directory.                        #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      February 2012          #
#                                                                             #
#    Copyright 2009-2012 National Weather Service (NWS),                      #
#       National Oceanic and Atmospheric Administration.  All rights          #
#       reserved.  WAVEWATCH III is a trademark of the NWS.                   #
#       No unauthorized use without permission.                               #
#                                                                             #
# --------------------------------------------------------------------------- #


# --------------------------------------------------------------------------- #
# 1. Preparations                                                             #
# --------------------------------------------------------------------------- #
# 1.a Check input

  if test "$#" -lt '1'
  then
    echo "usage: ln3 filename(s)" 1>&2 ; exit 1
  fi
  files="$*"

# 1.b Get data from setup file - - - - - - - - - - - - - - - - - - - - - - - - 

  source $(dirname $0)/w3_setenv
  main_dir=$WWATCH3_DIR
  temp_dir=$WWATCH3_TMP
  source=$WWATCH3_SOURCE
  list=$WWATCH3_LIST

# --------------------------------------------------------------------------- #
# 2. Make link(s)                                                             #
# --------------------------------------------------------------------------- #

  set $files
  found='0'

  cd $main_dir/work

  while [ "$#" -ge '1' ]
  do
    name="$1"

    for file in $name $name.ftn $name.f90 $name.doc \
                w3$name.ftn w3$name.doc w3${name}md.ftn w3${name}md.doc
    do
      if test -f ../ftn/$file
      then
        rm -f $file
        echo "make link to $file in ftn dir."
        ln -s ../ftn/$file .
        found=$(($found + 1))
      fi
      if test -f ../bin/$file
      then
        rm -f $file
        echo "make link to $file in bin dir."
        ln -s ../bin/$file .
        found=$(($found + 1))
      fi
      if test -f ../test/$file
      then
        rm -f $file
        echo "make link to $file in test dir."
        ln -s ../test/$file .
        found=$(($found + 1))
      fi
    done

    shift

  done

  if test "$found" = '0'
  then
    echo "No files found."
  fi
 
# End of list --------------------------------------------------------------- #
