#!/usr/bin/env bash
#
#       Copyright (c) 2018 German Aerospace Center DLR e.V. (GSOC)
#
#       Licensed 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.
#
#       This file is part of the openvocs project. http://openvocs.org
#
#       ------------------------------------------------------------------------
#
#       File            create complie_commands.json.sh
#       Authors         Markus Toepfer
#       Authors         Michael Beer
#       Date            2021-10-1
#
#       This script is based on
#       https://sarcasm.github.io/notes/dev/compilation-database.html#clang
#       ------------------------------------------------------------------------

SOURCE=$OPENVOCS_ROOT/build/obj
TARGET=$OPENVOCS_ROOT/compile_commands.json
TEMP=$(mktemp)

if [ $1 ]; then

    if [ -z $2 ]; then

        echo "You MUST specify in and out or none"

        exit 1

    fi

    SOURCE=$1
    TARGET=$2

fi

echo "" > $TEMP
for i in `find $SOURCE -name "*.o.json"`;
    do cat $i >> $TEMP;
done
sed -e '1s/^/[\n/' -e '$s/,$/\n]/' $TEMP > $TARGET
rm $TEMP
