#!/bin/sh

DISTRO=none

check_package () {
   case $DISTRO in
      debian)
         dpkg -s $1 2>&1|grep Status|grep installed >/dev/null 2>&1
      ;;

      mandriva)
         count=`rpm -qa $1 | wc -l`
         if [ $count -ge 1 ]; then echo return 0 ; else return 1 ; fi
   esac

   if [ $? -eq 1 ]; then
      echo "You need to install package $1"
      exit 2
   fi
}

check_program () {
   which $1 >/dev/null 2>&1

   if [ $? -eq 1 ]; then
      echo "You need to install program $1"
      exit 2
   fi
}

check_sysctl () {
   nbmsg=`/sbin/sysctl -n fs.mqueue.msg_max`
   if [ $nbmsg -lt 100 ]; then
      echo "Your system cannot store enough messages."
      echo "Please run the following command as root:"
      echo "         sysctl fs.mqueue.msg_max=100\n\n"
      echo "This will change the amount of message your"
      echo "kernel can store. To make this change permanent,"
      echo "change your sysctl configuration.\n"
      echo "On most linux distribution, you can do that"
      echo "by editing the /etc/sysctl.conf file and add"
      echo "the following line:"
      echo "        fs.mqueue.msg_max=100"
      exit 4
   fi
}

if [ -f /etc/debian_version ]; then
   DISTRO=debian
fi

if [ -f /etc/redhat-release ]; then
   grep Mandriva /etc/redhat-release >/dev/null 2>&1
   if [ $? -eq 0 ]; then
      DISTRO=mandriva
   fi
fi

#Software to verify
#  - Perl
#  - GCC
#  - GNAT

case $DISTRO in 
   "debian")
      check_package perl
      check_package xpdf
      check_package libgnome2-perl
      check_package libxml-parser-perl
      check_package libgtk2-gladexml-perl
      check_package libwxgtk2.8-dev
      check_package gnat
      check_package gcc
      check_package zip
      check_package libfile-copy-recursive-perl
      check_package perl-modules
      check_package tcllib
      check_package tcl8.5
      check_package tk8.5
      check_package tklib
      check_package swig
      check_package gnuplot
      check_package mono-runtime
      check_package openjdk-6-jre
   ;;

   "mandriva")
      check_package perl
      check_package gcc
      check_package gcc-gnat
      check_package perl-Gtk2-GladeXML
      check_package perl-File-BaseDir
      check_package perl-GTK
      check_package swig
      check_package perl-File-Remove
      check_package perl-Gtk2-SourceView
      check_package unzip
      check_package tk
      check_package mono
      check_package tcllib
      check_package tcl
      check_package xpdf
      check_package wxGTK
      check_package wxPythonGTK
      check_package gnuplot
   ;;
esac

check_program gmast
check_program gnatmake
check_program asn1.exe
check_program asn2aadlPlus.py
check_program asn2dataModel.py
check_program assert-builder-ocarina.py
check_program gmastresults
check_program zip
check_program gnuplot
check_program java
check_program gcov
check_program mono
check_program ocarina
check_program TASTE-IV
check_program TASTE-DV
check_program TASTE-CV

check_sysctl

echo "Everything seems to be ok"


