#!/bin/sh

# Changes: 07-MAR-2003 added /usr/local/... path for openMotif
#                      on Mac OS X
#

prefix=/usr/local
setUid=1

pSyntax()
{
   echo Syntax `basename $0` '[-h|--help]'
   echo "      " `basename $0` '[-p|--prefix installPath] [-d devpkgprefix] [-n|--no-suid] [-x|--no-x] [-g]] --no-gimp]'
   echo
   echo "Default install path: $prefix"
   echo "Chemin d'installation par default: $prefix"
   echo "Default Installationspfad: $prefix"
   echo "Percorso d'installazione di default: $prefix"
   echo "Alaprtelmezett teleptsi tvonal: $prefix"
   echo
   echo "The documentation files will be installed under:"
   echo "Les fichiers de documentation seront installes sous:"
   echo "Die Dokumentaionsdateien werden installiert unter:"
   echo "La Documentazione verr installata in:"
   echo "A dokumentcis fjlok ide teleplnek:"
   echo $prefix/mtink
   echo
   echo "If you want only the tty based version use the -x option."
   echo "Ha csak a tty bzis verzit kvnja hasznlni, alkalmazza a -x opcit."
   echo
   echo "This program require read write access to the device"
   echo "files. You may install mtink with the set uid bit set"
   echo "or change yourself the permissions to the device files."
   echo "In this case you may enter the -n option."
   echo
   echo "Ez a program olvassi s rsi jogot kvetel az eszkz-"
   echo "fjlokhoz. Teleptheti az mtink-et az uid bit kszlet belltsval"
   echo "vagy megvltoztathatja az engedlyeket az eszkzfjlokhoz."
   echo "Ebben az esetben a -n opcival lphet be."
   echo
   echo "Pour ne creer que la version de commande en ligne"
   echo "utilise l'option -x."
   echo
   echo "Le programme necessite les droit d'ecriture et de lecture"
   echo "pour les fichiers /dev/... . Vous pouvez installer mtink"
   echo "avec le bit set-uid ou modifier vous meme les droits"
   echo "d'acces aux fichiers concernes. Dans ce cas vous pouvez"
   echo "lancer `basename $0` avec l'option -n"
   echo
   echo "Nur Konsole basierte Version erstellen: Option -x angeben."
   echo
   echo "Das Program bentigt uneingeschraenkte Rechte auf die"
   echo "Geraetedateien. Sie koennen mtink mit das 'set-uid' bit"
   echo "installieren. Falls Sie es nicht wuenschen muessen Sie"
   echo "die Rechte auf der Geraetedateien anpassen und `basename $0`"
   echo "mit der Option -n aufrufen."
   echo
   echo "Se si vuole solo la versione a linea di comando usare -x."
   echo
   echo "Questo programma richiede l'accesso in lettura e scrittura al"
   echo "file dispositivo. Si pu installare mtink con il bit sed uid"
   echo "attivato o cambiare i permessi al file dispositivo."
   echo "In questo caso si pu usare l'opzione -n."
   echo
}

noX=0
noGimp=0
# Check for args
while [ $# -ne 0 ]
do
   case $1 in
   -h|--h*)
      pSyntax
      exit 0;;
   -n|--no-s*)
      setUid=0
      ;;
   -x|--no-x)
      noX=1
      ;;
   -g|--no-g*)
      noGimp=1
      ;;
   -p|--p*)
      shift
      if [ $# -ne 1 ]
      then
        pSyntax
        exit 1
      else
         prefix=$1
      fi;;
   *)
      pSyntax
      exit 1;;
   esac
   shift
done

if [ `uname` = "Linux" ]
then
  detectPrg='$(DETDIR)/askPrinter'
fi

# checks for Motif library and include files.
# checjk first for 64 bit library (Nicola Fabiano on Fedora Core 2)
LIBDIRS='/usr/X11R6/lib64
/usr/X11R6/lib
/usr/lib
/usr/X11/lib
/usr/openwin/lib
/usr/local/lib
/usr/dt/lib'

# Add gimp-2.0 for fedora core 2
INCLDIRS='/usr/include/gimp-2.0/libgimp
/usr/X11R6/include
/usr/X11/include
/usr/include
/usr/openwin/include
/usr/local/include
/usr/dt/include'

mlibFound=0
mxpmheader=
mxpmlib=
for dir in $LIBDIRS
do
   if [ "`ls -c $dir/libXm.* 2>/dev/null`" != "" ]
   then
      mlibFound=1
      break
   fi
done

minclFound=0
for dir in $INCLDIRS
do
   if [ -f $dir/Xm/Xm.h ]
   then
      minclFound=1
      if [ -f $dir/Xm/XpmP.h ]
      then
         mxpmheader="-DMOTIF_XPM"
      fi
      break
   fi
done

xinclFound=0
for dir in $INCLDIRS
do
   if [ -f $dir/X11/X.h ]
   then
      xinclFound=1
      if [ "$mxpmheader" = "" ]
      then
         if [ -f $dir/X11/xpm.h ]
         then
            mxpmheader="-DXPM_XPM"
            mxpmlib="-lXpm"
         fi
      fi
      break
   fi
done

if [ $noX -eq 0 ]
then
   if [ $xinclFound -eq 0 -o $minclFound -eq 0 -o $mlibFound -eq 0 ]
   then
      if [ $mlibFound -eq 0 ]
      then
         echo "The Motif libraries are not installed."
         echo "Install Motif or Lesstif if your system"
         echo "don't provide Motif."
         echo "Please note that the motif package may be"
         echo "called openmotif, motif or lesstif (Linux)."
         echo
         echo "A Motif knyvtrak nincsenek teleptve."
         echo "Teleptse a Motif vagy Lesstif csomagot, ha a rendszern"
         echo "nincs Motif."
         echo "Krem figyeljen, hogy a motif csomagot taln"
         echo "openmotif-nak, motif-nak vagy lesstif-nek nevezik (Linux)."
         echo
         echo "La bibliotheque Motif n'est pas installee."
         echo "Installez Motif ou, si votre Systeme ne contient"
         echo "pas Motif / Openmotif installez Lesstif (Linux)."
         echo
         echo "Die Motif Umgebung wurde nicht gefunden."
         echo "Installieren Sie bitte Motif / Openmotif oder,"
         echo "falls nicht vorhanden Lesstif (Linux)."
         echo
      fi

      if [ $minclFound -eq 0 ]
      then
         echo "Motif/Lesstif header file not found !"
         echo "mtink require this, please install them."
         echo
         echo "Nem tallom a Motif/Lesstif fejlc fjlt !"
         echo
         echo "Les fichiers includes Motif/Lesstif ne sont pas"
         echo "present! Installe Motif/Lesstif correctement."
         echo
         echo "Die Motif/Lesstif Include-Dateien wurden nicht"
         echo "gefunden! Installieren Sie bitte Motif/Lesstif"
         echo "vollstndig."
         echo
         if [ "`uname`" = Linux ]
         then
            echo "The Motif package is mostly called motif-devel"
            echo "If you have installed Lesstif the package name"
            echo "will probably be lesstif-devel"
            echo
            echo "A Motif csomagot leginkbb motif-devel-nek nevezik."
            echo "Ha a Lesstif van teleptve, a csomag nevt"
            echo "lesstif-devel-knt prblja."
            echo
            echo "Le paquetage est souvent nome motif-devel"
            echo "Si vous avez installe Lesstif le nom du paquet"
            echo "manquant est certainement lesstif-devel"
            echo
            echo "Sie muessen das'Entwicklungspacket motif-devel Bzw."
            echo "openmotif-devel installieren. Falls Lesstif zum"
            echo "Einsatzkommen soll is das Packetname meistens"
            echo "lesstif-devel"
         fi
         echo
      fi

      if [ $xinclFound -eq 0 ]
      then
         echo "X is not installed or the include pathes are stranges"
         echo
         echo "Az X nincs teleptve, vagy az elrsi tvonal ismeretlen."
         echo
         echo "X n'est pas apparement pas installe ou les fichier 'include'"
         echo "se trouvent sous un repertoire non standard."
         echo
         echo "X scheint nicth installiert zu sein. Mglicherweise"
         echo "befinden sich die Dateien unter einen nicht Standardpfad"
         if [ "`uname`" = Linux ]
         then
            echo "The X development package is mostly called X-devel"
            echo
            echo "Az X fejleszti csomagot leginkbb X-devel-nek nevezik."
            echo
            echo "Les paquets de developement X ne sont pas installes."
            echo "Installez les, le non est generalement X-devel"
            echo
            echo "Die X.Enticklungsumgebung muss installiert werden."
            echo "Das Packetname ist meistens X-devel"
         fi
         echo
      fi
      exit 1
   fi

   if [ `uname` = Linux ]
   then
      . ./checkMotifVersion.sh
   fi

   if [ "$MotifLib" != "" ]
   then
      mlCmd="s@^MOLIB.*@MOLIB = $MotifLib@"
   else
     mlCmd=
   fi
   if [ "$XmHPath" != "" ]
   then
      miCmd="s@^MOINC.*@MOINC = -I$XmHPath@"
   else
      miCmd=
   fi

   # Check for the Xp libary, some Motif libraries need this

   xplibFound=0
   for dir in $LIBDIRS
   do
      if [ "`ls -c $dir/libXp.* 2>/dev/null`" != "" ]
      then
         xplibFound=1
         break
      fi
   done

   # but not for Solaris
   if [ `uname` = SunOS ]
   then
      xplibFound=0
   fi

   # Gimp part ################################################
   # check for gimp
   includeFileFound=0
   gimptoolFound=0
   gimpVersion=0

   if [ $noGimp -eq 0 ]
   then
     for dir in `echo $PATH | tr ':' '\012'`
     do
        if [ -x $dir/gimptool-2.0 ]
        then
           # and ask for the version
           gimpVersion=`$dir/gimptool-2.0 --version | sed 's/\(1\..\).*/\1/'`
           gimptoolFound=2
           break
        fi
        if [ -x $dir/gimptool ]
        then
           # and ask for the version
           gimpVersion=`$dir/gimptool --version | sed 's/\(1\..\).*/\1/'`
           gimptoolFound=1
           break
        fi
     done
   fi

   if [ $gimptoolFound -eq 0 ]
   then
      if [ $noGimp -eq 0 ]
      then
         echo "Gimp development package not installed."
         echo "Plugins will not be compiled"
         echo "Please install the gimp package"
         echo "if you want the gimp plugins."
         echo
         echo "A Gimp feljeszti csomag nincs teleptve."
         echo "A pluginok nem szerkesztdnek be."
         echo "Krem teleptse a gimp csomagot"
         echo "ha gimp plugint kvn hasznlni."
         echo
         echo "Le paquet de developement gimp n'est pas installe,"
         echo "Le plugins ne sera pas genere."
         echo "Installez gimp et gimp-devel si vous souhaitez"
         echo "le plugins gimp."
         echo
         echo "Das Gimp-Entwiclungs packet ist nicht installieert,"
         echo "Das plugins wird nicht erzeugt."
         echo "Installieren Sie, bitte, gimp-devel (Linux) Wenn Sie"
         echo "das Gimp-Plugins erzeugen mchten."
         echo
      fi
   else
      # Build CFLAGS and LFLAGS
		if [ $gimptoolFound -eq 2 ]
		then
         CFLAGS=`gimptool-2.0 --cflags`
         LFLAGS=`gimptool-2.0 --libs`
		else
         CFLAGS=`gimptool --cflags`
         LFLAGS=`gimptool --libs`
      fi

      # Check for Header files libgimp/gimp.h
      includeFileFound=0
      for dir in `echo $CFLAGS | tr ' ' '\012' | grep -- -I | sed 's/-I//'`\
                 /usr/include /usr/local/include
      do
         if [ -f $dir/libgimp/gimp.h ]
         then
            includeFileFound=1
            break
         fi
      done

      if [ $includeFileFound -eq 0 ]
      then
         echo "Header file for gimp not found."
         echo "Please install the gimp development package."
         echo
         echo "Nem tallok fejlc fjlt a gimp-hez."
         echo "Krem teleptse a gimp fejleszti csomagot."
         echo
         echo "Les fichiers necessaire pour gimp ne sont pas installes."
         echo "Installez le paquet gimp-devel."
         echo
         echo "Die notwendige Entwicklungsumgebung fuer gimp ist."
         echo "nicht vorhanden. Packet gimp-devel bitte installieren (Linux)."
         echo
      fi
   fi

   # build Makefile.

   cat Makefile.ORG |\
   if [ "$xplibFound" = 1 ]
   then
      sed  -e 's/LIBXP .*/LIBXP = -lXp/'
   else
      sed  -e 's/LIBXP .*/LIBXP =/'
   fi |\
   if [ "$mxpmheader" != "" ]
   then
      sed -e 's@^XPM_INC.*@XPM_INC = '$mxpmheader'@' \
          -e 's@^XPM_LIB.*@XPM_LIB = '$mxpmlib'@'
   else
      sed -e 's@^XPM_INC.*@XPM_INC =@' \
          -e 's@^XPM_INC.*@XPM_LIB =@'
   fi |\
   if [ "$gimptoolFound" -ge 1 ]
   then
      sed  -e "s@GIMP_INC .*@GIMP_INC = $CFLAGS@" \
           -e "s@GIMP_LIB .*@GIMP_LIB = $LFLAGS@" \
           -e "s@^gimpVersion=.*@gimpVersion=$gimpVersion@"
   else
      cat -
   fi |\
   if [ "$includeFileFound" = 0 ]
   then
      sed  -e 's@PRG .*@PRG = mtink mtinkc ttink '$detectPrg'@'
   else
      sed  -e 's@PRG .*@PRG = mtink mtinkc ttink gimp-mtink '$detectPrg'@'
   fi  |\
   if [ "$gimpVersion" = "1.0" ]
   then
      sed  -e 's@\(GIMP_INC .*\)@\1 -DGIMP_VERSION_1@'
   else
	   if [ "$gimptoolFound" -gt 1 ]
		then
		   sed  -e 's@\(GIMP_INC .*\)@\1 -DGIMP_VERSION_2@'
		else
         cat -
		fi
   fi | sed -e "s@^prefix.*@prefix=$prefix@" \
            -e "s@^setUid.*@setUid=$setUid@" \
            -e "$mlCmd" -e "$miCmd"   > Makefile
   # and server install script
   sed 's@^PROG.*@PROG='$prefix'/sbin/mtinkd@' etc/mtink.ORG > etc/mtink.tmp
   mv etc/mtink.tmp etc/mtink.ORG
   sed 's@^PROG.*@PROG='$prefix'/sbin/mtinkd@' etc/mtink > etc/mtink.tmp
   mv etc/mtink.tmp etc/mtink

   if [ -d $prefix/doc ]
   then
      docDir=$prefix/doc
   else
      docDir=$prefix
   fi
   echo "Binary path:"
   echo "Chemin d'installation de l'executable:"
   echo "Installationspfad fuer das ausfuehrbares Programm:"
   echo "Percorso di installazione dell'eseguibile:"
   echo "Binris elrsi tvonal:"
   echo $prefix/bin
   echo
   echo "The documentation files will be installed under:"
   echo "Les fichiers de documentation seront installes sous:"
   echo "Die Dokumentationsdateien werden installiert unter:"
   echo "La Documentazione verr installata in:"
   echo "A dokumentcis fjlok ide teleplnek:"
   echo $docDir/mtink
   echo

   if [ "$gimpVersion" != "" ]
   then
      echo Gimp version  $gimpVersion found / trouve / gefunden / trovato / tallva.
      echo
   fi
else # no x file to build
   # build Makefile.

   cat Makefile.ORG | sed  -e 's/PRG .*/PRG = ttink '$detectPrg'/' |\
   sed -e "s@^prefix.*@prefix=$prefix@" \
       -e "s@^setUid.*@setUid=$setUid@" \
       -e "s@^install:.*@install: install-ttink install-doc@" > Makefile
   # and server install script
   sed '@^PROG.*@PROG='$prefix'/sbin@' etc/mtink.ORG > etc/mtink

   if [ -d $prefix/doc ]
   then
      docDir=$prefix/doc
   else
      docDir=$prefix
   fi
   echo "Binary path:"
   echo "Chemin d'installation de l'executable:"
   echo "Installationspfad fuer das ausfuehrbares Programm:"
   echo "Percorso di installazione dell'eseguibile:"
   echo "Binris elrsi tvonal:"
   echo $prefix/bin
   echo
   echo "The documentation files will be installed under:"
   echo "Les fichiers de documentation seront installes sous:"
   echo "Die Dokumentationsdateien werden installiert unter:"
   echo "La Documentazione verr installata in:"
   echo "A dokumentcis fjlok ide teleplnek:"
   echo $docDir/mtink
   echo
fi   
