#!/bin/bash

#Instala paquetes fuente en forma de tar



if test x"$1" = x--help ; then
    echo AYUDA
    exit 0
    fi

if test x"$1" = x--version ; then
    echo instalartar.sh version 3
    exit 0
    fi


BASETMP=`mktemp -d |sed -e's/\n//'|sed -e's/^\/tmp\///'`
mkdir -p /tmp/${BASETMP}
rm -rf /tmp/${BASETMP}/*

if test -d /tmp/$BASETMP ; then 
	   echo No hay BASETMP, no puedo descomprimir ;
           exit;
fi

	 NOMBREAF=$1 ; # Nombre completo del archivo con las fuentes
         DIRTAR=$2      # como se llama el directorio en el que se desempaqueta el archivo (debe ser uno)  
         NOMBREAINSTALAR=$3
         ARCHIVOCARACTERISTICO=$4
	 INSTRUCCIONESINSTALL=$5


	 
   if  test x"$NOMBREAF" = x  ; then 
      echo ERROR Debe suministrar argumento 1
     fi
  if  test -e $NOMBREAF ; then 
     echo ERROR Debe estar el archivo indicado en el primer argumento NOMBRE_DEL_ARCHIVO : $NOMBREAF
     fi

  if test x"$DIRTAR" = x; then
       echo ERROR Debe estar indicado el segundo argumento directorio interno del tar
      fi
  
	 if test x"$INSTRUCCIONESINSTALL" = x ; then
	      INSTRUCCIONESINSTALL="sudo make prepare; make; sudo make install;"
	 fi

    	 if test x"$NOMBREAINSTALAR" = x ; then
	      NOMBREAINSTALAR=$1
	    fi
	 
	 echo "Caracteristico de $NOMBREAINSTALAR es: >$ARCHIVOCARACTERISTICO<"


### necesita usar sudo si instala en /usr
#insceptre:  
#	$(call download,$(SCEPTRESIMUSOL),$(DOWNLOAD),${sceptresimusolNAME},${sceptresimusolDIR},sceptre-simusol,Sceptre,$(call compilation))

### reinstala
#updsceptre: 
#	$(call download,,$(DOWNLOAD),${sceptresimusolNAME},${sceptresimusolDIR},sceptre-simusol,Sceptre,$(call compilation))


# ojo si baja mal no dejka el archivo como antes!!!!!!!!!!!!!!!!!!!!!!!!
#	$(call download,$(SCEPTREBIN),$(DOWNLOAD),${sceptreNAME},${sceptreDIR},sceptre-inenco,Sceptre,$(call sceptrecomp))
#                        1            2            3              4             5              6       7   
	 
#	$(call download,$($ARCHIVOORIGEN},${sceptreDIR},sceptre-inenco,Sceptre,$(call sceptrecomp))
#                         1                      2              3             4              6       7

if test x"ARCHIVOCARACTERISTICO" != x ; then 
    echo No se instala: $NOMBREAINSTALAR ya esta instalado en $ARCHIVOCARACTERISTICO,  BORRAR  $ARCHIVOCARACTERISTICO y reintentar.
    # si $ARCHIVOCARACTERISTICO, esta vacio instala de prepo, update
    exit;
    fi


	     echo Instalando $NOMBREAINSTALAR;

	      cd /tmp/$BASETMP ;
	      tar -zxvf $NOMBREAF >/dev/null ; 
	      DIRBU=`echo $DIRTAR*| sed -e 's/ //g'` ;
	      if test -d $DIRBU  ; then   
		    echo Encontro directorio  $DIRBU con raiz $DIRTAR; 
	            cd $DIRTAR* ; 
	 	    `$INSTRUCCIONESINSTALL` ;
	            echo $NOMBREAINSTALAR fue instalado en $PREFIX;
	         #else no entiendo 
	         #   echo "No encontro directorio con raiz $DIRTAR, encontro $DIRBU" ;
	      fi; 
	      
