#!/bin/bash

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

set -euo pipefail

# Logs uploading function that contains the sequence of  collecting, merging and uploading
# the log files
cd "${BASH_SOURCE%/*}" || exit


mkdir -p ~/.cache/Microsoft/Identity/Broker/Logs

pushd ~/.cache/Microsoft/Identity/Broker/Logs > /dev/null

touch mainlogs
touch userlogs
touch brokerlogs
touch edge_auth
touch edge_auth_logs
touch intune_agent_logs
touch intune_portal_logs
touch intune_daemon_logs

#delete these files on interrupt / exit
# trap command manual: https://www.linuxjournal.com/content/bash-trap-command
trap "rm ~/.cache/Microsoft/Identity/Broker/Logs/*" INT EXIT

echo "Collecting & Merging Logs..."
echo " 1. Collecting CPP Broker Device Logs.. Requires permission to collect logs from a System Service"
sudo journalctl -t 'microsoft-identity-device-broker' -b > brokerlogs

echo " 2. Collecting CPP Broker Logs..."
journalctl -t 'microsoft-identity-broker' -b > userlogs

echo " 3. Collecting Intune logs"
journalctl -t 'intune-agent' -b > intune_agent_logs
journalctl -t 'intune-portal' -b > intune_portal_logs
sudo journalctl --system -t 'intune-daemon' -b > intune_daemon_logs

echo " 4. Collecting Microsoft Edge Logs"
# Edge logs are in this location: ~/.config/microsoft-edge-dev/chrome_debug.log
cat ~/.config/microsoft-edge-dev/chrome_debug.log > edge_auth 2>/dev/null || true

tail -n 3000  edge_auth >  edge_auth_logs 2>/dev/null || true
cat userlogs brokerlogs | sort -k1 -k2 -k3 -k4 > mainlogs


# Upload the logs
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

#. Create the incident
echo 'Creating Incident...'
# Assign variables from response from create incident command.
response=$(microsoft-identity-diagnostics-uploader --prod create-incident | jq '.')
id=$(echo "$response" | jq -r '.analysis."id"')
easyId=$(echo "$response" | jq -r '.analysis."easyId"')
analysisId=$(echo "$response" | jq -r '.analysis."analysisId"')

echo "Uploading Logs..."

#upload the logs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/mainlogs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/userlogs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/brokerlogs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/intune_agent_logs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/intune_portal_logs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/intune_daemon_logs
microsoft-identity-diagnostics-uploader --prod upload-file $id  ~/.cache/Microsoft/Identity/Broker/Logs/edge_auth_logs 2>/dev/null || true

echo 'Incident Details'
echo  - INCIDENT REFERENCE: ["$easyId"]
echo  - ID: ["$id"]
echo  - ANALYSIS ID: ["$analysisId"]
echo  - INCIDENT URL: ["https://powerlift.acompli.net/#/incidents/$id"]
echo  'Please share this link (INCIDENT URL) with your customer support contact.'

popd > /dev/null


# Use this while debugging.
# echo  - INCIDENT URL ["https://dev-powerlift-gym.acompli.net/#/incidents/$id"]
