#!/bin/sh


# The location where we are going to install the product
myPath=/cvmfs/osg.mwt2.org/osg/sw


function wn_client_install () {

  # Version of OSG WN-Client
  myVersion=$1

  # Remove the trailing -N
  myVer=${myVersion%-*}

  # EL should be either el5 or el6
  myEL=$2

  # arch should be either x86_64 or i386
  myArch=$3

  # Location where osg-wn-client will be installed
  myRoot=$myPath/osg-wn-client/$myEL/$myArch

  # Name of the tarball to download and install
  myTB=osg-wn-client-$myVersion.$myEL.$myArch.tar.gz

  # Make certain the install area exists
  mkdir -p $myRoot

  # Remove the old
  echo "Removing $myRoot/$myVersion"
  rm -fr $myRoot/$myVersion

  # Remove any old copies of the tarball
  (cd $myPath; rm -f $myTB)

  # Download the new
  echo "Downloading $myTB"
  rm -f $myTB
  (cd $myPath; wget --quiet http://repo.grid.iu.edu/tarball-install/$myVer/$myTB)

  # Unpack it
  echo "Unpacking $myTB into $myRoot"
  (cd $myRoot; tar xzf $myPath/$myTB; mv osg-wn-client $myVersion)

  # Remove the tarball
  echo "Removing tarball $myTB"
  (cd $myPath; rm $myTB)

  # Post install
  $myRoot/$myVersion/osg/osg-post-install


  # Determine the library path from the architecture

  case $myArch in
  (i386)   myLib=lib   ;;
  (x86_64) myLib=lib64 ;;
  (*)      myLib=lib64 ;;
  esac


  # Create the setup-local.[sh,csh]

  rm -f $myRoot/$myVersion/setup-local.sh
  rm -f $myRoot/$myVersion/setup-local.csh

cat <<EOF>>/$myRoot/$myVersion/setup-local.sh
#!/bin/sh

# Add a callout for a global setup-local.sh

[ -f $myPath/osg-wn-client/setup-local.sh ] && source $myPath/osg-wn-client/setup-local.sh

EOF


cat <<EOF>>/$myRoot/$myVersion/setup-local.csh

# Add a callout for a global setup-local.csh

if -r "$myPath/osg-wn-client/setup-local.csh" then
  source $myPath/osg-wn-client/setup-local.csh
endif

EOF


  # Connect to the install
#  source $myRoot/$myVersion/setup.sh

  # Setup the Certificate Authority
#  osg-ca-manage setupCA --url osg

  # Fix for the EL5 version of fetch-crl
#  [[ $myEL == "el5" ]] && ln -s fetch-crl3 $OSG_LOCATION/usr/sbin/fetch-crl

  # Update all the CRLs
#  fetch-crl

}

function wn_client_current () {

  # Version of OSG WN-Client
  myVersion=$1

  # EL should be either el5 or el6
  myEL=$2

  # arch should be either x86_64 or i386
  myArch=$3

  # Location where osg-wn-client will be installed
  myRoot=$myPath/osg-wn-client/$myEL/$myArch

  # Make this release current
  rm -f $myRoot/current
  ln -s $myVersion $myRoot/current

  # Create a shortcut setup_elX.sh file
# echo "$myPath/osg-wn-client/$myEL/\$(/bin/uname -i)/current/setup.sh" > $myPath/osg-wn-client/setup_$myEL.sh
  
}










# Install EL5 releases
 
#wn_client_install 3.1.18-1 el5 x86_64
#wn_client_install 3.1.18-1 el5 i386


# Which EL5 release is current

wn_client_current 3.1.18-1 el5 x86_64
wn_client_current 3.1.18-1 el5 i386




# Install EL6 releases

#wn_client_install 3.1.18-1 el6 x86_64
#wn_client_install 3.1.18-1 el6 i386


# Which EL6 release is current

wn_client_current 3.1.18-1 el6 x86_64
wn_client_current 3.1.18-1 el6 i386









# Create a global setup.sh

rm -f $myPath/osg-wn-client/setup.sh

cat <<EOF>>/$myPath/osg-wn-client/setup.sh
#!/bin/sh

# Version we should load
myVersion=\$1

# If no version given, default to current
[[ -z \$myVersion ]] && myVersion=current

# Base location of the product
myBase=$myPath/osg-wn-client

# The machine architecture
myArch=\$(/bin/uname -i)

# The RHEL Release level (el5 or el6)

myEL=unknown

if [[ -f /etc/redhat-release ]]; then

  /bin/grep -q ' 6\.' /etc/redhat-release
  [[ $? -eq 0 ]] && myEL=el6

  /bin/grep -q ' 5\.' /etc/redhat-release
  [[ $? -eq 0 ]] && myEL=el5

fi

# Set it up
source \$myBase/\$myEL/\$myArch/\$myVersion/setup.sh

EOF



# Create a global setup-local.sh

rm -f $myPath/osg-wn-client/setup-local.sh

cat <<EOF>>/$myPath/osg-wn-client/setup-local.sh
#!/bin/sh

export X509_CERT_DIR="/cvmfs/osg.mwt2.org/osg/CA/certificates"

#export X509_CERT_DIR="/etc/grid-security/certificates"
#export X509_VOMS_DIR="/etc/grid-security/vomsdir"

EOF


# Create a global setup-local.csh

rm -f $myPath/osg-wn-client/setup-local.csh

cat <<EOF>>/$myPath/osg-wn-client/setup-local.csh

setenv X509_CERT_DIR "/cvmfs/osg.mwt2.org/osg/CA/certificates"

#setenv X509_CERT_DIR "/etc/grid-security/certificates"
#setenv X509_VOMS_DIR "/etc/grid-security/vomsdir"

EOF

