#/bin/csh -f
# this file is sourced using the command 'psconfig'

# list the defined psconfig versions
set show_prefix = 0
set show_libs = 0
set show_bin = 0
set show_arch = 0
set show_path = 0
set show_include = 0
set show_configure = 0
set show_autogen = 0
set show_perlbuild = 0
set show_perldir = 0
set show_perl5lib = 0
set show_psversion = 0
set show_psconfdir = 0
set show_ld_library_path = 0
set show_pkg_config_path = 0
set show_aclocal_flags = 0
set args = ""
while ($#argv) 
  switch ($argv[1])
    case --help:
      goto help;
    case --list:
      /bin/ls $PSCONFDIR | grep -v "\<man\>" | grep -v "\<share\>" | awk -v dir=$PSCONFDIR -F. '{printf "%s : %-10s : %s\n", dir, $1, $2}'
      exit 0
    case --prefix:
      set show_prefix = 1
      breaksw
    case --libs:
      set show_libs = 1
      breaksw
    case --bin
      set show_bin = 1
      breaksw
    case --include
      set show_include = 1
      breaksw
    case --psconfigure:
      set show_configure = 1
      breaksw;   
    case --psautogen:
      set show_autogen = 1
      breaksw;
    case --psperlbuild:
      set show_perlbuild = 1
      breaksw;
    case --perldir:
      set show_perldir = 1
      breaksw;
    case --perl5lib:
      set show_perl5lib = 1
      breaksw;
    case --psversion
      set show_psversion = 1
      breaksw;
    case --psconfdir
      set show_psconfdir = 1
      breaksw;
    case --ld_library_path
      set show_ld_library_path = 1
      breaksw;
    case --pkg_config_path
      set show_pkg_config_path = 1
      breaksw;
    case --arch
      set show_arch = 1
      breaksw;
    case --path
      set show_path = 1
      breaksw;
    case --aclocal_flags
      set show_aclocal_flags = 1
      breaksw;
    case -*:
      echo "unknown option $1"
      goto help;
    default:
      set args=($args $1);
      breaksw;
  endsw
  shift
end
if ($#args != 1) goto usage
if ("$args" == "") goto usage

# make this configurable by the user
setenv PSCONFDIR {$HOME}/psconfig
setenv PSVERSION default

if (-e $HOME/.psconfigrc) then
  source $HOME/.psconfigrc
endif

setenv PSVERSION $args[1]

if ($?LD_LIBRARY_PATH == 0) setenv LD_LIBRARY_PATH
if ($?PKG_CONFIG_PATH == 0) setenv PKG_CONFIG_PATH
if ($?PERL5LIB == 0) setenv PERL5LIB

# identify system architecture
set sys=`uname -s` 
if ("$sys" == "SunOS") then
 set ver = `uname -r | awk '{print substr($1,1,1)}'`;
 if ($ver > 4) then 
   set sys = "Solaris"
 endif
endif
if ("$sys" == "Linux") then
 grep "Sidious" /etc/issue > /dev/null
 if ($status == 0) then 
  set sys = Sidious
 endif
 grep "Red Hat Enterprise" /etc/issue > /dev/null
 if ($status == 0) then 
  set sys = RedHat
 endif
endif

# determine architecture-dependent paths & variables for all shells
switch ($sys)
 case IRIX64:
   setenv ARCH irix;
   breaksw;

 case Solaris:
   setenv ARCH sol
   breaksw;

 case SunOs:
   setenv ARCH sun4
   breaksw;

 case Linux:
   setenv ARCH linux;
   set mach=`uname -m`
   if ("$mach" == "x86_64") setenv ARCH lin64
   breaksw;

 case Sidious:
   setenv ARCH sid;
   breaksw;

 case RedHat:
   setenv ARCH linrh;
   breaksw;

 case HP-UX:
    setenv ARCH hp;
    breaksw;

 default:
   echo "unknown architecture";
   setenv ARCH unknown;
   breaksw;
endsw
 
setenv PSCONFIG $PSVERSION.$ARCH

set newpath = ""
set pathlist = `echo $PATH | tr ':' '\n'`
foreach name ($pathlist)
  echo $name | grep $PSCONFDIR > /dev/null
  if ($status == 0) continue
  if ($newpath == "") then
    set newpath = {$name}:
  else
    set newpath = {$newpath}{$name}:
  endif
end
set bindir = {$PSCONFDIR}/{$PSCONFIG}/bin
if ("$PSCONFIG" == "none") then
  setenv PATH {$newpath}
else
  setenv PATH {$bindir}:{$newpath}
endif

set mandir  = {$PSCONFDIR}/man
set newpath = ""
set pathlist = `echo $MANPATH | tr ':' '\n'`
foreach name ($pathlist)
  echo $name | grep $PSCONFDIR > /dev/null
  if ($status == 0) continue
  if ($newpath == "") then
    set newpath = {$name}:
  else
    set newpath = {$newpath}{$name}:
  endif
end
if ("$PSCONFIG" == "none") then
  if ("$newpath" == "") then
    unsetenv MANPATH
  else
    setenv MANPATH {$newpath}
  endif 
else
  if ("$newpath" == "") then
    setenv MANPATH {$mandir}:
  else
    setenv MANPATH {$mandir}:{$newpath}
  endif 
endif 

set libdir  = {$PSCONFDIR}/{$PSCONFIG}/lib
set newpath = ""
set pathlist = `echo $LD_LIBRARY_PATH | tr ':' '\n'`
foreach name ($pathlist)
  echo $name | grep $PSCONFDIR > /dev/null
  if ($status == 0) continue
  if ($newpath == "") then
    set newpath = {$name}:
  else
    set newpath = {$newpath}{$name}:
  endif
end
if ("$PSCONFIG" == "none") then
  if ("$newpath" == "") then
    unsetenv LD_LIBRARY_PATH
  else
    setenv LD_LIBRARY_PATH {$newpath}
  endif 
else
  if ("$newpath" == "") then
    setenv LD_LIBRARY_PATH {$libdir}:
  else
    setenv LD_LIBRARY_PATH {$libdir}:{$newpath}
  endif 
endif 

# Build wants to put things in prefix/*, MakeMaker wants to put them in prefix/perl5/*
set plibdir  = {$PSCONFDIR}/{$PSCONFIG}/lib
# set plib5dir = {$PSCONFDIR}/{$PSCONFIG}/lib/perl5
set newpath = ""
set pathlist = `echo $PERL5LIB | tr ':' '\n'`
# build newpath with contents of PERL5LIB excluding PSCONFDIR entries
foreach name ($pathlist)
  echo $name | grep $PSCONFDIR > /dev/null
  if ($status == 0) continue
  if ($newpath == "") then
    set newpath = {$name}:
  else
    set newpath = {$newpath}{$name}:
  endif
end
if ("$PSCONFIG" == "none") then
  if ("$newpath" == "") then
    unsetenv PERL5LIB
  else
    setenv PERL5LIB {$newpath}
  endif 
else
  if ("$newpath" == "") then
    setenv PERL5LIB {$plibdir}:
    # setenv PERL5LIB {$plibdir}:{$plib5dir}:
  else
    setenv PERL5LIB {$plibdir}:{$newpath}
    # setenv PERL5LIB {$plibdir}:{$plib5dir}:{$newpath}
  endif 
endif 

set newpath = ""
set pathlist = `echo $PKG_CONFIG_PATH | tr ':' '\n'`
foreach name ($pathlist)
  echo $name | grep $PSCONFDIR > /dev/null
  if ($status == 0) continue
  if ($newpath == "") then
    set newpath = {$name}:
  else
    set newpath = {$newpath}{$name}:
  endif
end
if ("$PSCONFIG" == "none") then
  if ("$newpath" == "") then
    unsetenv PKG_CONFIG_PATH
  else
    setenv PKG_CONFIG_PATH {$newpath}
  endif
else
  if ("$newpath" == "") then
    setenv PKG_CONFIG_PATH {$PSCONFDIR}/{$PSCONFIG}/lib/pkgconfig:
  else
    setenv PKG_CONFIG_PATH {$PSCONFDIR}/{$PSCONFIG}/lib/pkgconfig:{$newpath}
  endif
endif

if ("$PSCONFIG" == "none") then
  alias  psconfigure configure
  alias  psautogen autogen.sh
  alias  psperlbuild perl Build.PL
else
  alias  psconfigure configure --prefix={$PSCONFDIR}/{$PSCONFIG} --bindir=$bindir --libdir=$libdir --includedir={$PSCONFDIR}/{$PSCONFIG}/include --sysconfdir={$PSCONFDIR}/{$PSCONFIG}/etc --datadir={$PSCONFDIR}/share --mandir={$PSCONFDIR}/man
  alias  psautogen autogen.sh --bindir=$bindir --libdir=$libdir --mandir=$mandir --includedir={$PSCONFDIR}/{$PSCONFIG}/include --sysconfdir={$PSCONFDIR}/{$PSCONFIG}/etc --datadir={$PSCONFDIR}/share
  alias  psperlbuild perl Build.PL --prefix {$PSCONFDIR}/{$PSCONFIG} --install_path script=$bindir --install_path arch=$bindir --install_path bin=$bindir --install_path lib=$libdir --install_path bindoc={$PSCONFDIR}/man/man1 --install_path libdoc={$PSCONFDIR}/man/man3
  setenv ACLOCAL_FLAGS "-I $PSCONFDIR/share/aclocal"
endif

# list selected environment variables
if ($show_prefix) then
  echo $PSCONFDIR/$PSVERSION.$ARCH
  exit 0
endif
if ($show_libs) then
  echo $PSCONFDIR/$PSVERSION.$ARCH/lib
  exit 0
endif
if ($show_bin) then
  echo $PSCONFDIR/$PSVERSION.$ARCH/bin
  exit 0
endif
if ($show_include) then
  echo $PSCONFDIR/$PSVERSION.$ARCH/include
  exit 0
endif

# list the defined psconfig versions
if ($show_configure) then
  alias psconfigure 
  exit 0
endif

# list the defined psconfig versions
if ($show_autogen) then
  alias psautogen
  exit 0
endif

# list the defined psconfig versions
if ($show_perlbuild) then
  alias psperlbuild
  exit 0
endif

if ($show_perldir) then
  echo $plibdir
  exit 0
endif

if ($show_perl5lib) then
  echo $PERL5LIB
  exit 0
endif

if ($show_psversion) then
  echo $PSVERSION
  exit 0
endif

if ($show_psconfdir) then
  echo $PSCONFDIR
  exit 0
endif

if ($show_ld_library_path) then
  echo $LD_LIBRARY_PATH
  exit 0
endif

if ($show_pkg_config_path) then
  echo $PKG_CONFIG_PATH
  exit 0
endif

if ($show_arch) then
  echo $ARCH
  exit 0
endif

if ($show_path) then
  echo $PATH
  exit 0
endif

if ($show_aclocal_flags) then
  echo $ACLOCAL_FLAGS
  exit 0
endif



exit 0

usage:
  if (! $?PSCONFDIR) echo "PSCONFDIR is not set : run psconfig with an argument to setup"
  if (! $?PSVERSION) echo "PSVERSION is not set : run psconfig with an argument to setup"
  if (! $?ARCH)      echo "ARCH is not set : run psconfig with an argument to setup"
  echo $PSCONFDIR : $PSVERSION : $ARCH 
  echo "psconfig --help for additional info"
  exit 2

help:
  echo "psconfig: set or show the current pslib configuration information"
  echo "USAGE: psconfig (version) : set configuration to specified version"
  echo "       psconfig --bin     : return the current path"
  echo "       psconfig --libs    : return the current library path"
  echo "       psconfig --include"
  echo 
  echo "       psconfig --prefix  : show prefix directory"
  echo "       psconfig --psconfigure"
  echo "       psconfig --psautogen"
  echo "       psconfig --psperlbuild"
  echo "       psconfig --perldir"
  echo "       psconfig --perl5lib"
  echo "       psconfig --psversion"
  echo "       psconfig --psconfdir"
  echo "       psconfig --ld_library_path"
  echo "       psconfig --pkg_config_path"
  echo "       psconfig --arch"
  echo "       psconfig --path"
  echo "       psconfig --aclocal_flags"
  echo
  echo "       psconfig --list    : list currently availabe configuration versions"
  echo "       psconfig --help    : this listing"
  echo " use ~/.psconfigrc to set PSCONFDIR as desired"
  exit 1
