#!/bin/bash

TMP_DIR="./test_tmp"
DATA_DIR="/tmp/Storage/DATA"
host=192.168.11.5
port=3001

rm -rf $TMP_DIR
rm -rf $DATA_DIR/*

mkdir $TMP_DIR

echo "test data 1"  > $TMP_DIR/test_data_1
echo "test data 2"  > $TMP_DIR/test_data_2
echo "test data 3"  > $TMP_DIR/test_data_3
echo "test data update"  > $TMP_DIR/test_data_4
echo "test data PUT"  > $TMP_DIR/test_data_5
echo "test 日本語 PUT"  > $TMP_DIR/test_data_jp
echo "test 日本語 POST"  > $TMP_DIR/test_data_jp2

# version
echo "get version"
curl -s "$host:$port"
echo  ""

# list
echo "list test"
curl -s "$host:$port/storage/" -o $TMP_DIR/list.data 
cat $TMP_DIR/list.data
echo  ""

# create
echo "create test"
curl -s -F "name=mydata" -F "file=@$TMP_DIR/test_data_1" "$host:$port/storage/"
curl -s -F "name=DIR1/DIR2/mydata" -F "file=@$TMP_DIR/test_data_3" "$host:$port/storage/"
curl -s -F "name=DIR1/mydata" -F "file=@$TMP_DIR/test_data_2" "$host:$port/storage/"
curl -s -X PUT -T $TMP_DIR/test_data_5 "$host:$port/storage/mydata3"
find $DATA_DIR
echo  ""


# delete 
echo "delete test"
echo "delete mydata"
curl -s -X DELETE "$host:$port/storage/mydata"
find $DATA_DIR
echo  ""

echo "delete DIR1/mydata"
curl -s -X DELETE "$host:$port/storage/DIR1/mydata"
find $DATA_DIR
echo  ""

echo "delete DIR1/DIR2/mydata"
curl -s -X DELETE "$host:$port/storage/DIR1/DIR2/mydata"
find $DATA_DIR
echo  ""

echo "delete mydata3"
curl -s -X DELETE "$host:$port/storage/mydata3"
find $DATA_DIR
echo  ""

# create 
echo "create test 2"
curl -s -F "name=DIR1/mydata2" -F "file=@$TMP_DIR/test_data_2" "$host:$port/storage/"
curl -s -F "name=mydata2" -F "file=@$TMP_DIR/test_data_1" "$host:$port/storage/"
curl -s -F "name=DIR1/DIR2/mydata2" -F "file=@$TMP_DIR/test_data_3" "$host:$port/storage/"
find $DATA_DIR
echo  ""

# get
echo "get test"
curl -s "$host:$port/storage/DIR1/mydata2" -o $TMP_DIR/get.data
cat $TMP_DIR/get.data
echo  ""

# create 上書き
echo "create test upadte"
curl -s -F "name=DIR1/mydata2" -F "file=@$TMP_DIR/test_data_4" "$host:$port/storage/"
find $DATA_DIR
echo  ""

# get
echo "get test"
curl -s "$host:$port/storage/DIR1/mydata2" -o $TMP_DIR/get.data
cat $TMP_DIR/get.data
echo  ""

# info
echo "info"
curl -s -I "$host:$port/storage/DIR1/mydata2" -o $TMP_DIR/info.data
cat $TMP_DIR/info.data
echo  ""

# list
echo "list test"
curl -s "$host:$port/storage/" -o $TMP_DIR/list.data 
cat $TMP_DIR/list.data
echo  ""

# delete 
echo "delete test"
curl -s -X DELETE "$host:$port/storage/DIR1/mydata2"
find $DATA_DIR
echo  ""

# delete 存在しない
echo "delete test no data"
curl -s -X DELETE "$host:$port/storage/DIR1/mydata2"
find $DATA_DIR
echo  ""

# get 存在しない
echo "get test no data"
curl -s "$host:$port/storage/DIR1/mydata2" -o $TMP_DIR/get.data
cat $TMP_DIR/get.data
echo  ""

# info 存在しない
echo "info test no data"
curl -s -I "$host:$port/storage/DIR1/mydata2" -o $TMP_DIR/info.data
cat $TMP_DIR/info.data
echo  ""

# list
echo "list test"
curl -s "$host:$port/storage/" -o $TMP_DIR/list.data 
cat $TMP_DIR/list.data
echo  ""

# create
curl -s -X PUT -T $TMP_DIR/test_data_5 "$host:$port/storage/mydata33"
echo "get test"
curl -s "$host:$port/storage/mydata33" -o $TMP_DIR/get.data
cat $TMP_DIR/get.data
echo  ""

# create
echo "create 日本語"
jpname=`echo '日本語ファイル名 PUT' | perl -MURI::Escape -ne 'chomp;print uri_escape($_)'`
curl -s -X PUT -T $TMP_DIR/test_data_jp "$host:$port/storage/$jpname"
echo "get test"
curl -s "$host:$port/storage/$jpname" -o $TMP_DIR/get.data
cat $TMP_DIR/get.data
echo  ""

# list
echo "list test"
curl -s "$host:$port/storage/" -o $TMP_DIR/list.data 
cat $TMP_DIR/list.data
echo  ""


# delete 
echo "delete test"
echo "delete 日本語ファイル名 PUT"
jpname=`echo '日本語ファイル名 PUT' | perl -MURI::Escape -ne 'chomp;print uri_escape($_)'`
curl -s -X DELETE "$host:$port/storage/$jpname"
find $DATA_DIR
echo  ""

# list
echo "list test"
curl -s "$host:$port/storage/" -o $TMP_DIR/list.data 
cat $TMP_DIR/list.data
echo  ""


# create 
echo "create 日本語 2"
curl -s -F "name=DIR1/日本語ファイル名 POST" -F "file=@$TMP_DIR/test_data_jp2" "$host:$port/storage/"
echo "get test"
jpname=`echo '日本語ファイル名 POST' | perl -MURI::Escape -ne 'chomp;print uri_escape($_)'`
curl -s "$host:$port/storage/DIR1/$jpname" -o $TMP_DIR/get.data
cat $TMP_DIR/get.data
echo  ""
find $DATA_DIR
echo  ""

# delete 
echo "delete test"
echo "delete 日本語ファイル名 POST"
jpname=`echo '日本語ファイル名 POST' | perl -MURI::Escape -ne 'chomp;print uri_escape($_)'`
curl -s -X DELETE "$host:$port/storage/DIR1/$jpname"
find $DATA_DIR
echo  ""

# list
echo "list test"
curl -s "$host:$port/storage/" -o $TMP_DIR/list.data 
cat $TMP_DIR/list.data
echo  ""

