#!usr/bin/perl
use v5.12;
use warnings;
use FindBin;
use File::Slurp;

my $datei = 'notizblock.txt';
my %kommando = (bewege => 'm', loesche => 'd');
my @notizen;
chdir $FindBin::Bin;

if (-e $datei) {
      @notizen = read_file( $datei );
      print "[$_] ", $notizen[ $_ ] for 0 .. $#notizen ;
}
print "Neue Notiz (Enter wenn keine, ".
      "$kommando{'loesche'} löscht, $kommando{'bewege'} bewegt): ";
my $notiz = <STDIN>;

given ( substr($notiz, 0, 1) ) {
    when ("\n"){ }
    when (" ") { append_file($datei, $notiz) }
    when ($kommando{'loesche'}) {
        continue if length($notiz) == length($kommando{'loesche'}) + 1;
        my $nr = 0 + int substr($notiz, length($kommando{'loesche'}));
        splice(@notizen, $nr, 1) if $nr >= 0 and $nr <= $#notizen;
        write_file($datei, @notizen);
    }
    when ($kommando{'bewege'}) {
        continue if length($notiz) == length($kommando{'bewege'}) + 1;
        my($von, $zu) = split ':', substr($notiz, length($kommando{'bewege'}));
        $von = 0 + int $von;
        $zu  = 0 + int $zu;
        continue if $von < 0 or $von > $#notizen;
        continue if $zu  < 0 or $zu  > $#notizen;
        splice(@notizen, $zu, 0, splice(@notizen, $von, 1));
        write_file($datei, @notizen);
    }
    default {
        say "Hilfe";
    }
}

