#!/bin/bash

SLEEP_TIME="0.5"

TEXTDOMAIN="ina-diagnostics"
TEXTDOMAINDIR="/usr/share/ina/ina-diagnostics/locale"
export TEXTDOMAIN TEXTDOMAINDIR

if [ -n "$1" ]; then
    export LANG=$1
fi

if [ -n "$2" ]; then
    export LANGUAGE=$2
fi

gettext() {
    local msgid="$1"
    local domain="${TEXTDOMAIN:-messages}"
    /usr/bin/gettext -s -d "$domain" "$msgid"
}

#env > /tmp/123.txt
#Wayland: XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.N8Q651
#Xorg:    XAUTHORITY=/run/user/1000/gdm/Xauthority

whoami | grep root
if [[ "$?" -eq 0 ]]
    then postfix="_su"
        userID=`env | grep XAUTHORITY | awk -Fuser/ '{print $NF}' | awk -F\/ '{print $1}'`
        userName=`cat /etc/passwd | grep $userID | awk -F: '{print $1}'`
    else postfix="_user"
        userName=`whoami`
fi

echo -e "userName: $userName\npostfix: $postfix"
# date like 8 March 2024 17-48: 08.03.2024_17.48
date_part=`date '+%d.%m.%Y_%H.%M'`
# hostname cut to first 5 symbols
host_name=`hostname | cut -c1-5`

if [[ -d /home/$userName ]]
    then target="/home/$userName/hw_report$postfix.$date_part.$host_name.tar.gz"
    else target="/tmp/hw_report$postfix.$date_part.$host_name.tar.gz"
fi

#echo "target = $target" >> /tmp/123.txt

title=$(gettext "Diagnostic data collection")
text1=$(gettext "Do you want to run the hardware and system logs collection script?")
text_in_progress=$(gettext "Collecting necessary information...")
text_cancelled=$(gettext "Cancelled, operation not completed!")
text_error=$(gettext "An unexpected error occurred!")
text2=$(gettext "An archive %s will be created")
text3=$(gettext "Send the generated archive to Ina OS support %s")
text_open=$(gettext "Show file")

# Format the strings with the $target parameter
text2=$(printf "$text2" "$target")
text3=$(printf "$text3" "\n\n$target")

#zenity --width=384 --height=128 --warning --title "$title"  --text "$text1\n$text2"

zenity  --width=384 --height=128 --question --title "$title"  --text "$text1\n\n$text2"; code=$?
if [ $code -eq 0 ]
    then echo "Proceeding..."
    else echo "Exiting..."
        exit 1
fi


tmpdir=`mktemp -d` ; code=$?
[[ $code -eq 0 ]] || {
    zenity --error --text="$text_error"
    exit 2
}

(
echo "10" ; sleep $SLEEP_TIME
lspci -vv > $tmpdir/lspci.txt

echo "20" ; sleep $SLEEP_TIME
lsusb -vv > $tmpdir/lsusb.txt

echo "30" ; sleep $SLEEP_TIME
uname -a  > $tmpdir/dmidecode.txt
echo "dmidecode output:" >> $tmpdir/dmidecode.txt
dmidecode >> $tmpdir/dmidecode.txt

echo "35" ; sleep $SLEEP_TIME
[ -d /usr/share/upmd/ ] && {
    cp /usr/share/upmd/* $tmpdir
}
[[ -f /var/ina/ina-license/machine-hash ]] && cp /var/ina/ina-license/machine-hash $tmpdir

echo "40" ; sleep $SLEEP_TIME
cp /var/log/syslog $tmpdir

echo "50" ; sleep $SLEEP_TIME
[ -f /var/log/ina-update.log ] && {
    cp /var/log/ina-update.log $tmpdir
}
[ -f /var/log/ina-kernel68-update.log ] && {
    cp /var/log/ina-kernel68-update.log $tmpdir
}
cp /usr/bin/ina-activate $tmpdir
cp /usr/lib/ina/ina-license/ina-activate.py $tmpdir

echo "55" ; sleep $SLEEP_TIME
i="1"
for log in `find /var/lib/dkms/ -name "make.log"`
do
    cp $log $tmpdir/make.log.$i
    i=`expr $i \+ 1`
done

echo "60" ; sleep $SLEEP_TIME
cp /var/log/kern.log $tmpdir

echo "65" ; sleep $SLEEP_TIME
cp /etc/apt/sources.list $tmpdir
ls -l /etc/apt/sources.list.d/ >> $tmpdir/apt.other.ls

if [ "$postfix" == "_user" ]
then echo "70" ; sleep $SLEEP_TIME
    pactl list cards > $tmpdir/pactl.txt
fi

echo "75" ; sleep $SLEEP_TIME
apt list --installed > $tmpdir/apt_list.txt
flatpak -d list > $tmpdir/flatpak_list.txt

echo "80" ; sleep $SLEEP_TIME
pushd $tmpdir
GZIP=-9 tar -czvf $target *
popd

echo "90" ; sleep $SLEEP_TIME
rm -fr $tmpdir
) |
zenity --progress --width=384 --height=128 \
  --title="$title" \
  --text="$text_in_progress" \
  --auto-close \
  --auto-kill \
  --percentage=0

if [ "$?" -eq 2 ] ; then
    echo "Exiting"
    exit 2
fi

if [ "$?" = -1 ] ; then
        zenity --error \
          --text="$text_cancelled"
    [ -d $tmpdir ] && rm -fr $tmpdir
    exit 1
fi

chown $userName:$userName $target

zenity  --width=384 \
    --height=128 \
    --info \
    --ok-label "$text_open" \
    --extra-button "OK" \
    --title "$title" \
    --text "$text3"

ret=$?
case "$ret" in
  0) nautilus -s $target & ;;
  1) echo 'Ok pressed'; exit 0;;
  *) echo 'unexpected (ESC?)'; exit 500;;
esac
