#!/bin/bash

CREDENTIALS=/home/user/secretfile
PSTOOLS_SRC=/home/user/pstools/
TOOLS_DIR=tools
TOOLS_UNIT=d
RAND_DIR=$1-$RANDOM

[ $# -lt 1 ] && echo "Error, too few parameters" && echo "Use: $0 server [unit]" && exit
[ ! -z "$2" ] && TOOLS_UNIT=$2

mkdir /tmp/$RAND_DIR
smbmount //$1/$TOOLS_UNIT$ /tmp/$RAND_DIR -o credentials=$CREDENTIALS

if [ $? -eq 0 ];then
  echo "Default credentials"
else
  echo "Default credentials doesn't authentify. Try others."
  read -p "Type username (DOMAIN/user):" user
  read -s -p "Type password:" pass
  smbmount //$1/$TOOLS_UNIT$ /tmp/$RAND_DIR -o username=$user,password=$pass
  [ $? -ne 0 ] &&  echo "Credentials error or unit not available (check smbmount errors)" && rmdir /tmp/$RAND_DIR && exit 1
fi

mkdir /tmp/$RAND_DIR/$TOOLS_DIR
cp -av $PSTOOLS_SRC /tmp/$RAND_DIR/$TOOLS_DIR/.
smbumount /tmp/$RAND_DIR

rmdir /tmp/$RAND_DIR
