#!/bin/bash

PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
. $PROGPATH/winvars.sh

winexe //$1 "$TOOLS_UNIT:\\$TOOLS_DIR\\$2 /accepteula" $PSCREDENTIALS
rc=$?

if [ $rc -eq 0 ];then
  echo "Default credentials"
elif [ $rc -eq 1 ];then
  echo "$? Hola"
  echo "Default credentials doesn't authentify. Try others."
  read -p "Type username (DOMAIN/user):" user
  read -s -p "Type password:" pass
  PSCREDENTIALS="--user $user --password $pass"
  winexe //$1 "$TOOLS_UNIT:\\$TOOLS_DIR\\pstools\\pslist.exe /accepteula -t" $PSCREDENTIALS
  rc=$?
  if [ $rc -eq 1 ];then
    echo "Credentials error or unit not available (check smbmount errors)" && exit 1
  elif [ $rc -eq 99 ];then
    echo "There's no pstools, I'll copy them"
    $PROGPATH/cp_pstools.sh $1 $user $pass
    [ $? -ne 0 ] && echo "There's no pstools and I couldn't copy them." && exit 1
    winexe //$1 "d:\\scripts\\pstools\\pslist.exe /accepteula -t" $PSCREDENTIALS
    rc=$?
    if [ $rc -eq 99 ];then
      echo "Pstools are copied, but they don'y work, somethings going on." && exit 1
    fi
  fi
elif [ $rc -eq 99 ];then
  echo "There's no pstools, I'll copy them"
  $PROGPATH/cp_pstools.sh $1
  [ $? -ne 0 ] && echo "There's no pstools and I couldn't copy them." && exit 1  
  winexe //$1 "d:\\scripts\\pstools\\pslist.exe /accepteula -t" $PSCREDENTIALS
  rc=$?
  if [ $rc -eq 99 ];then
      echo "Pstools are copied, but they don'y work, somethings going on." && exit 1
  fi
fi


