#! /bin/sh

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  <svn_repository>  <target_dir>  [ -y ] "
   echo ""
   echo "    Creates the Ferret installation file fer_environment.tar.gz. "
   echo "    The required files will be extracted from the subversion "
   echo "    repository <svn_repository>; for example, "
   echo "        file:///home/users/tmap/svn/repos/ferret/trunk "
   echo "    to a temporary directory which this script will create. "
   echo "    Font files and lib files are NOT included in the tar file "
   echo "    generated.  The gzipped tar file fer_environment.tar.gz "
   echo "    will be written in <target_dir>, which must already exist. "
   echo "    If the option third argument '-y' is given, any questions "
   echo "    normally asked by the script will be automatically answered "
   echo "    with 'y'. "
   echo ""
   echo "    Special case: if <svn_repository> is '.', then the required "
   echo "    files will be copied from the current directory instead of "
   echo "    being extracted from a subversion repository.  Thus, in "
   echo "    this case, this script should probably be invoked as: "
   echo "        bin/make_environment_tar . <target_dir> "
   echo ""
   exit 1
fi

if [ "$1" = "." ]; then
    source_dir=`pwd`
else
    info=`svn info "$1"`
    if [ -z "${info}" ]; then
#  svn has printed an appropriate error message
#  (but still returned a zero status)
       echo ""
       exit 1
    fi
fi
repository="$1"

if [ ! -d "$2" ]; then
   echo "$2 does not exist "
   echo ""
   exit 1
fi
# Make sure target_dir is a full pathname
target_dir=`cd "$2" ; pwd`

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

# Copy or checkout the required files.  The copying intentionally
# makes exactly the same directory structure as the check-out in
# order to minimize the divergent parts of this script.
if [ "${repository}" = "." ]; then
    echo "Copying FERRET environment files "
    echo "from ${source_dir} "
    echo "to ${temp_dir} "
    echo "   shell scripts"
    cp -f -r ${source_dir}/bin .
    rm -f ./bin/build_fonts/unix/binary ./bin/build_fonts/unix/fnt*.* 2>&1 1> /dev/null
    echo "   journal files"
    cp -f -r ${source_dir}/jnls .
    echo "   external function source files"
    cp -f -r ${source_dir}/external_functions .
#   remove any object files and libraries from this copy
#   of what is suppose to be just the source files
    find ${temp_dir}/external_functions -name \*.so -delete
    find ${temp_dir}/external_functions -name \*.a -delete
    find ${temp_dir}/external_functions -name \*.o -delete
    echo "   palettes"
    cp -f -r ${source_dir}/palettes .
else
    echo "Extracting FERRET environment files "
    echo "from ${repository} "
    echo "to ${temp_dir} "
    echo "   shell scripts"
    svn checkout -q ${repository}/bin
    echo "   journal files"
    svn checkout -q ${repository}/jnls
    echo "   external function source files"
    svn checkout -q ${repository}/external_functions
    echo "   palettes"
    svn checkout -q ${repository}/palettes
fi

# Move files into their proper position
echo "Doing a bit of rearranging"
mv jnls/* .
rm -rf jnls
mv palettes ppl
mkdir ext_func
mv external_functions ext_func/src

# Remove files that should not be distributed
echo "Removing clutter"
rm -f bin/Fapropos* 2>&1 1> /dev/null
rm -f bin/Fhelp* 2>&1 1> /dev/null
rm -f bin/Findex* 2>&1 1> /dev/null
rm -f bin/Finstall.[^c]* 2>&1 1> /dev/null
rm -f bin/Ftoc* 2>&1 1> /dev/null
rm -f bin/ferret_paths*_template 2>&1 1> /dev/null
rm -f bin/make_*_tar 2>&1 1> /dev/null
rm -fr bin/fonts_* 2>&1 1> /dev/null
rm -fr bin/build_fonts/original 2>&1 1> /dev/null

# Now set up the proper symbolic links
echo "Setting up symbolic links"
cd bin
ln -s Fdescr Fdesc
ln -s Fgrids Fgrid
ln -s Fprint_template Fprint
cd ..

# Create the tar file
ctar_file="${target_dir}/fer_environment.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 ""
   rm -f "${ctar_file}"
   tar czf "${ctar_file}" --exclude .svn *
   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 ""

