#!/bin/bash

# according to /usr/include/sysexit.h
EXIT_USAGE_ERROR=64

# create PS file
# example, tc-ps create 20 /tmp/test.img
if [ $# -eq 3 ]; then

    if [ $1 == "create" ]; then
        install -Dv /dev/null "$3"
        dd if=/dev/zero of="$3" seek="$2G" count=0 bs=1
        exit $?
    fi

    if [ $1 == "commit" ]; then
        pre="/home/tc/opt/ps"
        udf="$2G.img-guest.udf"
        mdf="$2G.img-guest.mdf"
        upload="/home/tc/upload/ps/"
        install -Dv "$pre/$mdf" "$upload/$2G.img-$3.mdf"
        install -Dv "$pre/$udf" "$upload/$2G.img-$3.udf"
        api="http://localhost/tc/rest/user.php/$3"
        mdf_p="\"mdf\":\"$2G.img-$3.mdf\""
        udf_p="\"udf\":\"$2G.img-$3.udf\""
        str='{"uri": "head","revision": "1"'
        str="$str, $mdf_p"
        str="$str, $udf_p"
        str="$str}"
        curl -H "Content-Type:application/json" -X POST --data "$str" $api
        exit $?
    fi

fi

echo "tc-ps create <GiB> <file>"
echo "      commit <GiB> <uid>"
exit $EXIT_USAGE_ERROR
