#!/bin/bash
PROCID=$1
LAUNCHER=""
if [[ $(readlink /proc/$PROCID/cwd) == /Applications/* ]]; then
	if [[ -f "/usr/share/applications/$(readlink /proc/$PROCID/cwd | sed "s+/Applications/.++g" | sed "s+/.*++g").napp.desktop" ]]; then
		LAUNCHER="/usr/share/applications/$(readlink /proc/$PROCID/cwd | sed "s+/Applications/.++g" | sed "s+/.*++g").napp.desktop"
	fi
else
	app_id=$PROCID
	executed=$(ps -p $app_id -o comm= | sed 's#/.*##' | sed 's/-$//')
	desktop_file_path=$(find /usr/share/applications -name "*.desktop" -type f -exec grep -H "^Exec=$executed" {} \; | awk -F: '{print $1}')
	if [ -z "$desktop_file_path" ]; then
	  ErrorText="Cannot pin the application launcher: No such file or directory..\n\nDon't forget, we're trying to fix errors and bugs like this and we can't give true time.\n\nTechnical Info: PROCID=$1"
	  if LANG="tr_TR.UTF-8"; then
	  	ErrorText="Uygulama başlatıcısı işaretlenemiyor: Böyle bir dosya ya da dizin yok.\n\nUnutmayın, gelecek yamalarda bu tarz hataları çözmek için çalışıyoruz ancak ne zaman çözüleceği konusunda kesin zaman veremeyiz.\n\nTeknik Bilgi: PROCID=$1"
	  fi
	  zenity --error --text="$ErrorText"
	  exit 1
	else
	  LAUNCHER=$desktop_file_path
	  LAUNCHER=$(echo $LAUNCHER | sed 's/ .*//')
	fi
fi
echo $LAUNCHER
echo $LAUNCHER >> ~/.config/NebiSoft/NebiDE.shell.data/widget/pinned-apps/apps.txt
awesome-client 'awesome.emit_signal("pinned_apps::update")'
