#! /bin/bash

cd /home/$USER

function preview_full {
  scrot -d 3 -c /home/$USER/scr/"$NAME"."$EXT"

  gpicview /home/$USER/scr/"$NAME"."$EXT" & (sleep 1 && zenity --question --title "mintshot" --text "Keep this screenshot?")

  case "$?" in
  "0")
    killall gpicview
    
    dir_path=/home/$USER
    file_name="$NAME"
    file_ext="$EXT"
    
    save_shot
    ;;
  "1")
    killall gpicview
    ;;
  esac
}

function preview_custom {
  scrot -s -b /home/$USER/scr/"$NAME"."$EXT"

  gpicview /home/$USER/scr/"$NAME"."$EXT" & (sleep 1 && zenity --question --title "mintshot" --text "Keep this screenshot?")

  case "$?" in
  "0")
    killall gpicview
    
    dir_path=/home/$USER
    file_name="$NAME"
    file_ext="$EXT"
    
    save_shot
    ;;
  "1")
    killall gpicview
    ;;
  esac
}

function save_shot {
    if [ -e "$dir_path"/"$file_name"."$file_ext" ] ; then v=1
	while [ -e "$dir_path"/"$file_name"_"$v"."$file_ext" ] 
	do
	  echo $v
	  v=$(($v+1))
	done
	fi

    if [ -n "$v" ] ; then 
      mv /home/$USER/scr/"$NAME"."$EXT" "$dir_path"/"$file_name"_"$v"."$file_ext"	

      zenity --title "antiXscreenshot" --info --text "Your screenshot has been saved as\n$dir_path/$file_name\_$v.$file_ext"
    else 
      mv /home/$USER/scr/"$NAME"."$EXT" "$dir_path"/"$file_name"."$file_ext"

      zenity --title "antiXscreenshot" --info --text "Your screenshot has been saved as\n$dir_path/$file_name.$file_ext"
    fi
}

### Main ###

mkdir /home/$USER/scr 
NAME="screenshot"
EXT="jpg"

SHOT=$(zenity  --title "mintshot" --list  --text "What type of screenshot?" --radiolist  --column "Pick" --column "Area" TRUE "Full Screen" FALSE "Custom Area"); echo $ans

if [ "$SHOT" = "Full Screen" ]; then
  preview_full
fi

if [ "$SHOT" = "Custom Area" ]; then
  preview_custom
fi

rm -rf /home/$USER/scr
