#!/bin/sh
#
# mpirun -version
# mpirun: HP MPI 01.04.00.00 B5880 - SPP-UX 5.x
# 
#******************************************************************************
GUNZIP=gunzip
UNTAR="tar xf "
RM="rm -f "

COURSE=course
COURSED=course-d
EXAMPLE=Example
SOLUTION=Solution
TEX=Tex
DOC=Doc

echo
echo "     Installation of MPI Course"
echo

if [ $# -gt 0 ]
  then
      ARG1=$1
  else
      ARG1="none"
  fi

#########################		check archi variable

#if [ -z $archi ]
if [ "x $archi " = "x  " ]
  then
      echo
      echo "Environment variable  archi  unset."
      echo "The following platforms are supported:"
      echo "archi = { AIX | IRIX | SOLARIS | LINUX }"
      echo
      BS=`basename $SHELL`
      if [ $BS = "bash" -o $BS = "sh" -o $BS = "ksh" -o $BS = "ash" ]
        then
	   echo "Type (for LINUX) :   export archi=LINUX"
	else
	    if [ $BS = "tcsh" -o $BS = "csh" ]
	    then
	        echo "Type (for LINUX) :   setenv  archi LINUX"
	    else
	        echo "Unknown shell type : " $BS
	    fi
      fi
      echo
#      exit 11	
  fi

if [ $archi =  AIX -o $archi = IRIX -o $archi = SOLARIS -o $archi = LINUX ]
  then
      echo "If there are problems in the compilation you may have to edit files"
      echo "Example/makefile."$archi " and/or  Example/default."$archi".mk"
      echo
  else
      echo "You have to derive the files"
      echo "Example/makefile."$archi " and/or  Example/default."$archi".mk"
      echo "by your own."
      echo
  fi

if [ -e $COURSED.tar  -a  ! -e $COURSE.tar  ]
  then
      echo "Renaming $COURSE into $COURSED"
      COURSE=$COURSED
  fi

#########################		unzip global file

if [ -e $COURSE.tar.gz ]
  then 
      $GUNZIP $COURSE.tar.gz
  fi

#########################		untar global file

if [ -e $COURSE.tar ]
  then 
      $UNTAR $COURSE.tar
  else 
      echo
      echo "$COURSE.tar  or  $COURSE.tar.gz  is not available."
      echo
      exit 123
  fi

#########################		remove global file

if [ -e $EXAMPLE.tar  -a -e $SOLUTION.tar  -a -e $TEX.tar ]
  then
      echo 
#      $RM $COURSE.tar
  else
      echo
      echo "At least one of the following files is missing:"
      echo
      echo $EXAMPLE.tar; echo $SOLUTION.tar; echo $TEX.tar; echo
      exit 123
  fi

#########################		untar Example
echo
echo "  --  Installing examples in " $EXAMPLE " --"
echo

if [ -d $EXAMPLE ]
  then
      echo "Directory " $EXAMPLE " does already exist."
      b="z"
      until case $b in
        [yYnN]*) true;;
        *)       false;;
      esac
      do  echo -n "Overwrite it [y/n] : "
	  read b
      done
      echo 
  else
      b="y"
  fi

if [ $b = "y" -o $b = "Y" ]
  then
      $UNTAR $EXAMPLE.tar
#      (cd $EXAMPLE; make all)
  fi

#########################		untar solution	[install sol|all]

if [ $ARG1 = "sol" -o $ARG1 = "all" ]
  then  
      echo
      echo "  --  Installing solution in " $SOLUTION "  --"
      echo
      if [ -d $SOLUTION ]
	then
	    echo "Directory " $SOLUTION " does already exist."
            b="z"
	    until case $b in
              [yYnN]*) true;;
              *)       false;;
	    esac
	    do  echo -n "Overwrite it [y/n] : "
		read b
	    done
	    echo 
	else
	    b="y"
	fi

      if [ $b = "y" -o $b = "Y" ]
	then
	    $UNTAR $SOLUTION.tar
#	    (cd $SOLUTION; make all)
	fi
fi

#########################			untar course material
echo
echo "  --  Installing course material --"
echo

if [ -d $DOC ]
  then
      echo "Directory " $DOC " does already exist."
      b="z"
      until case $b in
        [yYnN]*) true;;
        *)       false;;
      esac
      do  echo -n "Overwrite it [y/n] : "
	  read b
      done
      echo 
  else
      b="y"
  fi

if [ $b = "y" -o $b = "Y" ]
  then
      $UNTAR $TEX.tar
#      (cd $TEX; make all)
  fi

echo
echo "Course material can be accessed via"
echo
echo "           netscape Doc/index.html"
echo "       or  ghostview Doc/course.ps"
echo "       or  ghostview Doc/course.pdf"
echo





exit 0
