#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Invoke the FISE stateless service and get RDF descriptions
# POST files in folder {1} and saves output in folder {2}
# Input files are intendeed to be text/plain and have .txt extension
# For each .txt file, a related .uri file must exist, which contains the URI to be given the content-item
# FISE instance can be set: $fise
# Wait between two requests $slp seconds

fise="http://localhost:8080/engines"

slp=0.1

directory=${1}
if [[ $directory = "" ]]; then
  directory="./"
fi
directory=`cd $directory; pwd;`
outdir=${2}

if [[ $outdir = "" ]]; then
  outdir="./"
fi
outdir=`cd $outdir; pwd;`

echo Post files in dir: $directory
echo Save rdf in dir: $outdir

cd $directory

ls|grep .txt |while read fname
do
 rdfname=$(echo $fname|sed -e 's/\.txt/.rdf/')
 outname="$outdir/$rdfname"
if [[ -f $outname ]] 
then
   echo Skipping $fname : out file exists!
else
   sleep $slp
   furi=$(echo $fname|sed -e 's/\.txt/.uri/')
   uri=`cat $furi`
   echo Getting enhancements of $uri and saving it to $outname
   curl -X POST -H "Accept: application/rdf+xml" -H "Content-type: text/plain" -d @$fname "$fise?uri=$uri" > $outname
fi
done
