#!/usr/bin/env bash

# This script generates information about your rippled installation
# and system. It can be used to help debug issues that you may face
# in your installation. While this script endeavors to not display any 
# sensitive information, it is recommended that you read the output
# before sharing with any third parties.


rippled_exe=/opt/ripple/bin/rippled
conf_file=/etc/opt/ripple/rippled.cfg

while getopts ":e:c:" opt; do
    case $opt in
        e)
            rippled_exe=${OPTARG}
            ;;
        c)
            conf_file=${OPTARG}
            ;;
        \?)
            echo "Invalid option: -$OPTARG"
            exit -1
    esac
done

tmp_loc=$(mktemp -d --tmpdir ripple_info.XXXXX)
chmod 751 ${tmp_loc}
awk_prog=${tmp_loc}/cfg.awk
summary_out=${tmp_loc}/rippled_info.md
printf "# rippled report info\n\n> generated at %s\n" "$(date -R)" > ${summary_out}

function log_section {
    printf "\n## %s\n" "$*" >> ${summary_out}

    while read -r l; do
        echo "    $l" >> ${summary_out}
    done </dev/stdin
}

function join_by {
    local IFS="$1"; shift; echo "$*";
}

if [[ -f ${conf_file} ]] ; then
    exclude=( ips ips_fixed node_seed validation_seed validator_token )
    cleaned_conf=${tmp_loc}/cleaned_rippled_cfg.txt
    cat << 'EOP' >> ${awk_prog}
    BEGIN {FS="[[:space:]]*=[[:space:]]*"; skip=0; db_path=""; print > OUT_FILE; split(exl,exa,"|")}
    /^#/ {next}
    save==2 && /^[[:space:]]*$/ {next}
    /^\[.+\]$/ {
      section=tolower(gensub(/^\[[[:space:]]*([a-zA-Z_]+)[[:space:]]*\]$/, "\\1", "g"))
      skip = 0
      for (i in exa) {
        if (section == exa[i])
          skip = 1
      }
      if (section == "database_path")
        save = 1
    }
    skip==1 {next}
    save==2 {save=0; db_path=$0}
    save==1 {save=2}
    $1 ~ /password/ {$0=$1"=<redacted>"}
    {print >> OUT_FILE}
    END {print db_path}
EOP

    db=$(\
        sed -r -e 's/\<s[[:alnum:]]{28}\>/<redactedsecret>/g;s/^[[:space:]]*//;s/[[:space:]]*$//' ${conf_file} |\
        awk -v OUT_FILE=${cleaned_conf} -v exl="$(join_by '|' "${exclude[@]}")" -f ${awk_prog})
    rm ${awk_prog}
    cat ${cleaned_conf} | log_section "cleaned config file"
    rm ${cleaned_conf}
    echo "${db}"  | log_section "database path"
    df ${db}      | log_section "df: database"
fi

# Send output from this script to a log file
## this captures any messages
## or errors from the script itself

log_file=${tmp_loc}/get_info.log
exec 3>&1 1>>${log_file} 2>&1

## Send all stdout files to /tmp

if [[ -x ${rippled_exe} ]] ; then
    pgrep rippled && \
    ${rippled_exe} --conf ${conf_file} \
    -- server_info                  | log_section "server info"
fi

cat /proc/meminfo                   | log_section "meminfo"
cat /proc/swaps                     | log_section "swap space"
ulimit -a                           | log_section "ulimit"

if command -v lshw >/dev/null 2>&1 ; then
    lshw    2>/dev/null             | log_section "hardware info"
else
    lscpu                           >  ${tmp_loc}/hw_info.txt
    hwinfo                          >> ${tmp_loc}/hw_info.txt
    lspci                           >> ${tmp_loc}/hw_info.txt
    lsblk                           >> ${tmp_loc}/hw_info.txt
    cat ${tmp_loc}/hw_info.txt | log_section "hardware info"
    rm ${tmp_loc}/hw_info.txt
fi

if command -v iostat >/dev/null 2>&1 ; then
    iostat -t -d -x 2 6             | log_section "iostat"
fi

df -h                               | log_section "free disk space"
drives=($(df | awk '$1 ~ /^\/dev\// {print $1}' | xargs -n 1 basename))
block_devs=($(ls /sys/block/))
for d in "${drives[@]}"; do
    for dev in "${block_devs[@]}"; do
        #echo "D: [$d], DEV: [$dev]"
        if [[ $d =~ $dev ]]; then
            # this file (if exists) has 0 for SSD and 1 for HDD
            if [[ "$(cat /sys/block/${dev}/queue/rotational 2>/dev/null)" == 0 ]] ; then
                echo "${d} : SSD" >> ${tmp_loc}/is_ssd.txt
            else
                echo "${d} : NO SSD" >> ${tmp_loc}/is_ssd.txt
            fi
        fi
    done
done

if [[ -f ${tmp_loc}/is_ssd.txt ]] ; then
    cat ${tmp_loc}/is_ssd.txt | log_section "SSD"
    rm ${tmp_loc}/is_ssd.txt
fi

cat ${log_file} | log_section "script log"

cat << MSG | tee /dev/fd/3
####################################################
  rippled info has been gathered. Please copy the
  contents of ${summary_out}
  to a github gist at https://gist.github.com/

  PLEASE REVIEW THIS FILE FOR ANY SENSITIVE DATA
  BEFORE POSTING! We have tried our best to omit
  any sensitive information from this file, but you
  should verify before posting.
####################################################
MSG

