#!/bin/sh
#ututo uses emerge

#   update
#                Update the packages list and some system scripts
#        xs-update
#                Update all packages to new version availables (This process may take much time)
#        install
#                Download and install <package>
#        reinstall
#                Download and reinstall <package> and all its dependencies (though they were already installed)
#        fixdepend
#                Download and install any dependencies of <package> that was not installed or a new version of them
#        remove
#                Uninstall <package>
#        info
#                Show information about <package>
#        download
#                Download (but do not install) <package>  and all dependencies not installed or outdated
#        fastinstall
#                Download and install <package> but without verifying nor installing dependencies (Be careful, this can damage your system)
#        fastreinstall
#                Download and reinstall <package> but without verifying nor installing dependencies (Be careful, this can damage your system)
#        verify
#                Verify and install missing libraries of the system
#        autoupgradeuget
#                If there is a new version of ututo-get, download and install it
#        dist-upgrade
#                Upgrade UTUTO XS to new version available (This process may take much time)#

# SOLO UN PAQUETE POR VEZ



for arg in "$@" 
do
      index=`expr $index + 1` 
      #echo RRR $index $arg - $PAQS
      if test $index -gt 1 ; then
          narg=`echo $arg| sed 's/^\"//'| sed 's/\"$//'`
          PAQS="$PAQS $narg"                 
      fi
done  





UGET="ututo-get "
EMERGE=emerge

TACCION=$1

ACCION=`echo $TACCION|awk -F'-' '{print $1}'`
ACCIONINFO=`echo $TACCION|awk -F'-' '{print $2}'`
EACCION=`echo $ACCIONINFO|awk -F'_' '{print $1}'`
E2ACCION=`echo $ACCIONINFO|awk -F'_' '{print $2}'`
echo Accion: $ACCION, Opciones: $ACCIONINFO Distro: SUSE
echo Paquetes: $PAQS


OS=`echo $EACTION |cut -b1`
OND=`echo $EACTION |cut -b2`
ONU=`echo $EACTION |cut -b3`
OF=`echo $EACTION |cut -b4`
OBD=`echo $EACTION |cut -b5`


if test "x$ACCION" = xinstall ; then
 if test "x$E2ACCION" = xdwld ; then
   for arg in $PAQS 
   do
     echo  $UGET download $arg
     echo Y |$UGET download $arg
   done
   exit
 fi

OPTS=


if test "x$OS" != x ; then
    echo $EMERGE --fetchonly  $PAQS
    $EMERGE --fetchonly  $PAQS
    # ver -d buildeps -D nonbuilddeps
fi

if test  "$OND" != x ; then
    #nodeps
for arg in $PAQS 
   do
    echo  $UGET fastinstall $arg
    echo Y |$UGET fastinstall $arg
   done
   exit
 fi

if test  "x$ONU" != x ; then
    #OPTS="$OPTS --no-upgrade"
    
    exit
fi
if test "x$OBD" != x ; then
    exit
fi
if test "x$OF" != x ; then
for arg in $PAQS 
   do
    echo $UGET reinstall  $arg
    echo Y |$UGET reinstall  $arg
   done
  exit
fi

   for arg in $PAQS 
   do
     echo  $UGET install  $arg
     echo Y | $UGET install  $arg
   done

elif test "x$ACCION" = xoriginalsourceinstall ; then
   echo esto lo maneja source.sh, mal llamado desde babel
elif test "x$ACCION" = xsearch ; then
    # como esta integrado al install no se muestra se podria hacer
    #echo N | $UGET $PAQ
    # por las dudas
    #$EMERGE --search $PAQ
 if test "x$EACCION" = xnames -o "x$EACCION" = x ; then
     echo $EMERGE -s  $PAQS
     $EMERGE -s  $PAQS
  elif test "x$EACCION" = xall ; then
     echo $EMERGE -S $PAQS
     $EMERGE -S $PAQS

  else
     echo error opcion $EACCION diferente a la habilitada con search
  fi
elif test "x$ACCION" = xinfo  ; then
       #la opcion aca EACCION no parece servir
  for arg in $PAQS 
   do
     echo $UGET info  $arg
     $UGET info  $arg
   done
elif test "x$ACCION" = xremove ; then
  for arg in $PAQS 
   do
     echo $UGET remove  $arg
     $UGET remove  $arg
   done
elif test "x$ACCION" = xspecial ; then
  for arg in $PAQS 
   do
    echo $UGET --$ACCIONINFO $arg
    $UGET --$ACCIONINFO $arg
   done
# fix clean specialnp delrepo refresh update distupdate
elif test "x$ACCION" = xfix ; then
  echo $UGET --fixdepend;   $UGET --verify
  $UGET --fixdepend
  $UGET --verify
elif test "x$ACCION" = xclean ; then
     echo NO se como
     #$UGET clean 
     #apt-cache autoclean only obsolete
elif test "x$ACCION" = xdelrepo ; then
    echo No tiene sentido
    #hecho todavia
elif test "x$ACCION" = xrefresh ; then
    echo $UGET --update
    $UGET --update
elif test "x$ACCION" = xupdate ; then
   echo $UGET --xs-upgrade
    $UGET --xs-upgrade
elif test "x$ACCION" = xdistupdate ; then
    echo $UGET --dist-upgrade
    $UGET --dist-upgrade
elif test "x$ACCION" = xdepclean ; then
    echo   No se
elif test "x$ACCION" = xstats ; then
    echo No hay
elif test "x$ACCION" = xADDREPO ; then
 echo NO APLICA
else
  for arg in $PAQS 
  do
    echo $UGET $ACCION $arg
    $UGET $ACCION $arg
   done
fi


