{pkgs, ...}:
with pkgs; {
  home.packages = [
    (writeScriptBin "screenshot" ''
      notify_user() {
        ${pulseaudio}/bin/paplay ${sound-theme-freedesktop}/share/sounds/freedesktop/stereo/screen-capture.oga 2>/dev/null &
        ${libnotify}/bin/notify-send \
          -a Clipboard \
          -i ${builtins.toString ./clipboard.svg} \
          -u low \
          -r 699 "Clipboard" "Screenshot saved on clipboard"
      }

      screen() {
        ${maim}/bin/maim -u -f png | ${pkgs.xclip}/bin/xclip -selection clipboard -t image/png
        notify_user
      }

      window() {
        window_geometry=$(${xdotool}/bin/xdotool getwindowgeometry $(${xdotool}/bin/xdotool getactivewindow) | tail -n +2 | awk '{print $2}' | tac)
        last_line=$(echo "$window_geometry" | tail -n 1)
        modified_last_line=$(echo "$last_line" | awk 'BEGIN {FS=OFS=","} {for(i=1; i<=NF; i++) $i+=2; print}' | sed -E "s/([+-]?[0-9]+),([+-]?[0-9]+)/+\1+\2/g; s/\+\-/\-/g")
        final_string=$(echo "$window_geometry" | sed '$s/.*/'"$modified_last_line"'/' | paste -sd "")

        ${maim}/bin/maim -u -f png -g "$final_string" | ${pkgs.xclip}/bin/xclip -selection clipboard -t image/png
        notify_user
      }

      area() {
        ${maim}/bin/maim -u -f png -s -b 2 -c 0.35,0.55,0.85,0.25 -l | ${pkgs.xclip}/bin/xclip -selection clipboard -t image/png
        notify_user
      }

      menu() {
        screen="󰍹"
        area="󰒉"
        window=""

        chosen="$(printf "%s\n%s\n%s\n%s\n" "$screen" "$area" "$window" | rofi -theme ${builtins.toString ./screenshot.rasi} -p 'Take Screenshot' -dmenu -selected-row 0 -theme-str 'listview {lines: 3;}')"

        case $chosen in
        "$screen")
          screen
          ;;
        "$area")
          area
          ;;
        "$window")
          window
          ;;
        esac
      }

      docs() {
        echo "
      Usage:	screenshot [Options]
          --help	-	Prints this message
      Options:
          --screen	-	Take screenshot of the screen
          --window	-	Take screenshot of the focused window
          --area	-	Take screenshot of the selected area
          --menu	-	Opens a gui selector
        "
      }

      case $1 in
      --screen)
        screen
        ;;
      --window)
        window
        ;;
      --area)
        area
        ;;
      --menu)
        menu
        ;;
      --help | *)
        docs
        ;;
      esac
    '')
  ];
}
