Go to the documentation of this file.00001 #! /bin/bash
00002 ##############################################################################
00003 # @file basistest.sh
00004 # @brief Common wrapper for the basistest subcommands.
00005 #
00006 # Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00007 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00008 #
00009 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00010 #
00011 # @ingroup Tools
00012 ##############################################################################
00013
00014
00015 # constants used by the shflags.sh module
00016 HELP_COMMAND='basistest (BASIS)'
00017 HELP_CONTACT='SBIA Group <sbia-software at uphs.upenn.edu>'
00018 HELP_VERSION='version 1.2.3 (revision 2104)'
00019 HELP_COPYRIGHT='Copyright (c) University of Pennsylvania. All rights reserved.
00020 See https://www.rad.upenn.edu/sbia/software/license.html or COPYING file.'
00021
00022
00023 # ----------------------------------------------------------------------------
00024 ## @brief Get real path of given file or directory.
00025 #
00026 # @note This function was substituted by BASIS either for the string
00027 # \@BASIS_BASH_UTILITIES\@ or \@BASIS_BASH_FUNCTION_realpath\@.
00028 #
00029 # Example:
00030 # @code
00031 # exec_dir=`realpath $0`
00032 # @endcode
00033 #
00034 # @param [in] path File or directory path.
00035 #
00036 # @returns Canonical path.
00037 #
00038 # @sa http://stackoverflow.com/questions/7665/how-to-resolve-symbolic-links-in-a-shell-script
00039 function realpath
00040 {
00041 local path=$1
00042
00043 local linkdir=''
00044 local symlink=''
00045
00046 while [ -h ${path} ]; do
00047 # 1) change to directory of the symbolic link
00048 # 2) change to directory where the symbolic link points to
00049 # 3) get the current working directory
00050 # 4) append the basename
00051 linkdir=$(dirname -- "${path}")
00052 symlink=$(readlink ${path})
00053 path=$(cd "${linkdir}" && cd $(dirname -- "${symlink}") && pwd)/$(basename -- "${symlink}")
00054 done
00055
00056 echo -n "$(cd -P -- "$(dirname "${path}")" && pwd -P)/$(basename -- "${path}")"
00057 }
00058
00059 readonly _SBIA_BASIS_BASISTEST_DIR="$(dirname -- "$(realpath "$(cd -P -- "$(dirname -- "${BASH_SOURCE}")" && pwd -P)/$(basename -- "$BASH_SOURCE")")")"
00060 source "${_SBIA_BASIS_BASISTEST_DIR}/../lib/basis.sh" || exit 1
00061
00062
00063 # ============================================================================
00064 # constants
00065 # ============================================================================
00066
00067 get_executable_name _EXEC_NAME && readonly _EXEC_NAME
00068 get_executable_directory _EXEC_DIR && readonly _EXEC_DIR
00069
00070 # ============================================================================
00071 # help
00072 # ============================================================================
00073
00074 # ----------------------------------------------------------------------------
00075 ## @brief Print help.
00076 function print_help
00077 {
00078 print_synopsis
00079 echo
00080 cat - << EOF-DESCRIPTION
00081 Description:
00082 This executable is a wrapper for the basistest subcommands. The name of the
00083 subcommand to execute must be given as first argument.
00084 EOF-DESCRIPTION
00085 echo
00086 print_options
00087 echo
00088 print_contact
00089 }
00090
00091 # ----------------------------------------------------------------------------
00092 ## @brief Print usage information.
00093 function print_helpshort
00094 {
00095 print_synopsis
00096 echo
00097 print_options
00098 echo
00099 print_contact
00100 }
00101
00102 # ----------------------------------------------------------------------------
00103 ## @brief Print synopsis, i.e., usage section.
00104 function print_synopsis
00105 {
00106 cat - << EOF-SYNOPSIS
00107 Usage:
00108 ${_EXEC_NAME} <cmd> [options] [options of subcommand]
00109 ${_EXEC_NAME} help <cmd>
00110 ${_EXEC_NAME} [options]
00111 EOF-SYNOPSIS
00112 }
00113
00114 # ----------------------------------------------------------------------------
00115 ## @brief Print options.
00116 function print_options
00117 {
00118 cat - << EOF-OPTIONS
00119 Options:
00120 <cmd> Recognized subcommands are cron, master, slave, and svn.
00121 --help, -h Print help and exit
00122 --helpshort Print short help and exit.
00123 EOF-OPTIONS
00124 }
00125
00126 # ============================================================================
00127 # options
00128 # ============================================================================
00129
00130 cmd='slave' # subcommand to run
00131 args=() # options of subcommand
00132 verbose=0 # verbosity of output messages
00133
00134 if [ -n "$1" ]; then
00135 if match "$1" '^(cron|master|slave|svn)$'; then
00136 cmd="$1"
00137 shift
00138 elif [ "$1" == "help" ]; then
00139 if [ -n "$2" ]; then
00140 if match "$2" '^(cron|master|slave|svn)$'; then
00141 cmd="$2"
00142 shift
00143 else
00144 echo "Unknown command: $2" 1>&2
00145 exit 1
00146 fi
00147 else
00148 echo "Missing subcommand! See ${_EXEC_NAME} --help." 1>&2
00149 exit 1
00150 fi
00151
00152 exec "${_EXEC_DIR}/basistest-${cmd}" '--help'
00153 elif [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
00154 print_help
00155 exit 0
00156 elif [ "$1" == "--helpshort" ]; then
00157 print_helpshort
00158 exit 0
00159 elif [ "$1" == "--version" ]; then
00160 print_version 'basistest'
00161 exit 0
00162 fi
00163 fi
00164
00165 # ============================================================================
00166 # main
00167 # ============================================================================
00168
00169 [ -n "${cmd}" ] || { echo "Missing subcommand! See ${_EXEC_NAME} --help."; exit 1; }
00170 exec "${_EXEC_DIR}/basistest-${cmd}" "$@"