#! /bin/bash

if [ -f <CONFIGDIR>/globals.sh ]; then
    . <CONFIGDIR>/globals.sh
fi

PREURL="http://${INTRAHOST}/${ZOPEPRODUCT}"

function usage() {
    echo "usage: ${0##*/} [-u login] [-p password] [-v] dept assistant commercial [file ...]"
    exit 2
}

login="$DBUSER"
password="$DBPWD"
verbose=0

while getopts :u:p:v OPT; do
    case $OPT in
        u)
            login="$OPTARG"
            ;;
        p)
            password="$OPTARG"
            ;;
        v)
            verbose=1
            ;;
        *)
            usage
    esac
done
shift $(( OPTIND - 1 ))
OPTIND=1

if [ $# -lt 3 ]; then
    usage
fi

dept="$1"
assistant="$2"
commercial="$3"
shift 3

url="$PREURL/$dept/$assistant/$commercial/import_commandes"

for file in "$@"; do
    if [ $verbose = 1 ]; then
        echo "Import des commandes du fichier $file" 2>&1
    fi
    curl -u "$login:$password" -F "file=@$file;type=text/xml" "$url"
done

