--- lked.orig	2012-10-27 01:12:07.000000000 +0900
+++ lked	2013-02-27 16:06:32.000000000 +0900
@@ -57,6 +57,8 @@
                         set VB2000=false
 if ($TARGET == linux64) set VB2000=true
 if ($TARGET == linux32) set VB2000=true
+if ($TARGET == freebsd64) set VB2000=true
+if ($TARGET == freebsd32) set VB2000=true
 if ($TARGET == mac64)   set VB2000=true
 set XMVB=false
 #
@@ -632,6 +634,243 @@
 endif
 #
 endif
+
+if ($TARGET == freebsd32) then
+   set VECTOR=' '
+   set QUICHE='zunix.o'
+#
+#  settings which depend on the compiler choice:
+#
+   switch ($GMS_FORTRAN)
+     case g77:
+        set LDR='g77'
+        set LDOPTS=' '  # add '-Wl,-M' to see load map
+        set FORTLIBS=' '
+        breaksw
+     case gfortran:
+        set LDR=%%FC%%
+        set LDOPTS=' '
+        set FORTLIBS=' '
+        breaksw
+     case ifort:
+        set LDR='ifort'
+        set LDOPTS=' '  # add '-Wl,-M' to see load map
+        set FORTLIBS='-Vaxlib'
+        breaksw
+     case f2c:
+        set LDR='gcc'
+        set LDOPTS=' '
+        set FORTLIBS='-lf2c -lm'
+        breaksw
+     default:
+        echo Please spell your freebsd32 compiler correctly.
+        exit 4
+   endsw
+#
+#  settings which depend on the math library: mkl, atlas, acml, none
+#
+   switch ($GMS_MATHLIB)
+
+#       for MKL, we lack experience.  The following is the 64-bit
+#       stuff, with all 64's removed.  It might be right!
+#       For the case of "11", this has actually been verified.
+   case mkl:
+      set mpath=$GMS_MATHLIB_PATH
+      switch ($GMS_MKL_VERNO)
+        case 8:
+          set MATHLIBS="-L$mpath -lmkl_lapack   -lmkl -lguide -lpthread"
+          breaksw
+        case 9:
+          set MATHLIBS="-L$mpath -lmkl_lapack   -lmkl -lguide -lpthread"
+          breaksw
+        case 10:
+          #  next one forces single-threaded usage only...exactly what we want.
+          set MATHLIBS="-L$mpath -lmkl_intel -lmkl_sequential -lmkl_core"
+          breaksw
+        case 11:
+        case 12:
+          # next attempts a static link, whereas 10 above is a dynamic link.
+          # if this fails to work, try the version 10 lines shown just above.
+          set MATHLIBS="$mpath/libmkl_intel.a"
+          set MATHLIBS="$MATHLIBS $mpath/libmkl_sequential.a"
+          set MATHLIBS="$MATHLIBS $mpath/libmkl_core.a"
+          breaksw
+        default:
+          echo "unknown version number for MKL/linux32"
+          echo "please edit 'lked' to select the right 32 bit MKL libraries."
+          exit
+          breaksw
+      endsw
+      set BLAS=' '
+      breaksw
+
+   case atlas:
+      #     If the ATLAS comes with static libraries, link to them,
+      #     so that only the compile node has to have the libraries.
+      #     Otherwise, attempt a link against shared object libs.
+      #     See the 64 bit Atlas below for a way to fix the linker's
+      #     not being able to locate -lf77blas, if that happens.
+      if (-e $GMS_MATHLIB_PATH/libf77blas.a) then
+         set MATHLIBS="$GMS_MATHLIB_PATH/libf77blas.a"
+         set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/libatlas.a"
+      else
+         set MATHLIBS="-L$GMS_MATHLIB_PATH -lf77blas -latlas"
+      endif
+      set BLAS=' '
+      breaksw
+
+   case acml:
+      #     do a static link so that only compile node needs to install ACML
+      set MATHLIBS="$GMS_MATHLIB_PATH/libacml.a"
+      set BLAS=' '
+      breaksw
+
+   case none:
+   default:
+      echo "Warning.  No math library was found, you should install one."
+      echo "    MP2 calculations speed up about 2x with a math library."
+      echo "CCSD(T) calculations speed up about 5x with a math library."
+      set BLAS='%%BLAS%% %%LAPACK%%'
+      set MATHLIBS=' '
+      breaksw
+   endsw
+#
+   set LIBRARIES="$FORTLIBS $MATHLIBS"
+endif
+#
+#   ------ FreeBSD on 64 bit chips -----
+#
+if ($TARGET == freebsd64) then
+   set VECTOR=' '
+   set QUICHE='zunix.o'
+#
+#  settings which depend on the FORTRAN:
+#
+   switch ($GMS_FORTRAN)
+      case gfortran:
+         set LDR=%%FC%%
+         set LDOPTS=' '
+         set FORTLIBS=' '
+         breaksw
+      case ifort:
+         set LDR='ifort'
+         set LDOPTS='-i8'  # -Wl,-M generates a load map
+#            this auxiliary library seems to have disappeared in ifort 10
+         set FORTLIBS=' '
+         if($GMS_IFORT_VERNO < 10) set FORTLIBS='-Vaxlib'
+         breaksw
+      default:
+         echo "don't know how to use a freebsd64 compiler named $GMS_FORTRAN"
+         exit 4
+         breaksw
+   endsw
+#
+#  settings which depend on the math library: mkl, atlas, acml, none
+#
+   switch ($GMS_MATHLIB)
+
+   case mkl:
+      set mpath=$GMS_MATHLIB_PATH
+      set mklver=$GMS_MKL_VERNO
+      if ($CCHEM == true) set mklver=${mklver}-so
+      switch ($mklver)
+        case 8:
+          set MATHLIBS="-L$mpath -lmkl_lapack64 -lmkl -lguide -lpthread"
+          breaksw
+        case 9:
+          set MATHLIBS="-L$mpath -lmkl_lapack   -lmkl -lguide -lpthread"
+          breaksw
+          breaksw
+        case 10:
+        case 11:
+        case 12:
+          # next are a static link, in single-threaded mode (serial BLAS).
+          # The choices preclude any need to
+          #     a) install .so shared object libraries on every node,
+          #     b) define LD_LIBRARY_PATH to find the .so libraries, or
+          #     c) define MKL_NUM_THREADS=1
+          # If desired, there is a single-threaded shared object link below.
+          # Note, there's now a gfortran-specific compiler interface library.
+          set iflib=intel
+          if ($GMS_FORTRAN == gfortran) set iflib=gf
+          set MATHLIBS="          $mpath/libmkl_${iflib}_ilp64.a"
+          set MATHLIBS="$MATHLIBS $mpath/libmkl_sequential.a"
+          set MATHLIBS="$MATHLIBS $mpath/libmkl_core.a"
+          breaksw
+        case 10-so:
+        case 11-so:
+        case 12-so:
+          #  next one chooses dynamic linking (so= shared object libs)
+          #  Just hand edit GMS_MKL_VERNO in your 'install.info' to use this.
+          #  run-time libs (and LD_LIBRARY_PATH) needed on every compute node!
+          #  note that it does force single-threaded usage.
+          #  LIBCCHEM fails to link properly against static libraries.
+          #  LIBCCHEM prefers to use threading
+          set iflib=intel
+          if ($GMS_FORTRAN == gfortran) set iflib=gf
+          if ($CCHEM == false) then
+             set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_sequential -lmkl_core"
+          else
+             set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_${iflib}_thread -lmkl_core -liomp5"
+          endif
+          breaksw
+        default:
+          echo "unknown version number for MKL/linux64"
+          echo "please edit 'lked' to select the right 64 bit MKL libraries."
+          exit
+          breaksw
+      endsw
+      set BLAS=' '
+      breaksw
+
+   case atlas:
+      #     If the ATLAS comes with static libraries, link to them,
+      #     so that only the compile node has to have the libraries.
+      #     Otherwise, attempt a link against shared object libs.
+      #
+      #     Some versions of Atlas come without proper setup to use them.
+      #     For example, you may need to give some specific version such
+      #     as 3.0 some generic names, by doing the right soft links:
+      #          chdir /usr/lib64/atlas   (just 'lib' for linux32)
+      #          ln -s libatlas.so.3.0        libatlas.so
+      #          ln -s libblas.so.3.0         libblas.so
+      #          ln -s libcblas.so.3.0        libcblas.so
+      #          ln -s libf77blas.so.3.0      libf77blas.so
+      #          ln -s liblapack_atlas.so.3.0 liblapack.so
+      #          ln -s liblapack.so.3.0       liblapack.so
+      #     It is strange the RPM's don't do these soft links for us.
+      if (-e $GMS_MATHLIB_PATH/libf77blas.a) then
+         set MATHLIBS="$GMS_MATHLIB_PATH/libf77blas.a"
+         set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/libatlas.a"
+      else
+         set MATHLIBS="-L$GMS_MATHLIB_PATH -lf77blas -latlas"
+      endif
+      set BLAS=' '
+      breaksw
+
+   case acml:
+      #     do a static link so that only compile node needs to install ACML
+      set MATHLIBS="$GMS_MATHLIB_PATH/libacml.a"
+      #     if you want to try a dynamic link, use this:
+      #---set MATHLIBS="-L$GMS_MATHLIB_PATH -lacml -lacml_mv"
+      set BLAS=' '
+      breaksw
+
+   case none:
+   default:
+      echo "Warning.  No math library was found, you should install one."
+      echo "    MP2 calculations speed up about 2x with a math library."
+      echo "CCSD(T) calculations speed up about 5x with a math library."
+      set BLAS='%%BLAS%% %%LAPACK%%'
+      set MATHLIBS=' '
+      breaksw
+   endsw
+#
+   set LIBRARIES="$FORTLIBS $MATHLIBS"
+endif
+#
+endif
+
 #
 #      MacIntosh
 #
@@ -675,6 +914,23 @@
    set VECTOR=' '
    set QUICHE='zunix.o'
 endif
+#FreeBSD settings
+if ($TARGET == freebsd32) then
+   set LDR=%%FC%%
+   set LDOPTS='%%LDFLAGS%%'
+   set LIBRARIES=''
+   set BLAS="-L%%LOCALBASE%%/lib %%BLAS%% %%LIBSVM%% %%LIBG2C%%"
+   set VECTOR=' '
+   set QUICHE='zunix.o'
+endif
+if ($TARGET == freebsd64) then
+   set LDR=%%FC%%
+   set LDOPTS='%%LDFLAGS%%'
+   set LIBRARIES=''
+   set BLAS="-L%%LOCALBASE%%/lib %%BLAS%%"
+   set VECTOR=' '
+   set QUICHE='zunix.o'
+endif
 #
 if ($TARGET == necsx) then
    set LDR='f90'
@@ -1199,6 +1199,8 @@
    case ibm64:
    case linux32:
    case linux64:
+   case freebsd32:
+   case freebsd64:
    case mac32:
    case mac64:
    case sgi32:
