#!/bin/sh -

ARCH=i386
ALTARCH=i386
VERS=6.1
VERSR=6.1-1
VERSBR=6.1-1
VISAVERS=2.6-1
OLDVISA="nivisa"
CVIRTVERS=5.0.1-2
GPIBVERS="0.8.2 beta"
GPIBDIR=nigpib-0.8.2
ENETDIR=nienet-1.2
DDIR=lv61
UPGRADE=
RPM=/bin/rpm
RPMOPT="-ivh"  # Options for LabVIEW package installation
#NORPM=y

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
export PATH

rtepkg="labview61-rte-$VERSR.$ARCH.rpm"
allpkg="labview61-rte-$VERSR.$ARCH.rpm labview61-app-$VERSBR.$ARCH.rpm labview61-examples-$VERSBR.$ARCH.rpm labview61-help-$VERSBR.$ARCH.rpm labview61-ref-$VERSBR.$ARCH.rpm labview61-rt-$VERSBR.$ARCH.rpm labview61-pro-$VERSBR.$ARCH.rpm labview61-vxi-$VERSBR.$ARCH.rpm labview61-desktop-$VERSR.$ARCH.rpm"
visapkg="nivisa-$VISAVERS.$ALTARCH.rpm"
visapkgs="$visapkg nivisaserver-1.0-1.$ALTARCH.rpm"
cvirtpkg="nicvirte-$CVIRTVERS.$ALTARCH.rpm"

CDPATH="`dirname $0`"
case "$CDPATH" in
    /*) ;;
     .) CDPATH="`pwd`" ;;
     *) CDPATH="`pwd`/$CDPATH" ;;
esac

get_install_dir () {
   olddir="`pwd`"
   while :
   do
      dir=$instdir
      if [ "$RPMVERS" != 4.0.2 ]; then
         echo ""
         echo $1
         test ! -z "$2" && echo "$2"
         echo -n "[Default $dir, or 'q' to quit] "
         read ans
         if [ ! -z "$ans" ]; then
             dir=$ans
         fi
      fi
      echo ""
      case "$dir" in
	  /*) ;;
       [qQ]*) exit 1 ;;
	   *) echo "Directory must begin with /"
	      continue ;;
      esac
      if [ -f $dir ]; then
	  echo "That path already exists as a regular file!"
	  echo "A directory path is required."
	  continue
      fi
      if [ ! -d $dir ]; then
          echo -n "$dir does not exist.  Create? [yn] "
          read ans
          test -z "$ans" && ans=y
          case "$ans" in
            [yY]*) mkdir -p $dir ;;
            [qQ]*) echo "Aborted."; exit 1 ;;
	  esac
      fi
      cd $dir
      if touch foo 2>/dev/null; then
          rm -f foo
          break
      else
          echo ""
          echo "Cannot write to $dir."
      fi
   done
   instdir=$dir
   if [ "$instdir" != /usr/local ]; then
	RPMPREFIX="--prefix $instdir"
   fi
   cd $olddir
   echo ""
}

summary_rpm () {
   if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
       $RPM -qp --queryformat '%{name}: %{summary}\n' $1
   elif [ -x $CDPATH/bin/rpmq ]; then
       $CDPATH/bin/rpmq -qp --queryformat '%{name}: %{summary}\n' $1
   else
       echo "Install $1"
   fi
}

info_rpm () {
   if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
       $RPM -qip $1
   elif [ -x $CDPATH/bin/rpmq ]; then
       $CDPATH/bin/rpmq -qp --queryformat \
'Name        : %-27{NAME}\
Version     : %-27{VERSION}   Vendor: %{VENDOR}\
Group       : %-27{GROUP}     Size: %{SIZE}\
%|LICENSE?{License     : %{LICENSE}}|\
%|URL?{URL         : %{URL}}|\
Summary     : %{SUMMARY}\
Description :\n%{DESCRIPTION}\n' $1
   else
       echo "$1: can't read RPM description info"
   fi

}

install_lv6 () {
 
	echo "Preparing for installation..."

        betapkgs="`/bin/rpm -qa | egrep 'labview61-.*-0'`"
        if [ -n "$betapkgs" ]; then
	  echo ""
          echo "Previous LabVIEW 6.1 Beta packages must be removed before installation can"
                  echo "continue."
                  echo -n "Proceed? [ynq] "
          read ans
          test -z "$ans" && ans=y
          case "$ans" in
            [yY]*) 
                echo "Removing old beta release packages..."
                rpm --nodeps --allmatches -e $betapkgs # >dev/null 2>&1
                ;;
                *) echo "Aborted."; exit 1 ;;
          esac
        fi

        GLIBCV="`ldd --version | head -1 | sed -e 's/.* //'`"
        echo "C runtime library (glibc) version: $GLIBCV."
        case $GLIBCV in
           *2.1.3) ;;
           *2.1.[456789]*) ;;
           *2.[23456789]*)  ;;
           *) echo "WARNING: It does not appear that your are running GNU C Library version 2.1.3"
                  echo "         or later.  glibc-2.1.3 is required to upgrade.  See the README file"
                  echo "         for information on how to upgrade this library."                  ;;
        esac


	if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
	  RPMVERS="`$RPM --version | sed 's,^RPM version ,,'`"
          echo "Red Hat Package Manager (RPM) version: $RPMVERS."
          case "$RPMVERS" in
	    [123]*)
		echo ""
		echo "Note: RPM version $RPMVERS is too old for rpm package installation."
		echo "LabVIEW packages will be extracted directly."
		NORPM=y
		;;
	    4.0.2)
		echo ""
		echo "Note: Your system's RPM package manager (version 4.0.2) has a bug preventing"
		echo "package relocation.  No fix is yet available for this bug in a released"
		echo "version of RPM, so LabVIEW will be installed in the default location"
		echo "of /usr/local.  If you need files to be installed on a different partition,"
		echo "you may make a symbolic link from /usr/local/lv61 to another directory prior"
		echo "to installation.  You may enter 's' to skip LabVIEW installation,"
		echo "or 'q' to quit."
		echo ""
		;;
	         *)
		;;
	  esac
	fi


   if [ ! -z "$UPGRADE" ]; then
       proceed=y
       while [ "$proceed" = y ];
       do
         echo ""
         get_install_dir "Please enter the base path of your previous $UPGRADE installation." \
          "(Packages will be installed in the '$DDIR' subdirectory.)"
         echo "LabVIEW packages will be installed in $instdir/$DDIR"
    
         if [ -x "$instdir/$DDIR/labview" ]; then
            proceed=n
         else
            echo ""
	    echo "That directory does not seem to contain a LabVIEW installation."
	    echo "An existing $UPGRADE installation is required to upgrade to $VERS."
         fi
       done
   else
       get_install_dir "Where would you like to install LabVIEW packages?" \
    "(Packages will be installed in a subdirectory named '$DDIR'.)"   
   fi
   lvdir=$instdir/$DDIR
   ans=""
   asked=""
   if [ ! -z "$UPGRADE" ]; then
       echo "NOTE: This is an upgrade only; you must have $UPGRADE installed to proceed."
   fi
   echo "Please indicate whether you would like to install the following components:"
   echo ""
   for pkg in $allpkg
   do
      ask=""
      errout=">/dev/null 2>&1"
      if [ -f $pkg ]; then
          if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
		eval $RPM --ignoresize --test $RPMPREFIX $RPMOPT $pkg $errout ||
		eval $RPM --ignoresize --test $RPMPREFIX $RPMOPT $pkg $rtepkg $errout  
   	        if [ $? = 0 ]; then
		    ask=y
		fi
          else
		ask=y
	  fi
      fi
      if [ -n "$ask" ]; then
         while :
         do
	    asked=y
            if [ -n "$installpkg" ]; then
               summary_rpm $pkg
               ans=y
            else
               summary_rpm $pkg
               echo -n "[ynaq?] "
               read ans
            fi
            test -z "$ans" && ans=y
            case "$ans" in
                [yY]*) packages="$packages $pkg"
		       break ;;
                [aA]*) packages="$packages $pkg"
                       installpkg=y
		       break ;;
               [hH?]*) 
       		       info_rpm $pkg
		       echo ""
                       echo "Enter 'y' to install this package, 'n' to skip, 'a' to install all LabVIEW"
                       echo "packages, 's' to skip all, or 'q' to abort installation entirely."
		       echo "" ;;
                [qQ]*) echo "Installation aborted."
		       exit 1 ;;
                [sS]*) echo "Skipping LabVIEW installation."
                       return ;;
		    *) break ;;
            esac
         done
      fi
   done
   if [ -z "$asked" ]; then
       echo "All available LabVIEW packages have already been installed or cannot"
       echo "be installed due to package conflicts."
       return
   elif [ -z "$packages" ]; then
       echo "No LabVIEW packages were selected."
       return
   fi
   echo ""
   echo "Installing..."
   if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then

        for pkg in $packages
        do
           $RPM $RPMOPT $RPMPREFIX $pkg
        done
   else
	mkdir -p $instdir 2>/dev/null
        for pkg in $packages
        do
           $CDPATH/bin/INSTALL.norpm $pkg "$instdir"
        done
   fi
   if [ $? = 0 ]; then
	if [ -x "$instdir/$DDIR/labview"  ]; then
		if [ -f /usr/local/bin/labview -a ! -f /usr/local/bin/labview.old ]; then
			mv -f /usr/local/bin/labview /usr/local/bin/labview.old
		fi
		ln -s -f "$instdir/$DDIR/labview" /usr/local/bin/labview
	fi
	echo ""
	echo "LabVIEW package installation complete."
   else
	echo ""
	echo "LabVIEW package installation failed."
	if [ -n "$UPGRADE" ]; then
	    echo "Make sure you are installing in the same base directory as $UPGRADE."
	fi
        exit 1
   fi
}

cd $CDPATH
echo ""
if [ -f COPYRIGHT ]; then
   cat COPYRIGHT
else
   echo "LabVIEW $VERS for Linux/x86"
fi

arch="`arch`"
case $arch in
   i*86) ;;
      *)
	echo "Sorry, LabVIEW is not currently available for $arch."
	echo "Visit http://www.ni.com/support/ for search the knowledge base"
	echo "or http://www.ni.com/ask/ to obtain e-mail tech. support."
	;;
esac

#if [ -d /usr/lib/rhs -a -x $RPM ]; then
#   REL=`cat /etc/redhat-release`
#   echo You appear to be running $REL.
#fi
if [ ! -f /lib/libc.so.6 -a ! -f /usr/i?86-linux-libc6/lib/libc.so.6 ]; then
   cat <<EOF

WARNING!  You do not have glibc2 (libc.so.6) installed.
Because LabVIEW 6.1 is a multi-threaded application and
requires POSIX 1003.1 pthread support, this library is
a prerequisite for installation.

See the file Glibc2-HOWTO on this CD for information on installing glibc2
from source or binary distribution, or for additional mirror
sites.

EOF
   if [ -d /usr/lib/rhs -a -x $RPM ]; then
      echo "Since you are using RedHat Linux, you may wish to upgrade"
      echo "to RedHat 6.2 (or later), which comes with glibc2 installed"
      echo "by default.  RedHat may be reached at http://www.redhat.com."
   fi 
   echo ""
   echo -n "Try to proceed anyway? [ny] "
   read ans
   case "$ans" in
      [yY]*) ;;
          *) exit 1 ;;
   esac
else
   echo # "Prerequisite glibc2 (libc.so.6) libraries found."
fi
echo ""

instdir=/usr/local
lvdir=$instdir/$DDIR

if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
    dir="`$RPM -ql labview61-app 2>/dev/null | head -1 | xargs dirname 2>/dev/null`"
    if [ "x$dir" = x ]; then
       dir="`$RPM -q --queryformat '%{prefixes}\n' labview61-app 2>/dev/null`"
    fi
    if [ "x$dir" = x ]; then
       dir="`$RPM -q --queryformat '%{defaultprefix}\n' labview61-app 2>/dev/null`"
    fi
    case "$dir" in
       /*) instdir=$dir ;;
    esac
fi

origdir=$instdir
install_lv6

cd $CDPATH
if [ -d "acrobat" ]; then
    echo ""
    echo -n "Would you you like to install Adobe Acrobat Reader 4.05 for Linux? [ynq] "
    read ans
    test -z "$ans" && ans=y
    case "$ans" in
         [qQ]*) echo "Installation aborted."
                exit 1 ;;
         [yY]*)
             echo Invoking Adobe Systems Adobe Acrobat Reader installer...
             sleep 2
	     cd $CDPATH/acrobat
             ./INSTALL
             ;;
    esac
fi

if [ -d $instdir/Acrobat4 ]; then
    ACROREAD=$instdir/Acrobat4
elif [ -d /usr/local/Acrobat4 ]; then
    ACROREAD=/usr/local/Acrobat4
elif [ -d /usr/Acrobat4 ]; then
    ACROREAD=/usr/Acrobat4
elif [ -d /opt/Acrobat4 ]; then
    ACROREAD=/opt/Acrobat4
fi
if [ "$ACROREAD" != "" -a -d /usr/local/bin ]; then
    ln -s -f $ACROREAD/bin/acroread /usr/local/bin/acroread 2>/dev/null
    ACROREAD=/usr/local
fi
if [ -x "$ACROREAD/bin/acroread" ]; then
    ACROREAD="$ACROREAD/bin/acroread"
else
    ACROREAD=acroread
fi

cd $CDPATH
if [ -f "$cvirtpkg" ]; then
   echo ""
   echo -n "Would you you like to install the CVI Run-Time Engine ($CVIRTVERS) [needed by VISA]? [ynq] "
   read ans
   test -z "$ans" && ans=y
   case "$ans" in
       [qQ]*) echo "Installation aborted."
              exit 1 ;;
       [yY]*)
           if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
              rpm -Uvh "$cvirtpkg"
	   else
              $CDPATH/bin/INSTALL.norpm "$cvirtpkg" "$instdir"
	   fi
           ;;
   esac
fi

cd $CDPATH
if [ -f "$visapkg" ]; then
   echo ""
   echo -n "Would you you like to install NI-VISA ($VISAVERS) for Linux? [ynq] "
   read ans
   test -z "$ans" && ans=y
   case "$ans" in
       [qQ]*) echo "Installation aborted."
              exit 1 ;;
       [yY]*)
           if [ -d /var/lib/rpm -a -x $RPM -a "x$NORPM" = "x" ]; then
	      test -n "$OLDVISA" && rpm -e "$OLDVISA" >/dev/null 2>&1
	      # use --replacefiles to overwrite visa files in lv5.0.1f1 rpm
              rpm --replacefiles -Uvh $visapkgs
	   else
	      for pkg in $visapkgs
              do
                 $CDPATH/bin/INSTALL.norpm $pkg "$instdir"
              done
	   fi
           ;;
   esac
fi

cd $CDPATH
if [ -d "$CDPATH/$GPIBDIR" ]; then
   echo ""
   echo -n "Would you you like to install NI-GPIB ($GPIBVERS) for Linux? [ynq] "
   read ans
   test -z "$ans" && ans=y
   case "$ans" in
        [qQ]*) echo "Installation aborted."
               exit 1 ;;
   	[yY]*)
	    instdir="$origdir/nigpib"
	    get_install_dir "Where would you like to install NI-GPIB for Linux?"
	    if [ ! -d $instdir ]; then
 		echo Aborted.
		exit 1
	    fi
	    cd $CDPATH/$GPIBDIR
	    cp -r . $instdir
	    cd $instdir || exit 1
	    make clean >/dev/null 2>&1
            ./INSTALL
	    echo ""
	    echo "If you need to rebuild and reinstall the driver, cd to $instdir"
	    echo "and run ./INSTALL. (This is required if you upgrade your kernel.)"
            ;;
   esac
fi

cd $CDPATH
if [ -d "$CDPATH/$ENETDIR" ]; then
   echo ""
   echo -n "Would you you like to install GPIB-ENET for Linux? [ynq] "
   read ans
   test -z "$ans" && ans=y
   case "$ans" in
        [qQ]*) echo "Installation aborted."
               exit 1 ;;
   	[yY]*)
	    instdir="$origdir/nienet"
	    get_install_dir "Where would you like to install GPIB-ENET for Linux?"
	    if [ ! -d $instdir ]; then
 		echo Aborted.
		exit 1
	    fi
	    cd $CDPATH/$ENETDIR
	    cp -r . $instdir
	    cd $instdir || exit 1
	    make clean >/dev/null 2>&1
            ./instgpib
	    echo ""
            ;;
   esac
fi

if [ -n "$lvdir" -a -d "$lvdir" ]; then
   UPNOTES="$lvdir/manuals/upnot61.pdf"
   if [ -f "$UPNOTES" ]; then
      if [ -n "$DISPLAY" ]; then
	  echo ""
	  echo -n "View the LabVIEW $VERS release notes now? (y/n) "
          read ans
          case "$ans" in
             ""|y|Y) 
                 if [ -x "$ACROREAD" ]; then
                    $ACROREAD "$UPNOTES" &
                 elif [ -x /usr/bin/xpdf ]; then
                    /usr/bin/xpdf "$UPNOTES" &
                 else
                    echo "Warning: Adobe Acrobat Reader or xpdf must be installed to view PDF files."
                    $ACROREAD "$UPNOTES" &
                 fi
                 ;;
          esac
      else
          echo "Please see the LabVIEW Upgrade Notes in $UPNOTES"
	  echo "for information about new features in LabVIEW $VERS"
      fi
   else
      echo "Please see the LabVIEW Upgrade Notes (in the labview61-ref package) for"
      echo "information about new features in LabVIEW $VERS"
   fi
fi

echo ""
echo "Installation complete."
exit 0

