#!/usr/bin/perl

# simple TODO-manager

use strict;
use warnings "all";
use 5.10.0;
use Getopt::Long;

sub readtodo();

my $file = "$ENV{HOME}/todo.txt";
my ($delete, $help, $edit);

Getopt::Long::GetOptions(
"del"    => \$delete,
"d"      => \$delete,
"delete" => \$delete,
"edit"   => \$edit,
"e"      => \$edit,
"help"   => \$help
);

if ($help) {
	say "Usage: $0 --delete NUM";
	say "       $0 --del NUM";
	say "       $0 --d NUM";
	say "       deletes string number NUM";
	say "";
	say "       $0 --edit";
	say "       $0 --e";
	say "       edit todo file with default editor";
	say "";
	say "       $0 some thing to-do";
	say "       adds memo";
	say "";
	say "N.B. because weirdnes of Getopt module, you ca use -- or - as option prefix";
	exit 0;
}

if ($delete) {
	die "NUM not set\n" if ($ARGV[0] !~ /^\d+$/); 
	my @TODO = readtodo();
	die "NUM too big\n" if ($ARGV[0] > @TODO);

	my $cnt = 0;
	my $str = '';
	open (TD, ">", $file) or die "Unable to open $file: $!\n";

	for (my $i = 0; $i < @TODO; $i++) {
		next if ($i == $ARGV[0]);
		next unless (defined($TODO[$i]));
		print    sprintf("%s. %s\n", $cnt, $TODO[$i]);
		print TD sprintf("%s. %s\n", $cnt, $TODO[$i]);
		$cnt++;
	}

	close TD;
} elsif ($edit) {
	my $editor;

	if (defined($ENV{editor})) { $editor = $ENV{editor}; }
	elsif (defined($ENV{EDITOR})) { $editor = $ENV{EDITOR}; }

# i hope you do not use debian and your $editor is unset :)
	unless (defined($editor)) {
		if (-f '/usr/bin/vim') {
			$editor = '/usr/bin/vim';    # the most orthodox editor :) my favourite
		} elsif (-f '/usr/bin/nvim') {
			$editor = '/usr/bin/nvim';
		} elsif (-f '/usr/bin/neovim') {
			$editor = '/usr/bin/neovim'; # duh, one more name for neovim
		} elsif (-f '/usr/bin/nano') {
			$editor = '/usr/bin/nano';   # seriously?
		} elsif (-f '/usr/bin/micro') {
			$editor = '/usr/bin/micro';  # oh, c'mon
		} elsif (-f '/usr/bin/emacs') {
			$editor = '/usr/bin/emacs';  # really?
		} elsif (-f '/usr/bin/vi') {
			$editor = '/usr/bin/vi';     # do you really use it?
		} elsif (-f '/usr/bin/motor') {
			$editor = '/usr/bin/motor';  # where did you found it?
		} elsif (-f '/usr/bin/mcedit') {
			$editor = '/usr/bin/mcedit'; # yup
		} elsif (-f '/usr/bin/pico') {
			$editor = '/usr/bin/pico';   # oh, yea?
		} elsif (-f '/usr/bin/zile') {
			$editor = '/usr/bin/zile';   # hmm
		} elsif (-f '/usr/bin/joe') {
			$editor = '/usr/bin/joe';    # hope you set it up correctly :)
		} elsif (-f '/usr/bin/mle') {
			$editor = '/usr/bin/mle';    # as my collegue says - fucking lua, he really don't like lua, beacuse he knows it
		} elsif (-f '/usr/bin/slap') {
			$editor = '/usr/bin/slap';   # editor in js? you must be kidding!
		} elsif (-f '/usr/bin/jed') {
			$editor = '/usr/bin/jed';    # yup
		} else {
			say "Either you have no acceptable editor in your system or something went wrong.";
# i couldn't find any suitable editor, you're fucking kidding? how d' you work?
			exit 1;
		}
	}

	exec ($editor, $file);
} else {
	my @TODO = readtodo();

	if (@ARGV > 0) {
		push @TODO, join(" ", @ARGV);
		open (TD, ">", $file) or die "Unable to open $file: $!\n";

		for (my $i = 0; $i < @TODO; $i++) {
			next unless (defined($TODO[$i]));
			print    sprintf("%s. %s\n", $i, $TODO[$i]);
			print TD sprintf("%s. %s\n", $i, $TODO[$i]);
		}

		close TD;
	} else {
		for (my $i = 0; $i < @TODO; $i++) {
			next unless (defined($TODO[$i]));
			print sprintf("%s. %s\n", $i, $TODO[$i]);
		}
	}
}

exit 0;

sub readtodo() {
	my @TODO;
	open (T, "<", $file) or return @TODO;
	@TODO = <T>;
	close T;

	@TODO = map {
		chomp;
		$_ =~ /\s*\d+\. (.+)/;
		$1;
	} @TODO;

	return @TODO;
}
