Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

#!/bin/bash

function colorecho
{
  echo -e$2 "\033[33m$1\033[39m"
}

function errorecho
{
  echo -e "\031[33m$1\033[39m"
}

function helptext()
{
  colorecho "#------------------------------"
  colorecho "# Configure instructions: -----"
  colorecho "#------------------------------"
  colorecho ""
  colorecho "./configure [option] [type] [install directories] [ostype]"
  colorecho ""
  colorecho "[option] = Option for configure:"
  colorecho "  help             Display configure instructions."
  colorecho "  nomake   Only prepare system dependent files (base directory and online/offline mode)."
  colorecho "  all              Prepare system dependent files and make used libraries."
  colorecho "  clean            Clean the installation directory. Does not clean the results directory."
  colorecho "  compress Compress the source code in a tar-ball."
  colorecho ""
  colorecho "[type] = Configure for use in online or offline mode (only needed in nomake and all):"
  colorecho "  I        Online mode."
  colorecho "  O        Offline mode (no connection to CAMAC, motor, voltage supply and scope)."
  colorecho "  S        Offline mode with scope connection (no connection to CAMAC, motor and voltage supply)."
  colorecho ""
  colorecho "[install directories] = Directories where ROOT and NET-SNMP are installed (when running with superuser, this is important, otherwise optional):"
  colorecho "  --root-install=/root/install/directory"
  colorecho "  --snmp-install=/snmp/install/directory"
  colorecho ""
  colorecho "[ostype] = Specific setting for 64bit or 32bit version of OS (optional):"
  colorecho "  --ostype=i686            32bit OS type"
  colorecho "  --ostype=x86_64  64bit OS type"
  colorecho ""
  colorecho "Example:"
  colorecho "  ./configure all I --root-install=/home/user/root --snmp-install=/home/user/snmp"
  colorecho ""
  colorecho "#------------------------------"
}

# Check for arguments
if [ "$1" == "" ]; then
  errorecho "Error! No arguments supplied."
  echo ""
  helptext
  exit 1
else
  # When using help, only display help and then exit
  if  [ "$1" == "help" ]; then
    helptext
    exit 0
  fi

  # Print help and exit if we give a wrong first argument
  if  [ "$1" != "nomake" ] && [ "$1" != "all" ] && [ "$1" != "clean" ] && [ "$1" != "compress" ]; then
    errorecho "Error! Wrong configuration option selected (first argument)."
    echo ""
    helptext
    exit 1
  fi

  startdir=$PWD

  # Check for ROOT and NET-SNMP install directories and for OS type
  snmpsearch="--snmp-install="
  rootsearch="--root-install="
  ossearch="--ostype="
  snmpdirectory=-1
  rootdirectory=-1
  osmanual=-1
  for var in $@
  do
    case $var in
      "$snmpsearch"*)
        snmpdirectory=${var#$snmpsearch}
        echo "NET-SNMP directory: $snmpdirectory";;
      "$rootsearch"*)
        rootdirectory=${var#$rootsearch}
        echo "ROOT directory: $rootdirectory";;
      "$ossearch"*)
        osmanual=${var#$ossearch};;
      *) ;;
    esac 
  done   
         
  # If not supplied, check automatically for OS type
  if [ $osmanual == -1 ]; then
    ostype=`uname -p`
    if [  "$ostype" != "x86_64" ] && [ "$ostype" != "i686" ]; then
      ostype=`uname -i`
      if  [ "$ostype" != "x86_64" ] && [ "$ostype" != "i686" ]; then
         ostype=`uname -m`
      fi 
    fi
  else
    ostype=$osmanual
  fi
 
  # Check for installation directory of ROOT - if variables not currently set, remind user to set them before running make
  if [ "$1" != "clean" ] && [ "$1" != "compress" ]; then
    if [ $rootdirectory != -1 ]; then
      printenv ROOTSYS > /dev/null
      if [ $? != 0 ]; then
        colorecho "ROOT environment variables not set. Please run \"source $rootdirectory/bin/thisroot.sh\", before using make."
      fi
    else
      colorecho "Before running make, please make sure ROOT environment variables are set."
    fi
  fi

  # Compiles the table microcontroller program
  if [ "$1" == "all" ]; then
    if [ -d $startdir/src/MIKRO ]; then
      cd $startdir/src/MIKRO
      rm -f $startdir/src/MIKRO/mikro_ctrl
      make
      cd $startdir
    fi
  fi

  # When using compress, only create a tar-ball and then exit
  if [ "$1" == "compress" ]; then
    cd $startdir
    if [ ! -d $startdir/sipmscan_trunk ]; then
      mkdir $startdir/sipmscan_trunk
      mkdir $startdir/sipmscan_trunk/results
      mkdir $startdir/sipmscan_trunk/layout
      mkdir $startdir/sipmscan_trunk/settings
      echo "Copying files to temporary directory $startdir/sipmscan_trunk..."
      cp -r configure ./dict ./doc ./include ./input ./src ./sipmscan_trunk/ 
      cp -r ./layout/default.layout ./sipmscan_trunk/layout/
      echo "default.layout" > ./sipmscan_trunk/layout/selected_layout.txt
# TODO - Settings default files
      cd $startdir/sipmscan_trunk
      echo "Cleaning the base directory in $startdir/sipmscan_trunk..."
      rm -f ./*.bak
      echo "Cleaning the subdirectories of $startdir/sipmscan_trunk..."
      rm -f ./*/*.bak
      cd $startdir/sipmscan_trunk/src
      rm -f ./*/*.bak
      cd $startdir/sipmscan_trunk/include/vxi11_x86_64
      echo "Cleaning the 64 bit VXI11 directory in $startdir/sipmscan_trunk/include/vxi11_x86_64..."
      rm -f ./*.bak
      make clean
      cd $startdir/sipmscan_trunk/include/vxi11_i686
      echo "Cleaning the 32 bit VXI11 directory in $startdir/sipmscan_trunk/include/vxi11_i686..."
      rm -f ./*.bak
      make clean
      cd $startdir
      echo "Creating a tar-ball sipmscan_trunk.tar.gz..."
      tar czf $startdir/sipmscan_trunk.tar.gz ./sipmscan_trunk
      echo "Removing the temporary directory $startdir/sipmscan_trunk..."
      rm -fr $startdir/sipmscan_trunk
      exit 0
    else
      errorecho "Error! Directory ./sipmscan_trunk already exists."
      exit 1
    fi
  fi

  # Configure the workstation information and directory of program (0 if we find something and 1 otherwise)
  basedir=$(echo $startdir | sed 's/\//\\\//g')

  if [ "$1" == "nomake" ] || [ "$1" == "all" ]; then
    if [ "$2" == "O" ] || [ "$2" == "I" ] || [ "$2" == "S" ]; then
      # Setting up the current working computer
      grep -q "#define WORKSTAT 'N'" $startdir/input/workstation.h.in
      if [ $? == 0 ]; then
        sed "s/define WORKSTAT 'N'/define WORKSTAT '$2'/" $startdir/input/workstation.h.in > $startdir/input/workstation.h.mid
      fi
      grep -q "#define rootdir \"path-to-installation\"" $startdir/input/workstation.h.in
      if [ $? == 0 ]; then
        sed "s/path-to-installation/$basedir/g" $startdir/input/workstation.h.mid > $startdir/input/workstation.h.mid2
        rm $startdir/input/workstation.h.mid
      fi
      # Check if we are connected to IJS network
      etnet=$(ifconfig | grep "178.172.43.")
      if [ "$etnet" == "" ]; then
        sed "s/define IJSNET 1/define IJSNET 0/" $startdir/input/workstation.h.mid2 > $startdir/include/workstation.h
        rm $startdir/input/workstation.h.mid2
      else
        cp $startdir/input/workstation.h.mid2 $startdir/include/workstation.h
        rm $startdir/input/workstation.h.mid2
      fi
      # Setting up the OS type specific files
      grep -q "#include \"vxi11_user.h\"" $startdir/input/daqscope.C.in
      if [ $? == 0 ]; then
        sed "s/vxi11_user.h/..\/include\/vxi11_$ostype\/vxi11_user.h/g" $startdir/input/daqscope.C.in > $startdir/src/daqscope.C
      fi
      grep -q "SHLIB = \$(LIBFILE) \$(LDIR)\/libvxi11.a" $startdir/input/Makefile.in
      if [ $? == 0 ]; then
        if [ "$2" == "I" ]; then
          sed "s/SHLIB = \$(LIBFILE) \$(LDIR)\/libvxi11.a/SHLIB = \$(LIBFILE) \$(LDIR)\/libvxi11.a -lusb/g" $startdir/input/Makefile.in > $startdir/Makefile.mid
        fi
      fi
      grep -q "CAMLIB = \$(LIBFILE)" $startdir/input/Makefile.in
      if [ $? == 0 ]; then
        if [ "$2" == "I" ]; then
          sed "s/CAMLIB = \$(LIBFILE)/CAMLIB = \$(LIBFILE) -lusb/g" $startdir/Makefile.mid > $startdir/Makefile.mid2
          rm $startdir/Makefile.mid
        elif [ "$2" == "O" ] || [ "$2" == "S" ]; then
          cp $startdir/input/Makefile.in $startdir/Makefile.mid2
        fi
      fi
      sed "s/OSTYPE=none/OSTYPE=$ostype/g" $startdir/Makefile.mid2 > $startdir/Makefile
      rm $startdir/Makefile.mid2

      if [ "$2" == "O" ] || [ "$2" == "S" ]; then
        cp $startdir/input/daqusb.C.offline $startdir/src/daqusb.C
        cp $startdir/input/libxxusb.h.offline $startdir/include/libxxusb.h
        cp $startdir/input/usb.h.offline $startdir/include/usb.h
      elif [ "$2" == "I" ]; then
        cp $startdir/input/daqusb.C.online $startdir/src/daqusb.C
        cp $startdir/input/libxxusb.h.online $startdir/include/libxxusb.h
      fi

      echo "#!/bin/bash" > $startdir/start.sh
      echo "dir=\`dirname \$0\`" >> $startdir/start.sh
      echo "" >> $startdir/start.sh
      echo "snmpdirectory=$snmpdirectory" >> $startdir/start.sh
      echo "rootdirectory=$rootdirectory" >> $startdir/start.sh
      echo "" >> $startdir/start.sh
      cat $startdir/input/start.sh.in >> $startdir/start.sh
      chmod ug+x $startdir/start.sh
    fi
  fi

  # In case we just want to set the workstation information, exit here
  if [ "$1" == "nomake" ]; then
    exit 0
  fi

  # 64 bit configuration rules
  if [ $ostype == "x86_64" ]; then
    vxidir=$startdir/include/vxi11_$ostype
    if [ -d $vxidir ]; then
      cd $vxidir
      if [ "$1" == "clean" ]; then
        make clean
        cd $startdir
        make clean
        rm -f Makefile
      else
        if [ "$2" == "O" ] || [ "$2" == "I" ] || [ "$2" == "S" ]; then
          make
        else
          errorecho "Error! No configuration type selected (second argument)."
          echo ""
          helptext
          exit 1
        fi
      fi
      cd $startdir
    else
      errorecho "No 64 bit VXI11 source folder."
      exit 1
    fi
  # 32 bit configuration rules
  elif [ $ostype == "i686" ]; then
    vxidir=$startdir/include/vxi11_$ostype
    if [ -d $vxidir ]; then
      cd $vxidir
      if [ "$1" == "clean" ]; then
        make clean
        cd $startdir
        make clean
        rm -f Makefile
      else
        if [ "$2" == "O" ] || [ "$2" == "I" ] || [ "$2" == "S" ]; then
          make
        else
          errorecho "Error! No installation type selected (second argument)."
          echo ""
          helptext
          exit 1
        fi
      fi
      cd $startdir
    else
      errorecho "No 32 bit VXI11 source folder."
      exit 1
    fi
  else
    errorecho "No OS type information found."
    exit 1
  fi
fi

exit 0