#!/bin/csh -f
#
#   $Id: create_src_dist,v 1.12 2010-04-14 02:31:54 haley Exp $
#
#######################################################################
#                                                                     #
#              Copyright (C)  2007                                    #
#        University Corporation for Atmospheric Research              #
#              All Rights Reserved                                    #
#                                                                     #
#######################################################################
#
#   File:       create_src_dist
#
#   Author:     Mary Haley
#           National Center for Atmospheric Research
#           PO 3000, Boulder, Colorado
#
#   
#   Date:       Tue Mar 27 09:27:53 MDT 2007
#
#   Description:  Tars up NCL and/or NCAR Graphics source for release to users.
#
# There are three types of source possible:
#
#      - NCL and NCAR Graphics source code (default)
#      - NCAR Graphics source code
#      - NCL and NCAR Graphics source code and internal "install"
#           directory
#
#   Options:   -r :  root directory
#              -1 :  tar up just NCAR Graphics (not NCL)
#              -i :  Tar up files for internal use. Includes "install"
#                    directory
#              -c :  scp to windom.ucar.edu
#              -d :  Set the tar directory
#              -g :  Tag the code with the given version number
#                    This can only be done if code is checked-out
#                    with SVN.
#              -D :  Remove the given SVN tag
#              -t :  Include triangle code
#              -v :  version number
#              -x :  don't use SVN to get directory. Assume "ncarg"
#
# Set defaults for the root directory, the tar file directory, and the
# version number.
#
# set echo verbose timestamp
#
set rootdir     = "$NCARG"
set parentdir   = `pwd`
set scpmach     = "topaz.scd.ucar.edu"
set version     = ""
set svnroot     = "https://subversion.ucar.edu"
set branch      = "NCL_5_2_1"
set tardir1     = "/fs/scd/home0/ncargd/dist/tarfiles"
set tardir2     = "/fis/scd/home/ncargd"
set extra       = ""
set extra2      = "wo_tri"      # Without Shewchuk Triangle code

if (-e $tardir1) then
  set tardir = $tardir1
else if (-e $tardir2) then 
  set tardir = $tardir2
else
  set tardir = `pwd`
endif

#
# Sort through options.
#

unset SCP
set USE_SVN
unset INC_TRI

set NCARGNCL

while ($#argv > 0)
  switch ($1)
    case "-1":
      set NCARGONLY
      unset NCARGNCL
      shift
      breaksw

    case "-i":
      set INTERNAL
      unset NCARGNCL
      shift
      breaksw

    case "-v":
      shift
      set version = "$1"
      shift
      breaksw

    case "-g":
      shift
      set tag_version = "$1"
      shift
      breaksw

    case "-D":
      shift
      set remove_tag_version = "$1"
      shift
      breaksw

    case "-d":
      shift
      set tardir = "$1"
      shift
      breaksw

    case "-t":
      set INC_TRI
      set extra2 = "w_tri"      # With Shewchuk Triangle code
      shift
      breaksw

    case "-c":
      set SCP
      shift
      breaksw

    case "-e":
      shift
      set extra = "$1"
      shift
      breaksw

    case "-r":
      shift
      set rootdir = "$1"
      shift
      breaksw

    case "-p":
      shift
      set parentdir = "$1"
      shift
      breaksw

    case "-x":
      unset USE_SVN
      shift
      breaksw

    case "-h":
    case "-help":
      goto usage
      breaksw

    default:
      echo "option not recognized"
      exit 1
    endsw
end

#
# Error checking.
#
if (! -e $tardir) then
  echo "Directory $tardir does not exist. Please choose another directory."
  exit 1
endif

# Set directory to scp source files to.

if ($?tag_version && ! $?USE_SVN) then
  echo "To tag this version, you must use the SVN check-out method."
  exit 1
endif

set scpdir = "/fs/scd/home0/ncargd/dist/tarfiles/ncl/$version"

#
# SVN check out the latest source code.
#
# First set SVNROOT environment variable.
#
setenv SVNROOT $svnroot

echo ""
if ($?USE_SVN) then
  if ($?INC_TRI) then
    echo "  I will be SVN checking out the source code in the directory"
    echo "  '$parentdir' and including the triangle code."
  else
    echo "  I will be SVN checking out the source code in the directory"
    echo "  '$parentdir' and NOT including the triangle code."
  endif
else
  echo "  I will be trying to use the 'ncarg' directory in"
  echo "  '$parentdir'. Continue? (y/n) "
endif
echo ""
echo -n "Continue? (y/n) "

set answer="$<"
if ("$answer" != "y") then
  exit
endif

#
# What if there's already an "ncarg" directory?
#
cd $parentdir
if ($?USE_SVN && -e $branch) then
  echo "Directory $parentdir/$branch already exists." 
  echo -n "Do you want me to remove it? (y/n) "
  set answer="$<"
  if ("$answer" != "y") then
    echo "Directory '$parentdir/$branch' already exists and may not be"
    echo "the correct source code. Use the '-x' option if it's okay to"
    echo "use this directory."
    exit
  endif
  set cmd = "/bin/rm -rf $parentdir/$branch"
  echo "$cmd"
  $cmd
endif

# 
# Check out all of $SVNROOT/ncldev/ncarg/branches/$branch
#
if ($?USE_SVN) then
  set cmd = "svn co $SVNROOT/ncldev/ncarg/branches/$branch"
  echo "$cmd"
  $cmd
  if (! -e $branch) then
    echo "Directory '$parentdir/$branch' does not exist."
    echo "The SVN command might have failed. Can't continue."
    exit
  endif
else
  if (! -e $branch) then
    echo "Directory '$parentdir/$branch' does not exist. You either need"
    echo "to let this script try to check it out with SVN, or check it"
    echo "out yourself. Don't use the '-x' option if you want this script
    echo "to check it out for you."
    exit
  endif
endif

#
# Get the version, if not already set by user.
#
if ($version == "") then
  set version = `cat $branch/version`
endif

if ($?NCARGONLY) then
  set src_prefix = "ncarg-$version"
  set tar_prefix = "ncarg_src"
else if ($?NCARGNCL) then
  set src_prefix = "ncl_ncarg-$version"
  set tar_prefix = "ncl_ncarg_src"
else
  set src_prefix = "ncl_ncarg_internal-$version"
  set tar_prefix = "ncl_ncarg_internal_src"
endif
set src_tar_dir = "$parentdir/$src_prefix"
 
#
# What if there's already a $src_prefix directory?
#
cd $parentdir
if (-e $src_prefix) then
  echo "Directory $parentdir/$src_prefix already exists." 
  echo "I have to remove it, otherwise this script won't work."
  echo -n "Do you want me to remove it? (y/n) "
  set answer="$<"
  if ("$answer" != "y") then
    echo "I can't continue, since $src_prefix exists."
    exit
  endif
  set cmd = "/bin/rm -rf $parentdir/$src_prefix"
  echo "$cmd"
  $cmd
endif

if ($extra != "") then
  set tar_prefix = "{$tar_prefix}_{$extra2}_{$extra}"
else 
  set tar_prefix = "{$tar_prefix}_{$extra2}"
endif

set tar_filename = "$tar_prefix-$version.tar"

# 
# Set up directories and files to remove (that is, that we don't
# want to give to the user).
#

set ni_stuff = "ni/src/ngi ni/src/lib/xcb ni/src/lib/ngo ni/src/ncl/.cvsignore"
set ext_stuff = "external/fftpack5 external/sphere3.1 external/sphere3_dp external/g2clib-1.1.9"
set tri_stuff = "ni/src/lib/hlu/triangle.c ni/src/lib/hlu/triangle.h"
set stuff_to_always_remove = "pynio"

if ($?NCARGONLY) then       # NCAR Graphics only, for users.
  set more_stuff_to_remove = "install ni external"
else if ($?NCARGNCL) then   # NCL and NCAR Graphics, for users.
  if ($?INC_TRI) then
    set more_stuff_to_remove = "install $ext_stuff $ni_stuff"
  else 
    set more_stuff_to_remove = "install $ext_stuff $ni_stuff $tri_stuff"
  endif
else  # NCL and NCAR Graphics, for internal use, not for outside users
  set more_stuff_to_remove = "$ext_stuff $ni_stuff"
endif

set stuff_to_remove = "$stuff_to_always_remove $more_stuff_to_remove"

foreach f($stuff_to_remove)
  set cmd = "/bin/rm -rf $parentdir/$branch/$f"
  echo "$cmd"
  $cmd
end

#
# If requested, remove the given tag before we remove the
# "SVN" directories.
#
# NOT SURE ABOUT THIS CODE WITH SVN, SO COMMENTED OUT FOR NOW.
#
#if ($?remove_tag_version) then
#  cd $parentdir
#  svn tag -d $tag_version ncarg
#endif
  
#
# If requested, tag the directories as they are, before we remove the
# "SVN" directories.
#
# NOT SURE ABOUT THIS CODE WITH SVN, SO COMMENTED OUT FOR NOW.
#
#if ($?tag_version) then
#  cd $parentdir
#  cvs tag $tag_version ncarg
#endif
  
# 
# Remove "SVN" directories.
#
cd $parentdir/$branch
set cmd = "find . -name '.svn' -type d -exec /bin/rm -rf {} \;"
echo "$cmd"
find . -name '.svn' -type d -exec /bin/rm -rf {} \;
cd $parentdir

#
# Rename the parent "$branch" directory to something that indicates
# what kind of source we have, and what version.
#
mv $branch $src_prefix

echo -n "The tar file will be called $tar_filename and it will be put in $tardir. Continue? (y/n) "
set answer="$<"
if ("$answer" != "y") then
  exit
endif

#
# Tar up source directory
#
echo "Tarring up the files..."
tar -chf $tardir/$tar_filename $src_prefix

#
# Compress file.
#
echo "Gzipping the tar file..."
gzip $tardir/$tar_filename
if (-f $tardir/$tar_filename.gz) then 
  echo "Created '$tardir/$tar_filename.gz'..."
else
  echo "Error, no compressed tar file created."
  exit
endif

if ($?SCP) then
  echo "scp (via topaz.scd) or move file to $scpdir..."
  if ("$name" == "topaz.scd") then
    /bin/mv $tardir/$tar_filename.gz $scpdir
  else
    scp $tardir/$tar_filename.gz $scpmach\:$scpdir
  endif
endif

exit 0

usage:
echo "usage: create_src_dist [options]"
echo ""
echo "   Options:   -r : root directory"
echo "              -d : tar file directory"
echo "              -c : scp to topaz.scd.ucar.edu"
echo "              -t : include triangle code in HLU directory"
echo "              -g : Tag the code with the given version number. This"
echo "                   can only be done if code is checked-out with SVN."
echo "              -D : Remove the given SVN tag"
echo "              -v : version number"
echo "              -e : extra string to include in tarfile name"
echo "              -1 : tar up NCARG source files only"
echo "              -i : tar up source for internal use"
echo ""
exit
