#!/usr/bin/env bash

# Copyright (c) 2019 The STE||AR-Group
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

# Script to introduce the deprecated include after factorazing in modules
# This file is globbing through the include_compatibility folder, so basic 
# files should already be here

# There is a possibility to specify the files manually

script_sourced=0
# Important to be at the beginning
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
    script_sourced=1
fi

function _exit() {
    if [[ $script_sourced -eq 0 ]]; then
        exit
    fi
}

function extra_usage_message() {
    echo
    echo "- Can specify the --project_path if different from $HPX_ROOT"
    echo "- Can also specify some target files if no globbing (without any extension) with:"
    echo "--files \"<filename1> <filename2>\""
    echo "Example with files: ./libs/add_compat_headers.sh -m module --files file -o hpx/util -p \$PWD"
}

if [[ $# -lt 1 ]]; then
    arg=${BASH_SOURCE[0]}
    echo "Usage : "$arg" -m <module_name> --old_path <include_path> --new_path <include_path>"
    echo "Example: "$arg" -m cache -o hpx/util/cache -n hpx/cache"
    extra_usage_message
    _exit
    return
fi

function parse_arguments() {

    # store arguments list
    POSITIONAL=()

    while [[ $# -gt 0 ]]
    do
        local key="$1"
        case $key in
            -f|--files)
                all_files=0
                files=$2
                echo "manually specified files => no globbing"
                shift # pass option
                shift # pass value
                ;;
            -m|--module)
                module=$2
                echo "module : ${module}"
                shift # pass option
                shift # pass value
                ;;
            -n|--new_path)
                new_path_set=true
                new_path=$2
                echo "new_path : ${new_path}"
                shift # pass option
                shift # pass value
                ;;
            -o|--old_path)
                old_path_set=true
                old_path=$2
                echo "old_path : ${old_path}"
                shift # pass option
                shift # pass value
                ;;
            -p|--project_path)
                project_path=$2
                shift # pass option
                shift # pass value
                ;;
            --help|*)
                echo $"Usage: $0 [-m, --module <value>] [-o, --old_path <value>]"
                echo "[-n, --new_path <value>] [-p, --project_path <value>]"
                echo "[-f, --files \"<value1> <value2>\"]"
                echo "Example: "$0" -m cache -o hpx/util/cache -n hpx/cache"
                extra_usage_message
                _exit
                return
        esac
    done

    # restore positional parameters
    set -- "${POSITIONAL[@]}"

}

# Default values which can be overwritten while parsing args
project_path=$HPX_ROOT
module=cache
new_path_set=false
old_path_set=false
# Default is globbing compatibility files
all_files=1
files=

# Parsing arguments
parse_arguments "$@"

echo
echo "project_path: ${project_path}"
# Usual vars (depend on the parsing step)
libs_path=$project_path/libs
module_path=$libs_path/${module}
module_caps=${module^^}

# Error handling
if [[ "$old_path_set" = "false" ]]; then
    old_path=hpx/util/${module}
fi
if [[ "$new_path_set" = "false" ]]; then
    new_path=hpx/${module}
fi
if [[ "$old_path_set" = "false" && $all_files -eq 0 ]]; then
    echo "Attention only the basename of the files should be specified"
fi
# Project path not set (full specified path to be sure which source is used)
if [[ -z $HPX_ROOT && -z $project_path ]]; then
    "HPX_ROOT env var doesn't exists and project_path option not specified !"
    _exit
    return
fi

pushd $module_path/include_compatibility > /dev/null
if [[ $? -eq 1 ]]; then
    echo "Please specify a correct project_path"
    _exit
    return
fi
echo "current path: $module_path/include_compatibility"

# To enable **
shopt -s globstar
# Globbing step to get all the include_compatibility files, the files have to
# already be there, we are just rewriting them
if [[ all_files -eq 0 ]]; then
    files=($files)  # Make the string become an array
    files=(${files[@]/%/.hpp})
    files=(${files[@]/#/$old_path/})
else
    files=($(ls **/*.hpp))
fi

echo
echo "Files overwritten :"
# Create the corresponding compatibility headers
for full_file in "${files[@]}"; do
    # basename not used otherwise we lose hierarchy if any
    f=${full_file#"$old_path/"}
    echo $full_file

# No indentation otherwise it appears in the cat
cat >${full_file} <<EOL
//  Copyright (c) 2019 Ste||ar Group
//
//  SPDX-License-Identifier: BSL-1.0
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <hpx/config.hpp>
#include <hpx/${module}/config/defines.hpp>
#include <${new_path}/${f}>

#if defined(HPX_${module_caps}_HAVE_DEPRECATION_WARNINGS)
#if defined(HPX_MSVC)
#pragma message( \\
    "The header ${old_path}/${f} is deprecated, \\
    please include ${new_path}/${f} instead")
#else
#warning \\
    "The header ${old_path}/${f} is deprecated, \\
    please include ${new_path}/${f} instead"
#endif
#endif
EOL

done

popd > /dev/null
