#! /bin/sh

# mycp needs to recursively copy subdirectories for the lib/python* directories
mycp="/bin/cp -R -p"
mylns="/bin/ln -s"

echo ""
if [ $# -eq 3 ]; then
    if [ "$3" = "-y" ]; then
        auto_ans="y"
    fi
fi
if [ $# -lt 2 -o $# -gt 3 ]; then
   argerror="true"
elif [ $# -eq 3 -a -z "${auto_ans}" ]; then
   argerror="true"
fi
if [ -n "${argerror}" ]; then
   echo "Usage:  $0  <ferret_dir>  <target_dir>  [ -y ] "
   echo ""
   echo "    Creates the Ferret installation file fer_executables.tar.gz. "
   echo "    The required files will be copied from the ferret source directory "
   echo "    <ferret_dir>, in which ferret or pyferret, and the font files "
   echo "    have been built.  All the files required will be copied to a temporary "
   echo "    directory which this script will create.  Any missing executables will "
   echo "    be noted.  The gzipped tar file fer_executables.tar.gz will be written "
   echo "    in <target_dir>, which must already exist.  If the optional third "
   echo "    argument '-y' is given, any questions normally asked by the script "
   echo "    will be automatically answered with 'y'. "
   echo ""
   exit 1
fi

if [ ! -d "$1" ]; then
   echo "$1 does not exist or is not a directory"
   echo ""
   exit 1
fi
ferret_dir=`cd "$1" ; pwd`
if [ ! -d "$2" ]; then
   echo "$2 does not exist or is not a directory"
   echo ""
   exit 1
fi
target_dir=`cd "$2" ; pwd`

pyferret_dir="${ferret_dir}/pyferret_install"
if [ ! -d "${pyferret_dir}" ]; then
   echo "${pyferret_dir} does not exist or is not a directory"
   echo "    pyferret module not created"
   echo ""
   pyferret_dir=""
fi

# Get the version of ferret recorded in the code
version=`awk '/revision_level/ {print $4}' ${ferret_dir}/fer/dat/xrevision_data.F`
if [ $? -ne 0 -o "${version}" = "" ]; then
   echo "Unable to read the version number from xrevision_data.F in ${ferret_dir}/fer/dat"
   echo ""
   exit 1
fi
echo "Ferret version number is ${version}"
echo ""

# Make a clean temporary directory for the tar file contents
temp_dir="/var/tmp/fer_exe_$$"
rm -fr ${temp_dir}
mkdir ${temp_dir}
mkdir ${temp_dir}/bin

# Copy fer/ferret_c
if [ -x ${ferret_dir}/fer/ferret_c ]; then
   ${mycp} ${ferret_dir}/fer/ferret_c ${temp_dir}/bin/ferret_v${version}
   cd ${temp_dir}/bin
   ${mylns} ferret_v${version} ferret
# Copy the Ferret external function shared-object library files
   mkdir -p ${temp_dir}/ext_func/libs
   find ${ferret_dir}/external_functions -type f -perm -100 -name \*.so -exec ${mycp} {} ${temp_dir}/ext_func/libs \;
else
   echo "No ferret_c executable found in ${ferret_dir}/fer"
#  An error only if pyferret_dir was not given
   if [ "${pyferret_dir}" = "" ]; then
      echo ""
      exit 1
   else
      echo "   ferret_v<n> and ferret symbolic link not created"
      echo ""
   fi
# Copy the PyFerret external function shared-object library files
   mkdir -p ${temp_dir}/ext_func/pylibs
   find ${ferret_dir}/external_functions -type f -perm -100 -name \*.so -exec ${mycp} {} ${temp_dir}/ext_func/pylibs \;
fi

# Copy font files from bin/build_fonts/unix/
fer_files=${ferret_dir}/bin/build_fonts/unix/f*
if [ $? -ne 0 -o "${fer_files}" = "" ]; then
   echo "No font files found in ${ferret_dir}/bin/build_fonts/unix"
   echo ""
   exit 1
fi
mkdir ${temp_dir}/ppl
mkdir ${temp_dir}/ppl/fonts
${mycp} ${fer_files} ${temp_dir}/ppl/fonts

# Copy threddsBrowser/threddsBrowser.jar
tb_jar=${ferret_dir}/threddsBrowser/threddsBrowser.jar
if [ ! -r ${tb_jar} ]; then
   echo "No threddsBrowser.jar file found ${ferret_dir}/threddsBrowser"
   echo ""
   exit 1
fi
mkdir ${temp_dir}/lib
${mycp} ${tb_jar} ${temp_dir}/lib

# Either copy threddsBrowser/toolsUI/toolsUI-4.1.jar ...
toolsui_jar=${ferret_dir}/threddsBrowser/toolsUI/toolsUI-4.1.jar
if [ ! -r ${toolsui_jar} ]; then
   echo "No toolsUI-4.1.jar file found ${ferret_dir}/threddsBrowser/toolsUI"
   echo ""
   exit 1
fi
${mycp} ${toolsui_jar} ${temp_dir}/lib
# ... or print a message about how to get toolsUI-4.1.jar
# echo "To minimize size, the toolsUI-4.1.jar file is not included."
# echo "The user needs to download the jar file from:"
# echo "  ftp://ftp.unidata.ucar.edu/pub/netcdf-java/v4.1/toolsUI-4.1.jar"
# echo "(or we can just grab it from <ferret_dir>/threddsBrowser/toolsUI/)"
# echo 'and put it under the $FER_LIBS ($FER_DIR/lib) subdirectory'

# Create a symbolic link to a (possibly non-existant) toolsUI-4.1.jar file
cd ${temp_dir}/lib
${mylns} toolsUI-4.1.jar toolsUI.jar

# Copy ferret_ef_mem_subsc.so
${mycp} ${ferret_dir}/efmem/ferret_ef_mem_subsc.so ${temp_dir}/lib

# Copy pyferret files
if [ "${pyferret_dir}" != "" ]; then
   python_dirs=`find ${pyferret_dir} -type d -name python\*`
   if [ $? -ne 0 -o "${python_dirs}" = "" ]; then
      echo "No python* directories found under ${pyferret_dir}"
      echo ""
      exit 1
   fi
   ${mycp} ${python_dirs} ${temp_dir}/lib
fi

# Create the tar file
ctar_file="${target_dir}/fer_executables.tar.gz"
echo ""
echo "The tar file will be created from the contents of "
echo "${temp_dir}"
echo "(which can now be examined or tweaked from another shell/window)"
echo ""
echo -n "Create gzipped tar file ${ctar_file} (y/n)? "
if [ -n "$auto_ans" ]; then
    ans="${auto_ans}"
    echo $ans
else
    read ans
fi
while [ "${ans}" != "y" -a "${ans}" != "n" ]; do
   echo -n "Answer either y or n: "
   read ans
done
if [ "${ans}" = "y" ]; then
   echo ""
   cd ${temp_dir}
   rm -f "${ctar_file}"
   tar czf "${ctar_file}" *
   echo ""
   ls -l "${ctar_file}"
else
   echo ""
   echo "Tar file NOT created"
fi

# Clean up
echo ""
echo "Cleaning up - removing ${temp_dir}"
cd "${target_dir}"
rm -fr "${temp_dir}"
echo ""

