#!/usr/bin/env bash

# Copyright 2008, 2009, 2010, 2011, 2012 Roland Olbricht
#
# This file is part of PT_Diagrams.
#
# PT_Diagrams is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# PT_Diagrams is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PT_Diagrams.  If not, see <http://www.gnu.org/licenses/>.


CACHE_DIR="/tmp/osm3s_augmented_diffs_cache/"


# Do HTTP headers with respect to CORS
echo "Content-Type: text/plain; charset=utf-8"
if [[ -n $HTTP_ACCESS_CONTROL_REQUEST_HEADERS ]]; then
  echo "Access-Control-Allow-Headers: $HTTP_ACCESS_CONTROL_REQUEST_HEADERS"
fi
if [[ -n $HTTP_ORIGIN ]]; then
  echo "Access-Control-Allow-Origin: *"
fi
if [[ $REQUEST_METHOD == "OPTIONS" ]]; then {
  echo "Access-Control-Allow-Methods: GET, POST, OPTIONS"
  echo "Content-Length: 0"
  echo
  exit 0
}; fi
echo

if [[ -r "$CACHE_DIR/newest" ]]; then
  cat "$CACHE_DIR/newest"
  exit 0
fi

TIMESTAMP=`./timestamp | tail -n 1`

if [[ ${TIMESTAMP:0:1} == "2" ]]; then
  EPOCHSECS=`date --date=$TIMESTAMP '+%s'`
  echo $(( ( $EPOCHSECS - 1347432960 ) / 60 ))
fi
