#!/bin/sh NeXtMidas NMD script must be sourced
#
# NeXtMidas source tree directory changer
# @author  J. Czechowski IV
# @version $Id: nmdir.sh,v 1.1 2015/06/05 18:14:18 jxc Exp $
################################################################################

if [ -n "$1" ]; then
  export NM_DIR=$1
fi
if [ -n "$2" ]; then
  export NM_OPT=$2
fi
if [ -n "$1" ]; then
  if [[ "$NM_DIR" == "AUX"* || "$NM_DIR" == "aux"* ]]; then
    # Change to AUX directory
    aux_name="`echo $NM_DIR | sed /=/s//./`"
    nmd_path="`$NM_OS/nmsf $aux_name`"
    if [ "$nmd_path" == "NotFound" ]; then
      echo "NMD: $aux_name not defined."
    else
      nmd_path="`echo $nmd_path | sed /+/s//$USER/`"
      cd $nmd_path
    fi
  elif [ "$NM_DIR" == "home" ]; then
    # Change to home directory
    cd `$NM_OS/nmsf aux.home`
  elif [ -e $NMROOT/nxm/$NM_OPT ]; then
    # Change to opt/area (try the easy way first)
    cd $NMROOT/nxm/$NM_OPT/$NM_DIR
  else
    # Change to opt/area (do it the hard way)
    nmd_path="`$NM_OS/nmsf OPT.$NM_OPT`"
    if [ "$nmd_path" == "NotFound" ]; then
      nmd_path=$NMROOT
    fi
    cd $nmd_path/nxm/$NM_OPT/$NM_DIR
  fi
fi
# print "current" working directory to display
pwd
