#!/bin/tcsh
# vsp: virtue start project
set CONDA_ENVS_ROOT = "/prj/ids/ids-conda/envs-production"

# Display the help when no arguments are provided
if ($# == 0) then
	vsp -h
	exit 0
endif

# Get the directory of this script
#set rootdir = `dirname $0`
#set abs_rootdir = `cd $rootdir && pwd`

# Defaults
set conda_env="virtuoso"
set dev="false"
set no_ids="false"
set VIEWPRJ_CMD="false"

# Parse input options
set opts=(`getopt -s tcsh -o cdhon:p:v -l code,dev,help,no,name,prefix,view -- $argv:q`)
if ($? != 0) then 
  echo "Terminating..." >/dev/stderr
  exit 1
endif
# Now we do the eval part. As the result is a list, we need braces. But they
# must be quoted, because they must be evaluated when the eval is called.
# The 'q` stops doing any silly substitutions.
eval set argv=\($opts:q\)

while (1)
	switch($1:q)
	case -c:
	case --code:
		echo "code option set"
		setenv SP_START_CODE "TRUE"
		shift
		breaksw
	case -d:
	case --dev:
		# set conda_env="IDS-dev" 
		echo "dev option set"
		setenv IDS_DEV_ROOT "/prj/crdc_dev/${USER}"
		shift
		breaksw
	case -n:
	case --name:
		echo "named conda environment set to $2:q"
		set conda_env=\`$2:q\'  
		shift ; shift
		breaksw
	case --o:
	case --no:
		echo "ids skill environment disabled (-o / --no)"
		set no_ids="true" ; shift
		breaksw
	case -p:
	case --prefix:
		echo "conda environment prefix set to $2:q"
		set CONDA_PREFIX_SP=$2 ; shift ; shift
		breaksw
	case -v:
	case --view:
		echo "Opening virtuoso session in view mode (viewprj)"
		set VIEWPRJ_CMD="true" ; shift
		breaksw
	case -h:
	case --help:
		echo ""
		echo "Virtue Start Project (vsp)"
		echo " vsp [Options] [prj_name]"
		echo "   Starts a cadence project in the IDS environment"
		echo " Example:"
		echo "   sp sky58270_mk5872ASM_CR"
		echo " Options:"
		echo "   -c, --code: Starts vs code in the project's code workspace"
		echo "     instead of running virtuoso"
		echo "   -d, --dev: Development mode which sets the IDS_DEV_ROOT"
		echo "     variable to /prj/crdc_dev/${USER} This is then used by each"
		echo "     project's activate script to run from there"
		echo "   -h, --help:	Display this help message"
		echo "   --no: Don't load any SKILL environment,"
		echo "     just start the project"
		echo "   -n, --name: Uses the conda environment specified by the name given"
		echo "     If both -n and -p are specified then -p is used"
		echo "   -p, --prefix: Uses the conda environment specified by the prefix given"
		echo "     If both -c and -p are specified then -p is used"
		echo "   -v, --view: Uses the viewprj command to open the virtuoso session"
		echo ""
		exit 0
	case --:
		shift
		break
	default:
		echo "Internal error!" ; exit 1
	endsw
end

if ($#argv == 0) then
	echo "error: You must specify the project name as the last argument or -h / --help"
	echo "   The gp or grep_prj commands can be used to search for a project"
endif

setenv PRJ_NAME $1:q
set argv = ()

if (${VIEWPRJ_CMD} == "false") then
	cdsprj ${PRJ_NAME}
else
	viewprj ${PRJ_NAME}
endif



# Setup Conda env
if ( ! $?CONDA_PREFIX_SP ) then
  set CONDA_PREFIX_SP="${CONDA_ENVS_ROOT}/${conda_env}"
  echo "Setting up ${conda_env} conda environment"
endif
echo "Setting up conda environment at:"
echo "   $CONDA_PREFIX_SP"

echo "activating conda"
if ( -d "$CONDA_PREFIX_SP" ) then
	# Required in tcsh scripts, similar to the interactive "conda activate"
	source ${CONDA_PREFIX_SP}/etc/profile.d/conda.csh
	conda activate $CONDA_PREFIX_SP
	conda info
	echo "   Done, Activated conda environment"
else
   echo "   No Conda evironment found at $CONDA_PREFIX_SP"
endif


if ( $?SP_START_CODE ) then
	mkdir -p ../../${PRJ_NAME}_code
	cd ../../${PRJ_NAME}_code
	code .
else
	if ( -r "${CONDA_PREFIX_SP}/lib/skill/IDS.init.ils" && ${no_ids} == "false" ) then
		echo "Starting Virtuoso with IDS SKILL env initialization script:"
		echo "  ${CONDA_PREFIX_SP}/lib/skill/IDS.init.ils"
		icfb -replay "${CONDA_PREFIX_SP}/lib/skill/IDS.init.ils"
	else
		if ( ! -r "${CONDA_PREFIX_SP}/lib/skill/IDS.init.ils" ) then
			echo "Did not find IDS SKILL env initialization script:"
			echo "  ${CONDA_PREFIX_SP}/lib/skill/IDS.init.ils"
		endif
		if ( ${no_ids} == "true" ) then
			echo "IDS disabled"
		endif
		echo "Starting Virtuoso with no IDS SKILL env initialized"
		icfb
	endif
endif
