#!/bin/sh

# Run this script inside a kali-linux-everything machine

# shellcheck disable=SC3043


DIR="$(realpath "$(dirname "$0")/..")"

DESTDIR="${DIR}/desktop-files"
APP_ICONS="${DIR}/menu-icons/scalable/apps"

rm -f "${DESTDIR}"/*
mkdir -p "${DESTDIR}"/

# Function to get the description of a tool based on man pages or apt's package
# 'Description' parameter
get_description() {
  local name="$1"
  local package="$2"
  {
    whatis -l "${name}" \
    | sed -E -e "s/${name} \([^\)]+\) *- //i" \
             -e "s/^./\u\0/g" \
    | grep -v 'unknown subject'
    apt-cache show "${package}" \
    | grep -o -P '(?<=Description: ).+'
  } 2>/dev/null | head -n 1
}

# Function to get the package that provides the given command name
get_package() {
  local tool="$1"
  local binary=
        binary=$(which "${tool}")
  if binary=$(which "${tool}"); then
    dpkg -S "$(realpath "${binary}")" \
    | cut -d : -f 1
  else
    echo "${tool}"
  fi
}

desktop_file=/dev/null

# Set IFS to newline to handle lines with spaces correctly
IFS='
'
# Read each line from the file 'desktop-files-data', skipping comments
# shellcheck disable=SC2013
for line in $(grep -v '^#' "${DIR}/desktop-files-data"); do
  echo "${line}"

  # Append custom parameters and continue
  if parameter="$(echo "${line}" | grep -o -P '(?<=^  )[^ ].+')"; then
    sed -i "/^$(echo "${parameter}" | cut -d = -f 1)=/d" "${desktop_file}"
    echo "${parameter}" >> "${desktop_file}"
    # Fix 'sed -i' changing permissions in mounted drives
    chmod -x "${desktop_file}"
    continue
  fi

  parsed_data="$(echo "${line}" | sed -E -e 's/ : |->/|/' \
                                         -e 's/ -> /|/' \
                                         -e 's/ \| /||/' \
                                         -e 's/$/|/')"

  tool_exec="$(echo "${parsed_data}" \
               | cut -d '|' -f 1 \
               | sed -E 's|<([^>]+)>|/usr/share/kali-menu/helper-scripts/\1.sh|')" # Use helper-script if needed
  tool_package="$(echo "${parsed_data}" | cut -d '|' -f 2)"
  tool_name="$(echo "${parsed_data}" \
               | cut -d '|' -f 1 \
               | sed -E 's/^(sudo|pkexec) //' \
               | tr -d '<>' \
               | cut -d ' ' -f 1)"
  [ -z "${tool_package}" ] \
    && tool_package="$(get_package "${tool_name}")"
  tool_comment="$(get_description "${tool_name}" "${tool_package}")"
  tool_terminal=true
  tool_icon="kali-${tool_name}"

  # Handle custom file name
  desktop_file="$(echo "${parsed_data}" | cut -d '|' -f 3)"
  [ -z "${desktop_file}" ] \
    && desktop_file="kali-${tool_name}"
  desktop_file="${DESTDIR}/${desktop_file}.desktop"

  # Check if the tool has a GUI
  if echo "${tool_exec}" | grep -q -F ' [GUI]'; then
    tool_terminal=false
    tool_exec="$(echo "${tool_exec}" \
                 | sed -E 's/ \[GUI\]//')"
  else
    tool_exec="/usr/share/kali-menu/exec-in-shell \"${tool_exec}\""
  fi

  # Check if icon exists
  [ -f "${APP_ICONS}/${tool_icon}.svg" ] \
    || tool_icon=kali-menu

  cat > "${desktop_file}" <<-EOF
	[Desktop Entry]
	Name=${tool_name}
	Comment=${tool_comment}
	Exec=${tool_exec}
	Icon=${tool_icon}
	StartupNotify=false
	Terminal=${tool_terminal}
	Type=Application
	Categories=
	X-Kali-Package=${tool_package}
	EOF
done


# Generate menu layout and set 'Categories' parameter
"${DIR}"/bin/generate-menu-layout

echo ''
echo '[!] Tools without category:'
grep -rl '^Categories=$' desktop-files
