#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

#set -x
set -e
set -o pipefail

true "$0: START"

scriptname="$(basename -- "$BASH_SOURCE")"

error_handler() {
   local exit_code="$?"

   local my_pstree
   my_pstree="$(pstree -p $$)" || true
   local ps_p_parentpid

   if [ ! "$parentpid" = "" ]; then
      if [ ! "$parentpid" = "0000000000" ]; then
         ps_p_parentpid="$(ps -p "$parentpid")" || true
      fi
   fi

   local msg="\
###############################################################################
## $scriptname script bug.
## No panic. Nothing is broken. Just some rare condition has been hit.
## Try again later. There is likely a solution for this problem.
## Please see Whonix News, Whonix Blog and Whonix User Help Forum.
## Please report this bug!
##
## scriptname: '$scriptname'
## identifier: '$identifier'
## my_command_line_parameters: '$my_command_line_parameters'
## write_to_file: '$write_to_file'
## parentpid: '$parentpid'
## ps_p_parentpid: '$ps_p_parentpid'
## my_pstree: '$my_pstree'
## BASH_COMMAND: '$BASH_COMMAND'
## exit_code: '$exit_code'
###############################################################################\
"
   printf '%s\n' "$msg" >&2

   local home_var
   home_var=~/

   if ! test -w ~/ ; then
      printf '%s\n' "$scriptname: skipping log to ~/.msgcollector/msgdispatcher-error.log since not writeable home_var: $home_var" >&2
      exit 1
   fi

   if [ ! -d ~/".msgcollector" ]; then
      mkdir --parents ~/".msgcollector"
   fi
   ## XXX: set executable bit on the folder. This has nothing to do with executable binaries. See:
   ## https://superuser.com/questions/168578/why-must-a-folder-be-executable
   ## This is required because otherwise the following "touch" command would fail.
   ## Unclear why this is required. Maybe an issue in /usr/lib/tmpfiles.d/systemcheck.conf config file
   ## This workaround is good enough.
   chmod +x ~/".msgcollector" || true

   if ! touch -- ~/".msgcollector/msgdispatcher-error.log" ; then
      printf '%s\n' "$scriptname: skipping log to $home_var/.msgcollector/msgdispatcher-error.log since failed to 'touch' file." >&2
      exit 1
   fi

   if [ ! -w  ~/".msgcollector/msgdispatcher-error.log" ]; then
      printf '%s\n' "$scriptname: skipping log to ~/.msgcollector/msgdispatcher-error.log since not writeable file: $home_var/.msgcollector" >&2
      exit 1
   fi

   printf '%s\n' "\
scriptname: $scriptname
identifier: $identifier
my_command_line_parameters: $my_command_line_parameters
parentpid: $parentpid
ps_p_parentpid: $ps_p_parentpid
my_pstree: $my_pstree
BASH_COMMAND: $BASH_COMMAND
exit_code: $exit_code
" | tee -a -- ~/".msgcollector/msgdispatcher-error.log" >/dev/null

   true "INFO: end of $0 error_handler, ok."

   exit 1
}

trap "error_handler" ERR

my_command_line_parameters="$@"

parse_cmd_options() {
   trap "error_handler" ERR

   ## Thanks to:
   ## http://mywiki.wooledge.org/BashFAQ/035

   true "${cyan}INFO:${cyan} parse_cmd_options msgcollector"

   while true; do
       case $1 in
           --verbose)
               set -x
               verbose="1"
               shift
               ;;
           --debug)
               debug="1"
               shift
               ;;
           --identifier)
               identifier="$2"
               shift 2
               ;;
           --icon)
               icon="$2"
               if [ "$icon" = "" ]; then
                  printf '%s\n' "$BASH_SOURCE ERROR: variable icon is empty."
                  exit 1
               fi
               shift 2
               ;;
           --parentpid)
               parentpid="$2"
               shift 2
               ;;
           --typecli)
               cli="1"
               typecli="$2"
               shift 2
               if [ "$typecli" = "" ]; then
                  printf '%s\n' "$BASH_SOURCE ERROR: variable typecli is empty."
                  exit 1
               fi
               ;;
           --typex)
               typex="$2"
               shift 2
               if [ "$typex" = "" ]; then
                  printf '%s\n' "$BASH_SOURCE ERROR: variable typex is empty."
                  exit 1
               fi
               ;;
           --typexstatus)
               ## for --status
               shift
               typex_chosen="1"
               ;;
           --typeclistatus)
               ## for --status
               shift
               typecli_chosen="1"
               ;;
           --message)
               message="$2"
               shift 2
               if [ "$message" = "" ]; then
                  printf '%s\n' "$BASH_SOURCE ERROR: variable message is empty."
                  exit 1
               fi
               ;;
           --titlecli)
               cli="1"
               titlecli="$2"
               shift 2
               ;;
           --titlex)
               titlex="$2"
               shift 2
               ;;
           --progressbartitlex)
               progressbartitlex="$2"
               shift 2
               ;;
           --passivepopupqueuextitle)
               passivepopupqueuextitle="$2"
               shift 2
               ;;
           --messagex)
               messagex="1"
               shift
               ;;
           --messagecli)
               cli="1"
               messagecli="1"
               shift
               ;;
           --waitmessagecli)
               waitmessagecli="1"
               shift
               ;;
           --passivepopupqueuex)
               passivepopupqueuex="1"
               shift
               ;;
           --progressx)
               progressx="$2"
               shift 2
               if [ "$progressx" = "" ]; then
                  printf '%s\n' "$BASH_SOURCE ERROR: variable progressx is empty."
                  exit 1
               fi
               ;;
           --progressbarx)
               progressbarx="1"
               shift
               ;;
           --progressbarxrunning)
               progressbarxrunning="1"
               shift
               ;;
           --progressbarxprogresstxtexisting)
               progressbarxprogresstxtexisting="1"
               shift
               ;;
           --animate)
               animate="1"
               shift
               ;;
           --nonewlinex)
               nonewlinex="1"
               shift
               ;;
           --newlinecli)
               newlinecli="1"
               shift
               ;;
           --forceactive)
               forceactive="1"
               shift
               ;;
           --parenttty)
               parenttty="$2"
               shift 2
               ;;
           --done)
               done="1"
               shift
               ;;
           --forget)
               forget="1"
               shift
               ;;
           --forgetwaitcli)
               forgetwaitcli="1"
               shift
               ;;
           --progressbaridx)
               progressbaridx="$2"
               shift 2
               ;;
           --status)
               status="1"
               shift
               ;;
           --onlyecho)
               onlyecho="1"
               shift
               ;;
           --)
               shift
               break
               ;;
           -*)
               printf '%s\n' "$scriptname: unknown option: $1" >&2
               exit 1
               ;;
           *)
               break
               ;;
       esac
   done

   ## If there are input files (for example) that follow the options, they
   ## will remain in the "$@" positional parameters.

   if [[ ! "$@" = "" ]]; then
      printf '%s\n' "$scriptname: unknown option: $1" >&2
      exit 3
   fi
}

preparation() {
   trap "error_handler" ERR
}

colors() {
   trap "error_handler" ERR

   if [ "$TERM" = "" ]; then
      return 0
   fi

## The checks [[ -t 2 ]] and [[ $TERM != *-m ]] have been deactivated, because
## at the time msgcollector gets its first messages, there might be no
## terminal. We need those colors anyway, because they get stored in the files.

## Thanks to:
## http://mywiki.wooledge.org/BashFAQ/037
## Variables for terminal requests.
#[[ -t 2 ]]
true && {
    alt=$(      tput smcup  || tput ti      ) # Start alt display
    ealt=$(     tput rmcup  || tput te      ) # End   alt display
    hide=$(     tput civis  || tput vi      ) # Hide cursor
    show=$(     tput cnorm  || tput ve      ) # Show cursor
    save=$(     tput sc                     ) # Save cursor
    load=$(     tput rc                     ) # Load cursor
    bold=$(     tput bold   || tput md      ) # Start bold
    stout=$(    tput smso   || tput so      ) # Start stand-out
    estout=$(   tput rmso   || tput se      ) # End stand-out
    under=$(    tput smul   || tput us      ) # Start underline
    eunder=$(   tput rmul   || tput ue      ) # End   underline
    reset=$(    tput sgr0   || tput me      ) # Reset cursor
    blink=$(    tput blink  || tput mb      ) # Start blinking
    italic=$(   tput sitm   || tput ZH      ) # Start italic
    eitalic=$(  tput ritm   || tput ZR      ) # End   italic
#[[ $TERM != *-m ]]
true && {
    red=$(      tput setaf 1|| tput AF 1    )
    green=$(    tput setaf 2|| tput AF 2    )
    yellow=$(   tput setaf 3|| tput AF 3    )
    blue=$(     tput setaf 4|| tput AF 4    )
    magenta=$(  tput setaf 5|| tput AF 5    )
    cyan=$(     tput setaf 6|| tput AF 6    )
}
    white=$(    tput setaf 7|| tput AF 7    )
    default=$(  tput op                     )
    eed=$(      tput ed     || tput cd      )   # Erase to end of display
    eel=$(      tput el     || tput ce      )   # Erase to end of line
    ebl=$(      tput el1    || tput cb      )   # Erase to beginning of line
    ewl=$eel$ebl                                # Erase whole line
    draw=$(     tput -S <<< '   enacs
                                smacs
                                acsc
                                rmacs' || { \
                tput eA; tput as;
                tput ac; tput ae;         } )   # Drawing characters
    back=$'\b'
} 2>/dev/null ||:
}

pretty_type_cli() {
   trap "error_handler" ERR

   if [ "$1" = "info" ]; then
      p_type="${green}INFO${reset}"
   elif [ "$1" = "warning" ]; then
      p_type="${red}WARNING${reset}"
   elif [ "$1" = "error" ]; then
      p_type="${red}${bold}ERROR${reset}"
   else
      p_type="${red}???${reset}"
   fi
}

pretty_type_x() {
   trap "error_handler" ERR

   local msgINFO msgWARNING msgERROR
   msgINFO="<p><span style=color:#008000>INFO</span>:"
   msgWARNING="<p><span style=color:#c00000>WARNING</span>:"
   msgERROR="<p><span style=font-weight:600;color:#ff0000>ERROR</span>:"

   local first_three
   first_three="${message:0:3}"
   if [ "$first_three" = "<p>" ]; then
      local message_length remaining_chars message_without_p
      message_length="${#message}"
      remaining_chars="$(( $message_length -3 ))"
      message_without_p="${message:3:$remaining_chars}"
      if [ "$1" = "info" ]; then
         message="$msgINFO $message_without_p"
      elif [ "$1" = "warning" ]; then
         message="$msgWARNING $message_without_p"
      elif [ "$1" = "error" ]; then
         message="$msgERROR $message_without_p"
      fi
   fi
}

write_typecli_to_file() {
   # $1 - type
   # $2 - file
   if [ "$onlyecho" = "1" ]; then
      return 0
   fi
   printf '%s\n' "$1" > "$2"
}

collector() {
   trap "error_handler" ERR

   ## {{ --identifier
   if [ "${identifier}" = "" ]; then
      printf '%s\n' "$BASH_SOURCE ERROR: variable identifier is empty."
      exit 1
   fi
   ## }}

   ## {{ --icon
   if [ ! "$icon" = "" ]; then
      ## Debugging.
      #ls -la "${msgcollector_run_dir}"
      #test -r "${msgcollector_run_dir}"
      #test -w "${msgcollector_run_dir}"
      #test -r "${msgcollector_run_dir}/${identifier}_icon" || true "not readable"
      #test -w "${msgcollector_run_dir}/${identifier}_icon" || true "not writeable"
      #whoami
      ## NOTE: If not writeable, AppArmor issues?
      printf '%s\n' "$icon" > "${msgcollector_run_dir}/${identifier}_icon"
   fi
   ## }}

   ## {{ --parentpid
   if [ "$parentpid" = "" ]; then
      #printf '%s\n' "$BASH_SOURCE ERROR: variable parentpid is empty."
      #exit 1
      ## Small hack, so not all applications get killed when pressing the cancel button
      ## such as timesync.
      parentpid="0000000000"
   fi
   ## Not at this point to avoid useless files.
   #printf '%s\n' "$parentpid" > "${msgcollector_run_dir}/${identifier}_${progressbaridx}_parentpid"
   ## }}

   ## {{ --titlecli
   if [ ! -z "$titlecli" ]; then
      ## variable $titlecli exists
      ## TODO
      true "TODO: --titlecli not yet implemented in msgdispatcher"
      #printf '%s\n' "$titlecli" > "${msgcollector_run_dir}/${identifier}_titlecli"
   fi
   ## }}

   ## {{ --titlex
   if [ ! -z "$titlex" ]; then
      ## variable $titlex exists
      printf '%s\n' "$titlex" > "${msgcollector_run_dir}/${identifier}_titlex"
   fi
   ## }}

   ## {{ --passivepopupqueuextitle
   if [ ! -z "$passivepopupqueuextitle" ]; then
      ## variable $passivepopupqueuextitle exists
      printf '%s\n' "$passivepopupqueuextitle" > "${msgcollector_run_dir}/${identifier}_passivepopupqueuextitle"
   fi
   ## }}

   ## {{ --progressbartitlex
   if [ ! -z "$progressbartitlex" ]; then
      ## variable $progressbartitlex exists
      printf '%s\n' "$progressbartitlex" > "${msgcollector_run_dir}/${identifier}_${progressbaridx}_progressbartitlex"
   fi
   ## }}

   ## {{ --typecli
   if [ "$messagecli" = "1" ]; then
      if [ -f "${msgcollector_run_dir}/${identifier}_typecli" ]; then
         old_typecli="$(cat -- "${msgcollector_run_dir}/${identifier}_typecli")"
      fi

      if [ "$old_typecli" = "info" ]; then
         if [ "$typecli" = "error" ]; then
            ## Upgrade from info to error.
            write_typecli_to_file "error" "${msgcollector_run_dir}/${identifier}_typecli"
         elif [ "$typecli" = "warning" ]; then
            ## Upgrade from info to warning.
            write_typecli_to_file "warning" "${msgcollector_run_dir}/${identifier}_typecli"
         fi
      elif [ "$old_typecli" = "warning" ]; then
         if [ "$typecli" = "error" ]; then
            ## Upgrade from warning to error.
            write_typecli_to_file "error" "${msgcollector_run_dir}/${identifier}_typecli"
         fi
      elif [ "$old_typecli" = "error" ]; then
         true
      else
         if [ ! "$typecli" = "" ]; then
            write_typecli_to_file "$typecli" "${msgcollector_run_dir}/${identifier}_typecli"
         fi
      fi
   fi
   ## }}

   ## {{ --typex
   if [ "$messagex" = "1" ]; then
      if [ -f "${msgcollector_run_dir}/${identifier}_typex" ]; then
         old_typex="$(cat -- "${msgcollector_run_dir}/${identifier}_typex")"
      fi

      if [ "$old_typex" = "info" ]; then
         if [ "$typex" = "error" ]; then
            ## Upgrade from info to error.
            printf '%s\n' "error" > "${msgcollector_run_dir}/${identifier}_typex"
         elif [ "$typex" = "warning" ]; then
            ## Upgrade from info to warning.
            printf '%s\n' "warning" > "${msgcollector_run_dir}/${identifier}_typex"
         fi
      elif [ "$old_typex" = "warning" ]; then
         if [ "$typex" = "error" ]; then
            ## Upgrade from warning to error.
            printf '%s\n' "error" > "${msgcollector_run_dir}/${identifier}_typex"
         fi
      elif [ "$old_typex" = "error" ]; then
         true
      else
         printf '%s\n' "$typex" > "${msgcollector_run_dir}/${identifier}_typex"
      fi
   fi
   ## }}

   ## {{ --messagex
   if [ "$messagex" = "1" ]; then
      pretty_type_x "$typex"
      write_to_file="${msgcollector_run_dir}/${identifier}_messagex"
      append_to_file
   fi
   ## }}

   ## {{ --messagecli
   if [ "$messagecli" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_messagecli"
      if [ ! -z "$message" ]; then
         ## variable $message exists
         ## Cannot use 'sanitize-string' as it would remove terminal colors,
         ## such as for login security check table.
         ## Unfortunately, the application using 'msgcollector' needs provide sanitized strings.
         #message="$(sanitize-string -- nolimit "$message")"
         ## Instead, "only" use 'strip-markup'.
         message="$(strip-markup "$message")"
         pretty_type_cli "$typecli"
         message="[$p_type] [$identifier] $message"
         printf '%s\n' "$message"
      fi
      append_to_file
   fi
   ## }}

   ## {{ --waitmessagecli
   if [ "$waitmessagecli" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_waitmessagecli"
      if [ ! -z "$message" ]; then
         ## variable $message exists
         message="$(sanitize-string -- nolimit "$message")"
         pretty_type_cli "$typecli"
         message="[$p_type] [$identifier] $message"
         printf '%s\n' "$message"
      fi
      append_to_file
   fi
   ## }}

   ## {{ --passivepopupqueuex
   if [ "$passivepopupqueuex" = "1" ]; then
      if [ "$message" = "" ]; then
         message="message was empty."
      fi
      message="$(sanitize-string -- nolimit "$message")"
      if [ "$message" = "" ]; then
         message="message was empty after sanitize-string."
      fi
      write_to_file="${msgcollector_run_dir}/${identifier}_passivepopupqueuex"
      append_to_file
   fi
   ## }}

   ## {{ --animate
   if [ "$animate" = "1" ]; then
      touch -- "${msgcollector_run_dir}/${identifier}_${progressbaridx}_progressbarx_animate"
   fi
   ## }}

   ## {{ --progressx
   if [ ! "$progressx" = "" ]; then
      if [ "${progressbaridx}" = "" ]; then
         error "Variable progressbaridx is empty."
         exit 4
      fi

      ## Debugging.
      #caller="$(ps -p $PPID)" || true
      #printf '%s\n' "msgcollector: progressx: $progressx | caller: $PPID | $caller" >> /home/user/progresslog

      if [ "$verbose" = "1" ]; then
         bash -x /usr/libexec/msgcollector/msgprogress --identifier "$identifier" --progressbaridx "${progressbaridx}" --progress "$progressx"
      else
         ## & disown to prevent long waiting.
         /usr/libexec/msgcollector/msgprogress --identifier "$identifier" --progressbaridx "${progressbaridx}" --progress "$progressx" & disown
      fi
   fi
   ## }}

   ## {{ --forceactive
   if [ "$forceactive" = "1" ]; then
      touch -- "${msgcollector_run_dir}/${identifier}_forceactive"
   fi
   ## }}

   ## {{ --parenttty
   if [ ! -z "$parenttty" ]; then
      printf '%s\n' "$parenttty" > "${msgcollector_run_dir}/${identifier}_parenttty"
   fi
   ## }}

   ## {{ --progressbarx
   if [ "$progressbarx" = "1" ]; then
      if [ "${progressbaridx}" = "" ]; then
         error "Variable progressbaridx is empty."
         exit 4
      fi
      printf '%s\n' "$parentpid" > "${msgcollector_run_dir}/${identifier}_${progressbaridx}_parentpid"
      message="$(sanitize-string -- nolimit "$message")"
      write_to_file="${msgcollector_run_dir}/${identifier}_${progressbaridx}_progressbarx"
      append_to_file

      ## Cannot run from msgdispatcher due to user mismatch permission issues.
      /usr/libexec/msgcollector/msgprogressbar --identifier "${identifier}" --progressbaridx "${progressbaridx}" --progressbartitlex "$progressbartitlex" --message "$message" $animate & disown
   fi
   ## }}
}

append_to_file() {
   trap "error_handler" ERR

   if [ "$onlyecho" = "1" ]; then
      true "onlyecho"
      return 0
   fi

   if [ ! -z "$message" ]; then
      ## variable $message exists
      if [ "$newlinecli" = "1" ]; then
         printf '%s\n' "
$message" >> "$write_to_file"
      elif [ "$messagex" = 1 ]; then
         if [ "$nonewlinex" = "1" ]; then
            printf '%s\n' "$message" >> "$write_to_file"
         else
            printf '%s\n' "$message
" >> "$write_to_file"
         fi
      else
         printf '%s\n' "$message" >> "$write_to_file"
      fi
   fi

   ## {{ --done
   if [ "$done" = "1" ]; then
      touch -- "${write_to_file}_done"
   fi
   ## }}
}

return_status() {
   trap "error_handler" ERR

   ## {{ --identifier
   if [ "${identifier}" = "" ]; then
      printf '%s\n' "$BASH_SOURCE ERROR: variable identifier is empty."
      exit 1
   fi
   ## }}

   ## {{ --messagex
   if [ "$messagex" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_messagex"
      return_status_file
   fi
   ## }}

   ## {{ --messagecli
   if [ "$messagecli" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_messagecli"
      return_status_file
   fi
   ## }}

   ## {{ --waitmessagecli
   if [ "$waitmessagecli" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_waitmessagecli"
      return_status_file
   fi
   ## }}

   ## {{ --passivepopupqueuex
   if [ "$passivepopupqueuex" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_passivepopupqueuex"
      return_status_file
   fi
   ## }}

   ## {{ --progressbarx
   if [ "$progressbarx" = "1" ]; then
      if [ "${progressbaridx}" = "" ]; then
         error "Variable progressbaridx is empty."
         exit 4
      fi
      write_to_file="${msgcollector_run_dir}/${identifier}_${progressbaridx}_progressbarx"
      return_status_file
   fi
   ## }}

   ## {{ --progressbarxrunning
   if [ "$progressbarxrunning" = "1" ]; then
      if [ -f "${msgcollector_run_dir}/${identifier}_${progressbaridx}_yadprogresspid" ]; then
         local pid
         ## || true to prevent race condition
         yad_progress_pid="$(cat -- "${msgcollector_run_dir}/${identifier}_${progressbaridx}_yadprogresspid")" || true
         ## Check if still running.
         local ps__p_exit_code
         ps__p_exit_code="0"
         ps -p "$yad_progress_pid" >/dev/null 2>/dev/null || { ps__p_exit_code="$?"; true; };
         if [ "$ps__p_exit_code" = "0" ]; then
            exit 0
         else
            exit 1
         fi
      else
         exit 1
      fi
   fi
   ## }}

   ## {{ --progressbarxprogresstxtexisting
   if [ "$progressbarxprogresstxtexisting" = "1" ]; then
      if [ -f "${msgcollector_run_dir}/${identifier}_${progressbaridx}_progresstxt" ]; then
         exit 0
      else
         exit 1
      fi
   fi
   ## }}

   ## {{ --typexstatus
   if [ "$typex_chosen" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_typex"
      if [ -f "$write_to_file" ]; then
         local TYPE
         TYPE="$(cat -- "$write_to_file")"
         printf '%s\n' "$TYPE"
      fi
      exit 0
   fi
   ## }}

   ## {{ --typeclistatus
   if [ "$typecli_chosen" = "1" ]; then
      write_to_file="${msgcollector_run_dir}/${identifier}_typecli"
      if [ -f "$write_to_file" ]; then
         local TYPE
         TYPE="$(cat -- "$write_to_file")"
         printf '%s\n' "$TYPE"
      fi
      exit 0
   fi
   ## }}

   true "Fallback exit code, none of the above matched."
   exit 3
}

return_status_file() {
   trap "error_handler" ERR

   if [ -f "$write_to_file" ]; then
      exit 0
   else
      exit 1
   fi
}

forget() {
   trap "error_handler" ERR

   ## & disown to prevent long waiting.
   ## Setting progress to 100, so any eventually still open progress bars get closed.
   ## Deactivated.
   ## Problematic if a new progress bar will be started quickly afterward.
   #/usr/libexec/msgcollector/msgprogress --identifier "$identifier" --progress "100" & disown

   local file_list=(
      "${msgcollector_run_dir}/${identifier}_icon"
      "${msgcollector_run_dir}/${identifier}_titlecli"
      "${msgcollector_run_dir}/${identifier}_titlex"
      "${msgcollector_run_dir}/${identifier}_passivepopupqueuextitle"
      "${msgcollector_run_dir}/${identifier}_${progressbaridx}_progressbartitlex"
      "${msgcollector_run_dir}/${identifier}_typecli"
      "${msgcollector_run_dir}/${identifier}_typex"
      "${msgcollector_run_dir}/${identifier}_progressbarx_done"
      "${msgcollector_run_dir}/${identifier}_progressbarx"
      "${msgcollector_run_dir}/${identifier}_passivepopupqueuex_done"
      "${msgcollector_run_dir}/${identifier}_passivepopupqueuex"
      "${msgcollector_run_dir}/${identifier}_messagex_done"
      "${msgcollector_run_dir}/${identifier}_messagex"
      "${msgcollector_run_dir}/${identifier}_waitmessagecli_done"
      "${msgcollector_run_dir}/${identifier}_waitmessagecli"
      "${msgcollector_run_dir}/${identifier}_messagecli_done"
      "${msgcollector_run_dir}/${identifier}_messagecli"
      "${msgcollector_run_dir}/${identifier}_forceactive"
      "${msgcollector_run_dir}/${identifier}_parenttty"
   )

   ## Better not deleting
   ## "${msgcollector_run_dir}/${identifier}_${progressbaridx}_parentpid"
   ## "${msgcollector_run_dir}/${identifier}_${progressbaridx}_fifo"
   ## "${msgcollector_run_dir}/${identifier}_${progressbaridx}_progresstxt"
   ## "${msgcollector_run_dir}/${identifier}_${progressbaridx}_yadprogresspid"
   ## because that could confuse /usr/libexec/msgcollector/msgprogress.

   local file_name
   for file_name in "${file_list[@]}"; do
      if [ -e "$file_name" ]; then
         safe-rm --force -- "$file_name"
      fi
   done
   unset file_name
}

forgetwaitcli() {
   trap "error_handler" ERR

   local file_list=(
      "${msgcollector_run_dir}/${identifier}_waitmessagecli_done"
      "${msgcollector_run_dir}/${identifier}_waitmessagecli"
   )

   local file_name
   for file_name in "${file_list[@]}"; do
      if [ -e "$file_name" ]; then
         safe-rm --force "$file_name"
      fi
   done
   unset local file_name
}

## Debugging.
#printf '%s\n' "$BASH_SOURCE $@
#" >> "/home/user/msgcollector"

parse_cmd_options "$@"

source /usr/libexec/msgcollector/msgcollector_shared
## sets: ${msgcollector_run_dir}
folder_init

if [ "$forget" = "1" ]; then
   forget
   exit 0
fi

if [ "$forgetwaitcli" = "1" ]; then
   forgetwaitcli
   exit 0
fi

preparation
colors

if [ "$status" = "1" ]; then
   return_status
else
   collector
fi

true "$0: END"
