#!/bin/bash
#
# This script updates all timestamps in the HTML files of OX App Suite.
#
# This work is provided under the terms of the CREATIVE COMMONS PUBLIC
# LICENSE. This work is protected by copyright and/or other applicable
# law. Any use of the work other than as authorized under this license
# or copyright law is prohibited.
#
# http://creativecommons.org/licenses/by-nc-sa/2.5/
#
# Copyright 2016 OX Software GmbH, Germany. info@open-xchange.com
#

NOW=$(date -u +%Y%m%d.%H%M%S)

usage(){
    cat <<EOF

    Command usage: $(basename ${0}) [option]

    -h|--help            : displays this usage message
    --timestamp=<value>  : default: e.g. ${NOW}

    This tool creates timestamps on OX App Suite frontend Javascript-Code
    for browser cache invalidation.
EOF
}

for argument in "${@}"; do
    case ${argument} in
        -h|--help)
            usage 1
        ;;
        --timestamp=*)
            NOW=${argument##--timestamp=}
        ;;
        *)
            echo "${0}: Error, unknown argument: ${argument}"
            usage 1
        ;;
    esac
done

## cd ##

if [ ! -f core ]; then
    echo "HTML files not found in $(pwd)" >&2
    exit 1
fi

VERSION=$(sed -n \
    '/v=/{s/^.*v=\([0-9A-Za-z._-]*\)\.[0-9]\{8\}\.[0-9]\{6\}.*$/\1/p;q}' \
    ui)

for f in ui core signin boot.js; do
    sed -i "s/${VERSION}"'\.[0-9]\{8\}\.[0-9]\{6\}/'"${VERSION}.${NOW}/g" "$f"
done
