#!/bin/bash

export CMIS_ROOT_URL=http://localhost:8080/nuxeo/json/cmis/default/root
#export CMIS_ROOT_URL=http://demo.nuxeo.com/nuxeo/json/cmis/default/root
export AUTH_PARM="-u Administrator:Administrator"
#export AUTH_PARM="-u user1:user1"

export TEST_FOLDERPATH="default-domain/workspaces/CMISTEST%20workspace"
#export TEST_FOLDERPATH="test-domain/workspaces/CMISTEST%20workspace"

export STOP_FILE="./test-cmis-creates-deletes.sh.stop"
if [ -f "${STOP_FILE}" ]; then
    echo "Stop file ${STOP_FILE} exists!"
    exit 10
fi

export TEST_FILE="./testFile.bin"
if [ ! -f "${TEST_FILE}" ]; then
    echo "Test file ${TEST_FILE} does not exist!"
    exit 10
fi


getObjectIdByPath() {
    local OBJECTPATH="$1"
    RESPONSE_FILEPATH=$(mktemp -u)
    HTTP_CODE=$(curl -s -o "${RESPONSE_FILEPATH}" -w "%{http_code}" ${AUTH_PARM} \
        "${CMIS_ROOT_URL}/${OBJECTPATH}?cmisselector=properties")
    if [ "${HTTP_CODE}" -lt 200 ] || [ "${HTTP_CODE}" -gt 299 ]; then
        echo "getObjectByPath error: ${HTTP_CODE}, dumpFile: ${RESPONSE_FILEPATH}."
        exit 11
    fi
    local OBJECTID=$(cat ${RESPONSE_FILEPATH} | python -c \
        'import json,sys;obj=json.load(sys.stdin);print obj["cmis:objectId"]["value"]')
    rm -f ${RESPONSE_FILEPATH}
    echo ${OBJECTID}
}


createDocument() {
    local PARENTOBJECTID="$1"
    local NAME="$2"
    RESPONSE_FILEPATH=$(mktemp -u)
    HTTP_CODE=$(curl -s -o "${RESPONSE_FILEPATH}" -w "%{http_code}" ${AUTH_PARM} \
        -F "cmisaction=createDocument" \
        -F "propertyId[0]=cmis:objectTypeId" \
        -F "propertyValue[0]=File" \
        -F "propertyId[1]=cmis:name" \
        -F "propertyValue[1]=${NAME}" \
        -F "versioningState=major" \
        -F "content=@${TEST_FILE}" \
        "${CMIS_ROOT_URL}?objectId=${PARENTOBJECTID}")
    if [ "${HTTP_CODE}" -lt 200 ] || [ "${HTTP_CODE}" -gt 299 ]; then
        echo "createDocument error: ${HTTP_CODE}, dumpFile: ${RESPONSE_FILEPATH}."
        exit 11
    fi
    local OBJECTID=$(cat ${RESPONSE_FILEPATH} | python -c \
        'import json,sys;obj=json.load(sys.stdin);print obj["properties"]["cmis:objectId"]["value"]')
#    rm -f ${RESPONSE_FILEPATH}
    echo ${OBJECTID}
}


deleteObject() {
    local OBJECTID="$1"
    RESPONSE_FILEPATH=$(mktemp -u)
    HTTP_CODE=$(curl -s -o "${RESPONSE_FILEPATH}" -w "%{http_code}" ${AUTH_PARM} \
        -F "cmisaction=delete" \
        -F "allversions=true" \
        "${CMIS_ROOT_URL}?objectId=${OBJECTID}")
    if [ "${HTTP_CODE}" -lt 200 ] || [ "${HTTP_CODE}" -gt 299 ]; then
        echo "deleteObject error: ${HTTP_CODE}, dumpFile: ${RESPONSE_FILEPATH}."
        exit 11
    fi
    rm -f ${RESPONSE_FILEPATH}
}


doCmisWork() {
    TEST_FILENAME="testFile-${1}-${2}.txt"

    TEST_FOLDERID=$(getObjectIdByPath "${TEST_FOLDERPATH}")
    RETCODE=$?
    if [ ${RETCODE} -ne 0 ]; then echo ${TEST_FOLDERID}; exit ${RETCODE}; fi

    TEST_FILEID=$(createDocument ${TEST_FOLDERID} "${TEST_FILENAME}")
    RETCODE=$?
    if [ ${RETCODE} -ne 0 ]; then echo ${TEST_FILEID}; exit ${RETCODE}; fi
    printf "C"

#    sleep 0.1

    deleteObject ${TEST_FILEID}
    RETCODE=$?
    if [ ${RETCODE} -ne 0 ]; then echo ${TEST_FILEID}; exit ${RETCODE}; fi
    printf "D"
}


COUNT=0
PID=$$
while : ; do
    [[ -f "${STOP_FILE}" ]] && break
    COUNT=$((${COUNT} + 1))
    doCmisWork ${PID} ${COUNT}
done
