#!/bin/bash
######################################################
#                 PS Manager                         #
# A yad gui program that can stop or start           #
# your process consuming hardware resources.         #
######################################################
# Copyright: Community Development PCLinuxOS 2019    #
# Free software (GPLv3) without any warranty.        #
######################################################

ICON="/usr/share/psmanager/icons/system.png"
ICON2="/usr/share/psmanager/icons/error.svg"
ICON3="/usr/share/psmanager/icons/cancel.svg"

function main ()
{
select=$(ps -u $USER | yad --list --title="PSManager" \
--column "Select a process to stop or start" 2>/dev/null \
--button=About!/usr/share/psmanager/icons/about.svg:3 \
--button=Start!/usr/share/psmanager/icons/edit-redo.png:2 \
--button=Stop!/usr/share/psmanager/icons/stop.png:0 \
--button=Cancel!/usr/share/psmanager/icons/cancel.svg:5 \
--text="List of all "$USER"'s processes" \
--width=370 --height=500 --window-icon=$ICON)

# Stop running processes
ans=`echo $?`

        if test "$ans" -eq "0" ;
        then

              select=`echo $select | cut -d" " -f1`
             kill -SIGSTOP $(ps -ef | grep "$select" | awk 'NR==1{print $0}' | tail -n 1 | awk '{print $2}') 2>/dev/null  
                
                        ans1=`echo $?`
                        if [ "$ans1" == "0" ] ;
                        then
                        select1=`echo $select | cut -d":" -f3  | cut -d" " -f2`
                        echo | `yad --info --image=/usr/share/psmanager/icons/process-stop.png \
--window-icon=/usr/share/psmanager/icons/stop.png \
--title="Process Stop" \
--text="\nProcess '$select1' with pid '$select' was stopped!" \
--width=350 --height=80 --button="Ok!/usr/share/psmanager/icons/ok.svg:1"` exit 
                        else
                        echo | `yad --error --image=$ICON2 --window-icon=$ICON2 --title="Error" \
--text="\nProcess '$select1' with pid '$select' not found!" \
--width=300 --height=80 --button="Ok!/usr/share/psmanager/icons/ok.svg:1"` exit
                        fi
        main
        fi

        if test "$ans" -eq "1" ;
        then
        echo | `yad --image=$ICON2 --window-icon=$ICON2 --title="Canceled" \
--text="\nNo running process selected!" --width=300 --height=80 --button="Ok!/usr/share/psmanager/icons/ok.svg:1"`
        exit 0
        fi

# Start running processes

        if test "$ans" -eq "2" ;
        then

                select=`echo $select | cut -d" " -f1`
                kill -SIGCONT $(ps -ef | grep "$select" | awk 'NR==1{print $0}' | tail -n 1 | awk '{print $2}') 2>/dev/null
             
                        ans1=`echo $?`
                        if [ "$ans1" == "0" ] ;
                        then
                        select1=`echo $select | cut -d":" -f3  | cut -d" " -f2`
                        echo | `yad --info --image=$ICON --window-icon=$ICON \
--title="Process Start" --text="\nProcess '$select1' with pid '$select' was started!" \
--width=350 --height=80 --button="Ok!/usr/share/psmanager/icons/ok.svg:1"`
                        else
                        echo | `yad --error --image=$ICON2 --window-icon=$ICON2 \
--title="Error" --text="\nProcess '$select1' with pid '$select' not found!" \
--width=300 --height=80 --button="Ok!/usr/share/psmanager/icons/ok.svg:1"`

                        fi
        main
        fi
        
        if test "$ans" -eq "1" ;
        then
        echo | `yad --image=$ICON2 --window-icon=$ICON2 --button="Ok!/usr/share/psmanager/icons/ok.svg:1" \
--title="Canceled" --text="\nNo running process selected!" --width=250 --height=80`

        main
fi
# About
if test "$ans" -eq "3" ;
        then
ICON="/usr/share/psmanager/icons/system.png"
`yad --width=500 --center --height=100 \
--text="\<b>                              PSManager</b>\
\n                                            2.1\n\nA yad gui, bash program that can stop or start \
your process consuming hardware resources.\n\n                <b><a href='http://www.pclinuxos.com/'>Copyright (C) PCLinuxOS 2019 </a></b>\nThis program is free software: you can redistribute it and/or 
modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version." \
--window-icon=$ICON \
--image=$ICON \
--title='PSManager' \
--button="Cancel!/usr/share/psmanager/icons/cancel.svg:1"`
exec "/usr/bin/psmanager"
       main
fi
   }
   main
       
exit 0


