#!/usr/bin/env perl

use strict;
use warnings;
use File::HomeDir qw(home);
use constant {
	SAVE_PATH		=> 'pics/scrot/',
	SCREENSHOT_PATH	=> 'scrot',
	IMGVIEW_PATH	=> 'nsxiv',
	XCLIP_PATH		=> 'xclip',
	NOTIFY_PATH		=> 'notify-send'
};
use constant LIST => [
	'Select region',
	'Window',
	'Fullscreen',
	'Monitor 1',
	'Monitor 2'
	];

sub show_img
{
	my ($file) = @_;
	my $pid;

	$pid = fork();
	if (not $pid) {
		exec(IMGVIEW_PATH, '-b', $file);
	}
	return;
}

sub notify_img
{
	my ($file) = @_;

	system(
		NOTIFY_PATH,
		'-u', 'low',
		'-t', '2000',
		'scrot',
		' Screenshot captured to ' . "\n" .
		'<b>' . $file . '</b>'
		);
	return;
}

sub clip_img
{
	my ($file) = @_;

	chomp $file;
	system('echo -n ' . $file . ' | ' . XCLIP_PATH);
	return;
}

sub file_name
{
	my $file_name;

	$file_name = home() . '/' . SAVE_PATH . 'screen_';
	$file_name .= `date +%s`;
	chomp $file_name;
	$file_name .= '.png';
	return ($file_name);
}

sub action
{
	my ($var) = @_;
	my $pid;
	my $file_name;

	chomp $var;
	$file_name = file_name();
	if ($var eq ${+LIST}[0]) {
		system(
			SCREENSHOT_PATH,
			$file_name,
			'-q', '100',
			'-s',
			'-l', 'mode=edge,width=2,color=#990000'
			);
	}
	elsif ($var eq ${+LIST}[1]) {
		system(
			SCREENSHOT_PATH,
			$file_name,
			'-q', '100',
			'-u'
			);
	}
	elsif ($var eq ${+LIST}[2]) {
		system(
			SCREENSHOT_PATH,
			$file_name,
			'-q', '100'
			);
	}
	elsif ($var eq ${+LIST}[3]) {
		system(
			SCREENSHOT_PATH,
			$file_name,
			'-q', '100',
			'-a', '0,0,1920,1080'
			);
	}
	elsif ($var eq ${+LIST}[4]) {
		sleep(1);
		system(
			SCREENSHOT_PATH,
			$file_name,
			'-q', '100',
			'-a', '1920,0,2560,1440'
			);
	}
	else {
		return;
	}
	show_img($file_name);
	notify_img($file_name);
	clip_img($file_name);
	return;
}

sub main
{
	my $list;
	my $choice;

	for (@{+LIST}) {
		$list .= $_ . "\n";
	}
	$choice = `printf "%s" "$list" | dmenu -i -l 10 -m 0`;
	action($choice);
	return (0);
}

main();

__END__