#!/bin/bash if [[ -z "${2+x}" ]]; then DESCRIPTION=""; else DESCRIPTION="$2"; fi USER=$(whoami) LOG="/tmp/$USER-cron-gui-launcher-${DESCRIPTION}.log" printf '\n%s\n\n\nDetected environment variables:\n\n' "$(date +%Y-%m-%d_%H:%M:%S)" > "$LOG" unset DISPLAY; timeout=0 while [[ -z "$DISPLAY" ]]; do # Try to get DISPLAY using 'w' command first DISPLAY=$(w -h "$USER" | awk 'NF > 7 && $2 ~ /tty[0-9]+|^:/ {print $3; exit}' 2>/dev/null) if [[ -z "$DISPLAY" ]] then # Fallback to 'loginctl' if 'DISPLAY' is still null printf "Using loginctl...\n" DISPLAY=$(loginctl show-session $(loginctl | grep $USER | awk '{print $1}') -p Display --value) fi if [[ -z "$DISPLAY" ]] then # Fallback to 'xrandr' if 'DISPLAY' is still null printf "Using xrandr...\n" DISPLAY=$(xrandr | grep ' connected' | awk '{ print $1 }') fi if [[ -z "$DISPLAY" ]] then # Fallback to environment variable if still not set printf "Using env...\n" DISPLAY=$(env | grep "^DISPLAY=" | cut -d= -f2) fi if [[ -z "$DISPLAY" ]] then # Final fallback to ps for Xorg printf "Using ps...\n" DISPLAY=$(ps -ef | grep Xorg | grep -oP ':[0-9]+' | head -1) fi if [[ "$DISPLAY" == "" ]] then printf $DISPLAY XRDP_DISPLAY=$(ps -ef | grep -oP '/usr/lib/xorg/Xorg :[0-9]+ -auth .Xauthority.*log$' | grep -oP ':[0-9]+') if [[ "$XRDP_DISPLAY" != "" ]] then export DISPLAY="${XRDP_DISPLAY}.0" else printf $DISPLAY sleep 30 && ((timeout++)) if [[ ! -z "${3+x}" ]] && [[ "$timeout" -eq "$3" ]] then printf "Timeout: %s\n" "$timeout" >> "$LOG" exit 1 fi fi else export DISPLAY="$DISPLAY" fi done printf 'DISPLAY=%s\n' "$DISPLAY" >> "$LOG" get_environ(){ EnvVar=$(sed -zne "s/^$1=//p" "/proc/$2/environ" 2>/dev/null | tr -d '\0'); printf "%s" "$EnvVar"; } get_frequent(){ awk 'BEGIN{ FS=" " } { for(i=1;i<=NF;i++) print $i }' | \ awk '{ n=++hsh[$1]; if(n>max_occ){ max_occ=n; what=$1 } else if(n==max_occ){ if(what>$1) what=$1 } } END{ print what }' } export_environ(){ printf '\n\nExported environment (source file /proc/%s/environ):\n\n' "$1" >> "$LOG" EnvVarList=$(cat -e "/proc/$1/environ" 2>/dev/null | sed 's/\^@/\n/g' | tr -d '\0') for EnvVar in $EnvVarList do echo "export $EnvVar" >> "$LOG" export "$EnvVar" 2>/dev/null done } execute_input_commands(){ printf "%s" "$1" | awk 'BEGIN{ FS=" && "; print "\nInput command list:" } {for(i=1;i<=NF;i++) system("echo \"Command: " $i "\"") system("nohup " $i " >/dev/null 2>&1 &")}' >> "$LOG" } for PN in $(pgrep -U "$UID") do XDG_CURRENT_DESKTOP+=$(get_environ "XDG_CURRENT_DESKTOP" "$PN"; echo " ") done XDG_CURRENT_DESKTOP=$(echo -e "${XDG_CURRENT_DESKTOP[@]}" | get_frequent) declare -l DE && export DE="${XDG_CURRENT_DESKTOP/:*/}" && printf 'XDG_CURRENT_DESKTOP=%s\nDE=%s\n' "$XDG_CURRENT_DESKTOP" "$DE" >> "$LOG" if [[ "$DE" == "unity" ]]; then export_environ "$(pgrep gnome-session -n -U $UID)" elif [[ "$DE" == "gnome" ]]; then export_environ "$(pgrep gnome-session -n -U $UID)" elif [[ "$DE" == "ubuntu" ]]; then export_environ "$(pgrep gnome-session -n -U $UID)" elif [[ "$DE" == "gnome-classic" ]]; then export_environ "$(pgrep gnome-session -n -U $UID)" elif [[ "$DE" == "kde" ]]; then export_environ "$(pgrep startkde -n -U $UID)" elif [[ "$DE" == "mate" ]]; then export_environ "$(pgrep mate-session -n -U $UID)" elif [[ "$DE" == "lxde" ]]; then export_environ "$(pgrep lxsession -n -U $UID)" elif [[ "$DE" == "xfce" ]]; then export_environ "$(pgrep xfce4-session -n -U $UID)" elif [[ "$DE" == "xfce4" ]]; then export_environ "$(pgrep xfce4-session -n -U $UID)" elif [[ "$DE" == "x-cinnamon" ]]; then export_environ "$(pgrep cinnamon-session -n -U $UID)" else printf 'Your current Desktop Environment is not supported!\n Please contribute to https://github.com/pa4080/cron-gui-launcher\n' >> "$LOG" fi if [ -e ~/.local/share/timechamp/timechamp_logo.png ] then printf "TimeChamp Icon Exists\n" else printf "TimeChamp Icon does not Exists\n" wget -P ~/.cache https://tracker.timechamp.io/linux/timechamp_logo.png mv ~/.cache/timechamp_logo.png ~/.local/share/timechamp/ rm ~/.local/share/applications/timechamp.desktop fi (crontab -l 2>/dev/null | grep -Fq '@reboot rm -f $HOME/.local/share/timechamp/exit.txt') || (crontab -l 2>/dev/null; echo "@reboot rm -f \$HOME/.local/share/timechamp/exit.txt") | crontab - currentutcdatetime=$(date --utc +'%Y-%m-%dT%H:%M:%SZ') printf "Current UTC dateTime: $currentutcdatetime\n" if [ -e ~/.local/share/timechamp/Pulse.txt ] then recentpulsedatetime=$(<~/.local/share/timechamp/Pulse.txt) printf "Recent pulse dateTime: $recentpulsedatetime\n" else recentpulsedatetime=$(date --utc +'%Y-%m-%dT%H:%M:%SZ') fi CURRENT=$(date +%s -d $currentutcdatetime) RECENT=$(date +%s -d $recentpulsedatetime) MINUTES=$(( ($CURRENT - $RECENT) / 60 )) if [[ -e ~/.local/share/timechamp/exit.txt && $(<~/.local/share/timechamp/exit.txt) == "true" ]] then RESTART="NO" else RESTART="YES" fi process=time-champ.-tra process1=TimeChamp.Track specified_file="TimeChamp.AppImage" most_recent_directory="" most_recent_timestamp=0 # Loop through directories in the search path for dir in ~/.local/share/timechamp/*/; do printf "DirectoryPath: $dir\n" # Check if the directory contains the specified file if [ -f "$dir/$specified_file" ]; then # Get the creation time of the directory timestamp=$(stat -c %Y "$dir") # Check if this directory is more recent than the previous one if [ "$timestamp" -gt "$most_recent_timestamp" ]; then most_recent_directory="$dir" most_recent_timestamp="$timestamp" fi else if [ -d "$dir" ]; then printf "Deleting directory: $dir\n" rm -rf "$dir" fi fi done if [ -n "$most_recent_directory" ] then LATESTAUTOUPDATE="YES" printf "$most_recent_directory is NOT NULL\n" else LATESTAUTOUPDATE="NO" printf "$most_recent_directory is NULL\n" fi # Killing Application based on the killme txt file if [[ -e ~/.local/share/timechamp/killme.txt && $(<~/.local/share/timechamp/killme.txt) == "true" ]] then KILL="YES" else KILL="NO" fi if ps aux | pgrep -x "$process" && ps aux | pgrep -x "$process1" > /dev/null then if [ "$MINUTES" -ge 4 ] || [ "$KILL" == "YES" ]; then killall -9 time-champ.-tracker.-electron killall -9 TimeChamp.Tracker.Electron killall -9 TimeChamp.AppImage killall -9 "time-champ.-tracker.-electron --no-sandbox" rm -r ~/.cache/tracker rm -r ~/.local/share/tracker rm -r ~/.cache/tracker3 rm -r ~/.local/share/tracker3 rm -r ~/.config/time-champ.-tracker.-electron printf "making process restart due to deadlock\n" if [ "$LATESTAUTOUPDATE" == "YES" ] then chmod +x "${most_recent_directory}TimeChamp.AppImage" if [ -d "${most_recent_directory}executables" ]; then printf "Folder exists: ${most_recent_directory}executables\n" else printf "Folder does not exist: ${most_recent_directory}executables\n" "${most_recent_directory}TimeChamp.AppImage" --appimage-extract mv squashfs-root "${most_recent_directory}executables" fi nohup "${most_recent_directory}executables/AppRun" & else chmod +x ~/.local/share/timechamp/TimeChamp.AppImage nohup ~/.local/share/timechamp/TimeChamp.AppImage --no-sandbox & fi else printf "Process running\n" exit fi else if [ "$RESTART" == "NO" ] then killall -9 time-champ.-tracker.-electron killall -9 TimeChamp.Tracker.Electron killall -9 TimeChamp.AppImage killall -9 "time-champ.-tracker.-electron --no-sandbox" rm -r ~/.cache/tracker rm -r ~/.local/share/tracker rm -r ~/.cache/tracker3 rm -r ~/.local/share/tracker3 rm -r ~/.config/time-champ.-tracker.-electron printf "User exited manually\n" else killall -9 time-champ.-tracker.-electron killall -9 TimeChamp.Tracker.Electron killall -9 TimeChamp.AppImage killall -9 "time-champ.-tracker.-electron --no-sandbox" rm -r ~/.cache/tracker rm -r ~/.local/share/tracker rm -r ~/.cache/tracker3 rm -r ~/.local/share/tracker3 rm -r ~/.config/time-champ.-tracker.-electron printf "making process start\n" if [ "$LATESTAUTOUPDATE" == "YES" ] then chmod +x "${most_recent_directory}TimeChamp.AppImage" if [ -d "${most_recent_directory}executables" ]; then printf "Folder exists: ${most_recent_directory}executables\n" else printf "Folder does not exist: ${most_recent_directory}executables\n" "${most_recent_directory}TimeChamp.AppImage" --appimage-extract mv squashfs-root "${most_recent_directory}executables" fi nohup "${most_recent_directory}executables/AppRun" & else chmod +x ~/.local/share/timechamp/TimeChamp.AppImage nohup ~/.local/share/timechamp/TimeChamp.AppImage --no-sandbox & fi fi fi exit