#!/bin/sh

# Usage: Only an administrator of OSRF's amazon s3 bucket will be able to use
# this script.
#
# sh build/upload_doc.sh

# Check if the node was configured to use s3cmd
# This is done by running s3cmd --configure
if [ ! -f "${HOME}/.s3cfg" ]; then
    echo "No $HOME/.s3cfg file found. Please config the software first in your system"
    exit 1
fi

# Make sdf descriptions
rm @CMAKE_BINARY_DIR@/sdf/full_*.sdf
make sdf_descriptions

# Date/time stamp sdf descriptions
for f in @CMAKE_BINARY_DIR@/sdf/full_*.sdf; do
  cp ${f} ${f%.sdf}_@SDF_VERSION_FULL@_$(date +%Y%m%d_%H%M%S).sdf
done

# Upload sdf descriptions
s3cmd sync @CMAKE_BINARY_DIR@/sdf/full_*.sdf s3://osrf-distributions/sdformat/api/ --dry-run -v

echo -n "Upload sdf descriptions API(Y/n)? "
read ans

if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
  s3cmd sync @CMAKE_BINARY_DIR@/sdf/full_*.sdf s3://osrf-distributions/sdformat/api/  -v
fi

# Make documentation if not build
make doc
if [ ! -f "@CMAKE_BINARY_DIR@/doxygen/html/index.html" ]; then
  echo "Documentation not present. Install doxygen, and run `make doc` in the build directory"
  exit 1
fi

# The code API
s3cmd sync @CMAKE_BINARY_DIR@/doxygen/html/* s3://osrf-distributions/sdformat/api/@SDF_VERSION_FULL@/ --dry-run -v

echo -n "Upload code API(Y/n)? "
read ans

if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
  s3cmd sync @CMAKE_BINARY_DIR@/doxygen/html/* s3://osrf-distributions/sdformat/api/@SDF_VERSION_FULL@/ -v
fi
