#!/bin/bash
#
# Copyright 2019 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# 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.

set -euo pipefail
source es_util_env

function usage() {
cat << EOF

Import kibana objects from a search response to the kibana index found in the first entry of
a search response exported by 'export-kibana-objects'. NOTE: This script
overwrites/updates any objects of the same type and id

usage:
  $0 <objects_json>

  objects_json   A search response JSON from the export script

EOF
exit "1"
}

while IFS= read -r -t 5 line
do
    file="${file:-}${line}"
done < "${1:-/dev/stdin}"

if [ -z "${file:-}" ] ; then
    usage
fi


CMD=${CMD:-es_util}
kibindex=$(
        echo "$file" | python -c '
import sys
import json
obj = json.load(sys.stdin)
if obj["hits"]["total"] == 0:
    sys.stdout.write("")
sys.stdout.write(obj["hits"]["hits"][0]["_index"])
'
)
query="${kibindex}/_bulk"
response=$({
        echo "$file" | python -c '
import sys
import json
obj = json.load(sys.stdin)
for doc in obj["hits"]["hits"]:
  hdr = {"index":{"_type":doc["_type"],"_id":doc["_id"]}}
  json.dump(hdr, sys.stdout)
  sys.stdout.write("\n")
  json.dump(doc["_source"], sys.stdout)
  sys.stdout.write("\n")
'
} | ${CMD} --query="${query}" -H "Content-type: application/json" -XPOST --data-binary @-)

echo $response | python -c '
import sys
import json
obj = json.load(sys.stdin)
if obj["errors"]:
    sys.stdout.write("There were errors importing objects:\n")
    sys.stdout.write(json.dumps(obj, indent=4))
    sys.stdout.write("\n")
else:
    sys.stdout.write("Success\n")
'
