#
# nios_csh - set up nios development environment for 'csh' shell
#          ($sopc_builder must point to installation directory)
#
# Example:
#	setenv sopc_builder /usr/altera/excalibur/sopc_builder_2_5
#	source $sopc_builder/bin/nios_csh
#

# get PTF assignment from specified file (if assignment
# does not exist, variable is not modified)
alias get_ptf_assignment 'if ( -f \!:1 ) then; set afile=\!:1; set aname=\!:2; \
   set aval=`cat $afile | grep "[ ]*${aname}[ ]*=.*" | sed -e "s/[ ]*=[ ]*/=/g"`; \
   if ( "$aval" != "" ) eval "set $aval";\
endif'

alias add_sopc_paths 'if ( -f \!:1 ) then; set f=\!:1; set sopc_paths=""; \
   foreach y (`cat $f | perl -e "binmode STDOUT; while (<>) { s/\015/ /sg; s/\012/ /; print; }"`) \
      set x=`safe_path $y`; \
      if ( -d $x ) then; set sopc_paths="$x":"$sopc_paths" ; endif; \
   end\
   endif'

# safe_path, safe_path_m: aliases to make path useable
# in a patch
# cygwin: disable, other: enable
alias safe_path 'echo "\!:1"'
alias safe_path_m 'echo "\!:1"'
# cygwin: enable, other: disable
#alias safe_path 'echo "`cygpath -u \!:1`"'
#alias safe_path_m 'echo "`cygpath -m \!:1`"'

if ( "$?NIOS_CSH_RUN_6_0" == "0" ) then
   setenv PATH "/bin:/usr/bin"
   # set up $sopc_builder, using existing variable
   # or <quartus>/sopc_builder
	if ( "$?sopc_builder" == "0" ) then
	   if ( "$?QUARTUS_ROOTDIR" == "0" ) then
         echo 'Attempting to base \$sopc_builder from an empty \$QUARTUS_ROOTDIR...'
      endif
      setenv sopc_builder $QUARTUS_ROOTDIR/sopc_builder
   endif

   if ( -d "$sopc_builder" ) then

      setenv sopc_home_dir $HOME/.sopc_builder

      set sopc_legacy_dir=""
      get_ptf_assignment $sopc_builder/.sopc_builder sopc_legacy_dir
      get_ptf_assignment $sopc_home_dir/.sopc_builder sopc_legacy_dir

      if (! $?PERL5LIB) setenv PERL5LIB ""
      # legacy (pre-3.0) kit: add 'bin' and its various subdirs
      if ( $sopc_legacy_dir != "" && -d "$sopc_legacy_dir" && -f "$sopc_legacy_dir/.sopc_builder" ) then
         set x=`safe_path $sopc_legacy_dir`
         setenv PATH "$x/bin":"$x/bin/FS2/Bin":"$x/bin/nios-gnupro/bin":"$x/bin/arm-gnupro/$ALTERA_ARM9GP_VER/bin":"$PATH"
         setenv PERL5LIB "$x":"$PERL5LIB"
         setenv PERL5LIB "$x/europa":"$PERL5LIB"
         setenv PERL5LIB "$x/perl_lib":"$PERL5LIB"
      endif

      # add the built-in sopc_builder path items
      set x=`safe_path "$sopc_builder"`
      set q=`safe_path "$QUARTUS_ROOTDIR"`
      setenv PATH "$q/bin:$q/bin/perl561/bin:$x/bin:$PATH"
      setenv PERL5LIB "$x/bin":"$PERL5LIB"
      setenv PERL5LIB "$x/bin/europa":"$PERL5LIB"
      setenv PERL5LIB "$x/bin/perl_lib":"$PERL5LIB"

      # add in kit shell and perl paths
      add_sopc_paths $sopc_home_dir/kit_shell_path
      setenv PATH "$sopc_paths$PATH"
      add_sopc_paths $sopc_home_dir/kit_perl_path
      setenv PERL5LIB "$sopc_paths$PERL5LIB"

      #
      # unset GCC settings which interfere with our environment
      #
      unset GCC_EXEC_PREFIX
      unset COMPILER_PATH
      unset LIBRARY_PATH

      # source all kit cshs
      if ($?SOPC_BUILDER_PATH) then
          set old_dir=`pwd`
          foreach potential_kit_dir (`echo $SOPC_BUILDER_PATH | sed -e "s/+/ /g"`)
            if ( $?potential_kit_dir && -f "$potential_kit_dir/kit_csh" ) then
              cd $potential_kit_dir
              source ./kit_csh
            endif
          cd $old_dir
          end
      endif

   echo ''
   echo ''
   echo '------------------------------------------------'
   echo 'Welcome To Altera SOPC Builder'
   echo ''
   echo 'Version 6.0, Built Mon Apr 24 15:26:45 PDT 2006'
   echo ''
   echo '------------------------------------------------'

   setenv NIOS_CSH_RUN_6_0 1
   
   else
      echo '$sopc_builder does not point to valid directory:' $sopc_builder
   endif

endif
