#!/usr/bin/perl -w

# vv  : visual versioning
# zz  : generic shell clipboard
# ezz : clipboard editor
# tt  : local file+data clipboard
# del : (un)delete
#
# https://fex.belwue.de/fstools/vv.html
# https://fex.belwue.de/fstools/zz.html
# https://fex.belwue.de/fstools/tt.html
# https://fex.belwue.de/fstools/del.html
#
# by Ulli Horlacher <framstag@belwue.de>
#
# Perl Artistic Licence

# vv is a script to handle file versions:
# list, view, recover, diff, purge, migrate, save, delete
#
# vv is an extension to emacs idea of backup~ files.
#
# vv does version rotating, the newest version is always 01,
# the maximum numbers of versions are 99.
# vv rotation keeps a last version if it is not from today.
#
# Versions are stored in local subdirectory .versions/
#
# You can integrate vv into your editor, so file versions will be automatically
# saved!
#
# To integrate vv into vim, add to $HOME/.vimrc:
#
#   autocmd BufWritePre  * execute '! vv -qs ' . shellescape(@%)
#   autocmd BufWritePost * execute '! vv -qs ' . shellescape(@%)
#
# To integrate vv into emacs, add to $HOME/.emacs:
#
#   (add-hook 'before-save-hook (lambda () (shell-command (
#    concat "vv -qs " (shell-quote-argument (buffer-file-name))))))
#   (add-hook 'after-save-hook  (lambda () (shell-command (
#    concat "vv -qs " (shell-quote-argument (buffer-file-name))))))
#   (setq make-backup-files nil)
#
# To integrate vv into jed, see https://fex.belwue.de/sw/share/jedlib/vv.sl
#
# To use vv with ANY editor, first set:
#
#   export EDITOR=your_favourite_editor
#   alias ve='vv -e'
#
# and then edit your file with:
#
#   ve file
#
# $HOME/.vvrc is the config file for vv and will be created and filled with
# default values, if it does not exist.
#
# vv contains also del, zz, tt and ezz. To install them, call: perl vv -I

# These external programs are needed (GNU version): 
# rsync, cp, find, stat, rm

# 2013-04-15 initial version
# 2013-04-16 added options -m and -v
# 2013-04-18 added option -s
# 2013-04-22 realfilename() fixes symlink problematics
# 2013-04-22 use rsync instead of cp
# 2013-04-23 added option -I
# 2013-04-23 renamed from jedv to vv
# 2013-04-24 added options -e -0
# 2013-05-09 added option -R
# 2013-05-22 modified option -d to double argument
# 2013-05-22 added vvrc with $exclude and @diff
# 2013-07-05 fixed bug potential endless loop in rotate()
# 2014-04-16 added change-file-test for opt_s (needs .versions/$file)
# 2014-04-18 added option -b : save backup
# 2014-05-02 fixed bug wrong file ownership when using as root
# 2014-06-18 options -r -d -v : parameter is optional, default is 1
# 2014-06-18 fixed (stupid!) bug option -s does only sometimes saving
# 2014-06-20 options -d -v : argument is optional, default is last file
# 2014-07-22 fixed bug no (new) backup version 0 on option -r
# 2014-11-14 added option -D : delete last saved version
# 2014-11-14 make .versions/ mode 777 if parent directory is world writable
# 2015-03-19 allow write access by root even if file and .versions/ have different owners
# 2015-03-20 better error formatting for jed
# 2015-06-02 added option -r . : restore backup
# 2016-03-07 added options -M -L : 10 or 100 versions
# 2016-03-08 renamed option -I to -H
# 2016-05-02 call rsync -A to preserve ACLs
# 2016-06-07 option -v : use PAGER=cat if STDOUT is not a tty
# 2016-06-08 added zz, ezz and installer vvzz
# 2016-07-06 avoid empty $ZZ versioning
# 2016-09-12 added vv option -q : quiet mode
# 2016-11-29 show bytes up to 9999999, then M or G
# 2016-11-29 added vv option -P : delete all versions
# 2016-11-29 support for rsync without -A option
# 2016-11-30 fixed bug vv option -r breaks hard links
# 2016-11-30 added version number/range argument to vv option -D
# 2016-12-03 installer creates symbolic links, no more hard links
# 2016-12-03 handle backup as regular version: vv option -b acts like -s
# 2016-12-04 added del
# 2016-12-04 added vv option -I : installation
# 2016-12-04 added vv option -N : set number of versions
# 2016-12-04 removed vv options -M -L
# 2016-12-05 added vv option -U : update
# 2016-12-06 fixed bug cannot handle files with meta characters
# 2016-12-07 added vv option -V : show program version and history
# 2016-12-08 better (more secure) directory handling for del
# 2016-12-08 added del option -f : force deletion of directories
# 2016-12-08 added del option -D : real delete
# 2016-12-08 modified del option -p : purge named file(s)
# 2016-12-08 modified del option -P : purge all file(s)
# 2016-12-08 modified vv option -v : verbose
# 2016-12-14 recognize identical files on vv option -r
# 2016-12-15 modified option -V : show history only with -v
# 2017-02-08 added vv option -f : force = ignore exclude lists
# 2017-02-23 modified vv option -r : number is optional
# 2017-02-24 ask user for missing version number on vv -r or vv -d
# 2017-10-26 added option -D .
# 2018-06-08 added ezz options -1 ... -9
# 2018-06-10 reverse list if there are more than 20 versions and STDOUT is a tty
# 2018-08-28 fixed bug cannot edit new file with vv -e
# 2018-11-30 ezz STDIN is /dev/tty for $EDITOR
# 2018-12-17 del DIRECTORY does not remove existing .del/DIRECTORY, but appends
# 2018-12-18 added vv option -M : move
# 2018-12-19 vv: new .versions/ gets same permissions and ownership as .
# 2018-12-19 vv: new versions gets same permissions and ownership as file
# 2019-02-14 vv: added $excludedir to vvrc
# 2019-07-25 vv: added command execution
# 2019-08-01 vv: new .versions directory gets owner and group from parent directory
# 2019-08-01 vv: added optional configuration variable $versionsdir_perms
# 2019-08-04 vv: added .versions read/write check ==> better error message
# 2019-08-21 vv: added $versions to vvrc
# 2019-08-29 vv: added btrfs clone-copy support
# 2019-08-29 vv: speedup (less md5sum checks)
# 2019-12-09 vv: option -l shows modifiction date of last version
# 2020-07-25 vv: added option -c for comments
# 2020-07-26 vv: shorter usage
# 2020-07-27 vv: added option -u for unique versions
# 2020-07-27 vv: added optional command arguments to option -v view
# 2020-12-02 ezz: do not post-versioning $ZZ
# 2020-12-02 zz: added commands "+ NUMBER(s)" and "- NUMBER(s)"
# 2021-03-22 vv: save version only if file has been modified
# 2021-07-03 vv: call rsync -X to preserve extended attributes
# 2021-10-27 vv: inherite default numbers of versions to subdirs
# 2021-10-27 vv: option -N 0 . deletes default numbers of versions
# 2021-11-26 vv: fixed bug option -N for single files is ignored
# 2022-12-07 del: workaround for broken cp in MacOS
# 2023-11-27 vv: fixed bug no rotation of last comment
# 2023-11-27 zz: added option -d for diff
# 2024-03-25 vv: added '--filter=-x security.selinux' to rsync options
# 2024-10-07 del: fixed bug bad exit error
# 2025-01-16 vv: fixed bug rotating and moving comment
# 2025-01-16 vv: added parameter "today" for option -D
# 2025-01-18 vv: use rsync -V instead of -h to determine capabilties
# 2025-08-02 vv: added parameter "reduce" for option -D
# 2025-08-03 vv: added %versions to vvrc
# 2025-08-03 vv: fixed bug cannot detect all versions with -d
# 2025-08-22 vv: added aliases t for today and r for reduce
# 2025-08-23 vv: added @rsync support in vvrc
# 2025-08-24 vv: added $VVRC support
# 2025-09-17 added tt
# 2025-09-19 tt: use $TT/0 for exclusive writing
# 2025-09-20 tt: use tar --no-same-owner when extracting
# 2025-10-26 vv: list always time ascending
# 2025-11-05 vv: keep last version not from today

use Config;
use Getopt::Std;
use File::Basename;
use Term::ReadLine;
use Digest::MD5 'md5_hex';
use Cwd qw'abs_path getcwd';

$prg = basename($0);
$HOME = (getpwuid($<))[7];
$ZZ = $ENV{ZZ} || "$HOME/.zz";
$TT = $ENV{TT} || '/var/tmp/tt';

$version = 0;
unless ($version) {
  my @d = localtime(mtime($0));
  $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
}

$macos = $Config{osname} =~ /^darwin/i;

&install if $prg eq 'vvzz' or "@ARGV" eq '-I';
&update  if                   "@ARGV" eq '-U';
&zz      if $prg eq 'zz';
&ezz     if $prg eq 'ezz';
&del     if $prg eq 'del';
&tt      if $prg eq 'tt';

$prg eq 'vv' or die "$0: must be named vv\n";

if (abs_path($0) =~ m:/sw/:) {
  $update = '';
} else {
  $update = "\n".
    "  # self-update vv\n".
    "  vv -U\n";
}

# vv
$usage = <<EOD;

usage:

  # list available versions
  vv [-l] [FILE]

  # save new version (-v : verbose, -f : force)
  vv -s [-v] [-f] FILE [COMMENT]

  # recover version (-v : verbose)
  vv -r [-v] [VERSION_NUMBER] FILE [NEW_FILE]

  # show diff
  vv -d [VERSION_NUMBER[:VERSION_NUMBER]] [FILE]

  # delete versions (-v : verbose)
  vv -D [-v] [VERSION_NUMBER[-VERSION_NUMBER]] FILE
  vv -D [-v] today FILE  # delete all versions of today
  vv -D [-v] reduce FILE # delete all but last version of every day
  vv -D [-v] [-R] .      # delete all versions of all files (-R : recursive)

  # view version
  vv -v VERSION_NUMBER FILE [VIEWER_COMMAND]

  # comment version
  vv -c [VERSION_NUMBER] FILE [COMMENT]

  # edit file with \$EDITOR (with versioning)
  vv -e FILE

  # set or show number of versions (1-99, use file "." to set or show default)
  vv -N [NUMBER] FILE

  # migrate backup~ files to version files (-v : verbose, -R : recursive)
  vv -m [-v] [-R]

  # purge orphaned versions (-v : verbose)
  vv -p [-v]

  # unique versions = remove dupes (-v : verbose)
  vv -u [-v] FILE

  # move file with versions to new directory
  vv -M [-v] FILE DIRECTORY

  # execute shell command on file and versions
  vv SHELL_COMMAND FILE

  # show vv version (-v : history)
  vv -V [-v]
  $update

examples:
  vv -s project.pl                  # save new version
  vv -e project.pl                  # edit with versioning
  vv project.pl                     # list versions
  vv -d 2 project.pl                # show difference to version 2
  vv -D 2-8 project.pl              # delete versions 2 to 8
  vv -r project.pl                  # recover (with prompting)
  vv -r 2 project.pl project_2.pl   # recover version 2 as project_2.pl
  vv -N 20 project.pl               # max 20 versions for project.pl
  vv wc project.pl                  # execute wc on project.pl and its versions

see also:
  del -h
  zz -h
EOD

$fmt = "  %-46s # %s\n";
$short_usage = "usage:\n";
foreach (split "\n",$usage) {
  last if /examples/;
  next if /vv -V|history/;
  if (/^ *# (\w+.*(diff|number|all \S+))/ or /^ *# (\S+)/) {
    $comment = $1;
  } elsif (s/^\s+(vv .+)/$1/) {
    s/ \[-v\]//;
    s/#.*//;
    $short_usage .= sprintf($fmt,$_,$comment);
  }
}
$short_usage .= sprintf($fmt,"vv -H","help");

if ("@ARGV" !~ /^-/) {
  if (scalar(@ARGV) > 1) {
    if ("@ARGV " =~ /.~ /) {
      &vv_command;
      exit;
    } elsif (-f $ARGV[-1] and not -l $ARGV[-1]) {
      $ARGV[-1] .= '~';
      &vv_command;
      exit;
    } else {
      die $short_usage;
    }
  } else {
    $noopt = $opt_l = 1;
  }
}

$opt_h = $opt_p = $opt_m = $opt_s = $opt_0 = $opt_e = $opt_H = $opt_b = 0;
$opt_f = $opt_q = $opt_R = $opt_P = $opt_U = $opt_V = $opt_M = $opt_c = 0;
$opt_u = 0;
$opt_r = $opt_d = $opt_D = $opt_v = $opt_N = '';
${'opt_+'} = 0;
getopts('hHls0bepPfqmMRDUudcvV+rN') or die $short_usage;
$opt_s ||= $opt_b;

if ($opt_h) {
  print "vv: visual versioning (version $version)\n";
  if ($opt_v) {
    print $usage;
  } else {
    print $short_usage;
  }
  exit;
}

if ($opt_H) {
  open $0,$0 or die "vv: $0 - $!\n";
  while (<$0>) {
    $help = $_ if /^# vv is/;
    if ($help) {
      last if /^\s*$/ or /^#\s*\d\d\d\d-\d\d-\d\d/;
      print;
    }
  }
  print "\nvv: visual versioning ($version)\n";
  print $usage;
  exit;
}

if ($opt_V) {
  warn "$prg: version $version\n";
  if ($opt_v) {
    open $0,$0 or die "$prg: $0 - $!\n";
    while (<$0>) {
      print if s/^#\s*(\d\d\d\d-\d\d-\d\d)/$1/;
    }
  }
  exit;
}

if ($opt_U) { &update }

$vvrc = "$HOME/.vvrc";
unless (-e $vvrc) {
  open $vvrc,'>',$vvrc or die "vv: cannot write $vvrc - $!\n";
  print {$vvrc} q{
# default number of versions
$versions = 9;

# default number of versions for special files (regexp)
%versions = (
  '.*\.log' => 3,
  'fex.*' => 20,
);
    
# exclude files (regexp)
$exclude = q(
  ~$
  \.tmp$
  ^mutt-.+-\d+
  ^#.*#$
  ^\.#
);

# exclude directories (regexp)
$excludedir = q(
  /\.ssh/
  /\.versions/
  /crontab\.
);

# diff command
@diff = qw'diff -u';

# default rsync command (to ignore ACLs and extended attributes)
# @rsync = qw'rsync -a';
};
  close $vvrc;

# default: use permissions of parent directory for .versions
# $versionsdir_perms = 0700;
}

$versions = 9;
%versions = ();
$editor = $ENV{EDITOR}||'vi';

require $ENV{VVRC} if $ENV{VVRC};
require $vvrc;

$_ = `rsync -h`;
if (/rsync/) {
  s/\s+/ /g;
  unless (@rsync) {
    @rsync = qw'rsync -a';
    foreach my $file (@ARGV) {
      if (-f $file) {
        my $qfile = quote($file);
        # extended attributes support?
        if (`stat -f -c %T $qfile` =~ /^(ext|xfs|btrfs)/) {
          push @rsync,'-A' if /, ACLs/;
          push @rsync,'-X','--filter=-x security.selinux' if /, xattrs/;
        }
        last;
      }
    }
  }
} else {
  die "vv: no rsync found\n";
}

if ($< == 0 and scalar(@ARGV) and my @s = stat($ARGV[-1])) {
  local ($(,$>,$)) = (0,@s[4,5]);
}

if ($opt_c) {
  my $n = 1;
  if (scalar(@ARGV) > 1 and $ARGV[0] =~ /^\d\d?$/) {
    $n = shift @ARGV;
  }
  $file = shift @ARGV;
  $comment = "@ARGV";
  
  $n = "0$n" if $n =~ /^\d$/;

  vars($file);
  
  my $nvfile = "$vfile~$n~";
  if (-f $nvfile) {
    my $cfile = $nvfile.'COMMENT';
    if (not -t STDOUT) {
      open $cfile,$cfile or die "$prg: cannot read $cfile - $!\n";
      print while <$cfile>;
      close $cfile;
    } elsif (-t STDIN and not length($comment)) {
      $ENV{JEDINIT} = "SAVE_STATE=0";
      exec $editor,$cfile;
    } else {
      local $/;
      $comment = <STDIN> unless length($comment);
      $comment =~ s/\n*$/\n/;
      open $cfile,'>',$cfile or die "$prg: cannot write $cfile - $!\n";
      print {$cfile} $comment;
      close $cfile;
    }
  } else {
    die "$prg: $nvfile does not exist\n";
  }
  exit;
}

if ($opt_d) {
  &check_ARGV;
  if (@ARGV and $ARGV[0] =~ /^\d\d?(:\d\d?)?$/) {
    $opt_d = shift;
  } else {
    $opt_d = 1;
    if (@ARGV) {
      my $a = $ARGV[0];
      if (-f $a) {
        my $v = sdf(abs_path($a),'.versions');
        if (-f "$v~01~" and -f "$v~02~" and same_file($a,"$v~01~")) {
          $opt_d = 2;
        }
      }
      vars($a);
      &vv_list;
      $term = new Term::ReadLine $0;
      $term->ornaments(0) unless $ENV{PERL_RL};
      $opt_d = $term->readline('show diff to version number: ',$opt_d);
      exit if $opt_d =~ /^\s*$/;
    }
  }
  option_usage('-d') if scalar(@ARGV) != 1;
  $a = shift @ARGV;
}

elsif ($opt_D) {
  $opt_D = '-';
  if (@ARGV) {
    $opt_D = shift(@ARGV);
    $opt_D = 'today'  if $opt_D eq 't';
    $opt_D = 'reduce' if $opt_D eq 'r';
    $opt_D = 'all'    if $opt_D eq 'a';
    $opt_D =~ /^\d\d?(-(\d?\d?))?$|^(today|reduce|all)$/ or option_usage('-D');
  }
  option_usage('-D') if scalar(@ARGV) != 1;
  $a = shift @ARGV;
  if ($a eq '.') {
    if ($opt_R) {
      vsystem(qw'find . -name .versions -exec rm -rf {} +');
    } else {
      vsystem(qw'rm -rf .versions');
    }
    exit $?;
  }
}

elsif ($0 eq 've' or $opt_e) {
  $a = pop @ARGV or option_usage('-e');
  $opt_e = 1;
}

elsif ($opt_r) {
  my $nargs = scalar(@ARGV);
  option_usage('-r') if $nargs == 0 or $nargs > 3;
  if ($nargs == 1) {
    $opt_r = '-';
  } else {
    $opt_r = shift @ARGV;
    option_usage('-r') if $opt_r !~ /^\d\d?$/;
  }
  $a = shift @ARGV;
}

elsif (length($opt_N)) {
  $opt_N = shift @ARGV // '.';
  $a = shift @ARGV;
}

elsif ($opt_l or $opt_s or $opt_P or $opt_u or $opt_0) {
  $a = shift @ARGV;
}

elsif ($opt_M) {
  if (scalar(@ARGV) != 2) {
    die "usage: $prg file directory\n";
  }

  my ($file,$dir) = @ARGV;

  if (-l $file or not -f $file) {
    die "$prg: no such file '$file'\n";
  }
  unless (-d $dir) {
    die "$prg: no such directory '$dir'\n";
  }
  if (abs_path(dirname($file)) eq abs_path($dir)) {
    die "$prg: destination directory '$dir' is same as source\n";
  }
  unless (-w $dir) {
    die "$prg: no write access for directory '$dir'\n";
  }
  print "'$file' -> '$dir/'\n" if $opt_v;
  if (system('mv',$file,$dir)==0) {
    my $vfile = $file;
    $vfile =~ s:(.+)/(.+):$1/.versions/$2: or $vfile = ".versions/$vfile";
    my $vdir = "$dir/.versions";
    checkvdir($vdir,'w');
    if (mkdir $vdir and my @s = stat("$vdir/..")) {
      chmod $versionsdir_perms||$s[2],$vdir;
      chown $s[4],$s[5],$vdir;
    }
    if (my @versions = fmatch(quotemeta($vfile).'(~\d\d?~(COMMENT)?)?')) {
      system('mv',@versions,$vdir)
    }
  }
  exit $?;
}

elsif ($opt_m or $opt_p or length($opt_N)) { }

elsif ($opt_v) {
  if (@ARGV and $ARGV[0] =~ /^\d\d?$/) { $opt_v = shift }
  else                                 { $opt_v = 1 }
  $a = shift @ARGV;
}

$file = '*';
$vfile = '';
$vdir = '.versions';

if ($a) {

  vars($a);

  # change eugid if root and version directory belongs user
  my @s = stat($vdir);
  if (${">"} == 0 and (not @s or $s[4])) {
    if (my @s = stat($a)) {
      ${")"} = $s[5];
      ${">"} = $s[4];
    }
  }

  # rename old one-digit versions
  if ($vfile and (-f "$vfile~0~" or -f "$vfile~1~")) {
    for (my $i=0; $i<10; $i++) {
      my $vfile1 = "$vfile~$i~";
      my $vfile2 = "$vfile~0$i~";
      if (-f $vfile1) {
        rename $vfile1,$vfile2 or warn "vv: $vfile1 -> $vfile2 - $!\n";
      }
    }
  }

  if (not (length($opt_r) or length($opt_N) or $opt_v)) {
    if (not lstat $file) {
      if (-s $vfile) {
        warn "vv: '$a' does not exist any more\n";
        print "found '$vfile' - recover it? ";
        $_ = <STDIN>;
        copy($vfile,$file) if /^y/i;
        exit 0;
      }
      if (-e $vfile1) {
        warn "vv: '$a' does not exist any more\n";
        warn "found old:\n";
        &vv_list;
        $a = quote($a);
        warn "to recover latest version, use:\nvv -r $a\n";
        exit 1;
      }
      if (-e sdf($file,'.del')) {
        warn "vv: '$a' does not exist any more, but you can recover it with:\n";
        $a = quote($a);
        warn "del -u $a\n";
        exit 1;
      }
      die "vv: '$a' does not exist\n" unless $opt_e;
    }
    if (not $opt_e and (-l $file or not -f $file)) {
      die "vv: '$a' is not a regular file\n";
    }
  }
}

checkvdir($vdir);

if ($opt_u) {
  @versions = fmatch(quotemeta($vfile).'~\d\d?~');
  foreach $v (@versions) {
    $md5 = md5f($v);
    $v{$v} = $md5;
    $md5{$md5}++;
  }
  foreach $v (reverse @versions) {
#    printf "%s : %s\n",$v,$v{$v}
    $md5 = $v{$v};
    if ($md5{$md5} > 1 and $v =~ /~(\d\d?)~$/) {
      rotate_back($vfile,$1);
      printf "remove %s\n",quote($v) if $opt_v;
      $md5{$md5}--;
    }
  }
  exit;
}

# update old max versions number
$n = norm("$vdir/.versions/n");
if ($nn = readlink $n and $nn =~ /^\d\d?$/) {
  $nn--;
  unlink $n;
  symlink $nn,norm("$vdir/.versions/.versions");
}

if (length($opt_N)) {
  if (not $bfile) {
    if ($opt_N eq '.') {
      my $n = vv_versions('.');
      print "files in this directory have default $n versions\n";
    } elsif ($opt_N eq '0') {
      unlink '.versions/.versions/.versions';
    } elsif (-f $opt_N) {
      my $file = $opt_N;
      my $dir = dirname($file);
      my $n = readlink("$dir/.versions/.versions/".basename($file))||
              vv_versions($file)||
              vv_versions($dir);
      print "'$file' has up to $n versions\n";
    } else {
      option_usage('-N');
    }
    exit;
  }
  option_usage('-N') if not $bfile or @ARGV;
  option_usage('-N') if $opt_N !~ /^\d\d?$/;
  checkvdir($vdir,'w');
  if (mkdir $vdir and my @s = stat("$vdir/..")) {
    chmod $versionsdir_perms||$s[2],$vdir;
    chown $s[4],$s[5],$vdir;
  }
  if (not -d $vdir) { die "vv: cannot mkdir $vdir - $!\n" }
  my $vvv = norm($vdir.'/.versions');
  if (mkdir $vvv and my @s = stat("$vvv/..")) {
    chmod $versionsdir_perms||$s[2],$vvv;
    chown $s[4],$s[5],$vvv;
  }
  if (not -d $vvv) { die "vv: cannot mkdir $vvv - $!\n" }
  if ($bfile eq '.') {
    $vvv .= "/.versions";
    unlink $vvv;
    symlink $opt_N,$vvv or die "vv: cannot create $vvv - $!\n";
  } else {
    $vvv .= "/$bfile";
    unlink $vvv;
    symlink $opt_N,$vvv or die "vv: cannot create $vvv - $!\n";
    print "'$file' has up to $opt_N versions\n" if $opt_v;
    for (my $i = $opt_N+1; $i <= 99; $i++) {
      unlink sprintf("$vfile~%02d~",$i);
    }
  }
  exit;
}

if ($opt_e) {
  option_usage('-e') if not $a or @ARGV;
  system(qw'vv -qs',$file) if -f $file; # save current version
  system($editor,@ARGV,$file);
  exit $? if $?;
  unlink $ofile;                        # delete new file~ created by editor
  system(qw'vv -0',$file);              # delete last version if no change
  system(qw'vv -qs',$file);             # save new version
  exit;
}

if ($opt_p) {
  checkvdir($vdir,'w');
  opendir $vdir,$vdir or die "vv: no $vdir\n";
  while (defined($vfile = readdir($vdir))) {
    next unless -f norm("$vdir/$vfile");
    $bfile = $vfile;
    $bfile =~ s/~\d\d?~$//;
    if (not -f $bfile or -l $bfile) {
      unlink norm("$vdir/$vfile");
      $purge{$bfile}++;
    }
  }
  if (@purge = keys %purge) {
    foreach $p (@purge) {
      printf "%2d %s purged\n",$purge{$p},$p;
    }
  }
  exit;
}

if ($opt_m) {
  option_usage('-m') if @ARGV;
  migrate('.');
  exit;
}

if (length($opt_r)) {
  push @rsync,'--inplace';
  if ($opt_r eq '.') {
    # legacy backup file
    if (not -f $vfile) { die "vv: no backup '$vfile'\n" }
    if (lstat $file)   { die "vv: '$file' does exist\n" }
    copy($vfile,$file);
  } else {
    if ($opt_r eq '-') {
      $opt_r = 1;
      $md5f = md5f($file);
      foreach my $v ("$vfile~1~","$vfile~01~") {
        if (-f $v and $md5f eq md5f($v)) {
          $opt_r = 2;
          last;
        }
      }
      &vv_list;
      exit 1 unless -f $vfile1;
      $term = new Term::ReadLine $0;
      $term->ornaments(0) unless $ENV{PERL_RL};
      $opt_r = $term->readline('recover version number: ',$opt_r);
      exit if $opt_r =~ /^\s*$/;
      $opt_v = 1;
    }
    if ($opt_r =~ /^\d$/ and -f "$vfile~0$opt_r~") {
      $vfile .= "~0$opt_r~"
    } else {
      $vfile .= "~$opt_r~"
    }
    if (not -f $vfile) { die "vv: '$vfile' does not exist'\n" }
    if (defined($nfile = shift @ARGV)) {
      if (-f $nfile and not -l $nfile) {
        if (same_file($vfile,$nfile)) {
          warn "vv: '$file' and '$nfile' are identical\n";
          exit;
        }
        if ($opt_v) { system qw'vv -vs',$nfile }
        else        { system qw'vv -s',$nfile }
      } elsif (lstat $nfile) {
        die "vv: '$nfile' already exists and is not a regular file\n";
      }
      copy($vfile,$nfile);
    } else {
      $md5f ||= md5f($file);
      if (md5f($vfile) eq $md5f) {
        warn "vv: '$file' and '$vfile' are identical\n";
      } else {
        if ($vfile ne $vfile0 and mtime($file) > mtime($vfile0)) {
          copy($file,$vfile0);
        }
        copy($vfile,$file);
      }
    }
  }
  if (-f $vfile0 and md5f($vfile0) eq md5f($vfile1)) {
    unlink $vfile0;
  }
  exit;
}

if (length($opt_d)) {
  option_usage('-d') if @ARGV;
  if (not $bfile) { die "vv: no such file '$bfile'\n" }
  @diff = qw'diff -u' unless @diff;
  if ($opt_d =~ /^(\d\d?):(\d\d?)$/) {
    my $v1 = $1;
    my $v2 = $2;
    $v1 = "0$v1" if -f "$vdir/$bfile~0$v1~";
    $v2 = "0$v2" if -f "$vdir/$bfile~0$v2~";
    exec @diff,"$vdir/$bfile~$v2~","$vdir/$bfile~$v1~"
  } else {
    if (-f "$vdir/$bfile~0$opt_d~") {
      exec @diff,"$vdir/$bfile~0$opt_d~",$file;
    } else {
      exec @diff,"$vdir/$bfile~$opt_d~",$file;
    }
  }
  exit $!;
}

if ($opt_s) {
  exit if $ENV{EZZ} and $ENV{EZZ} eq $ZZ;
  $comment = "@ARGV";
  option_usage('-s') if not $bfile;
  unless ($opt_f) {
    exit if $bfile =~ /~\d\d~COMMENT$/;
    if ($exclude) {
      $exclude =~ s/^\s+//;
      $exclude =~ s/\s+$//;
      $exclude =~ s/\s*\n\s*/|/g;
      if ($bfile =~ /$exclude/) {
        warn "\r\nvv: ignoring '$bfile'\n" unless $opt_q;
        exit;
      }
    }
    if ($excludedir) {
      $excludedir =~ s/^\s+//;
      $excludedir =~ s/\s+$//;
      $excludedir =~ s/\s*\n\s*/|/g;
      my $afile = abs_path($file);
#      warn "$afile =~ /$excludedir/";
      if ($afile =~ /$excludedir/) {
        warn "\r\nvv: ignoring '$afile'\n" unless $opt_q;
        exit;
      }
    }
  }
  unless (-d $vdir) {
    mkdir $vdir or die "vv: cannot mkdir $vdir - $!\n";
    if (my @s = stat("$vdir/..")) {
      chmod $versionsdir_perms||$s[2],$vdir;
      chown $s[4],$s[5],$vdir;
    }
  }

  # migrate old file~ to versions
  if (-f $ofile and not -l $ofile and -r $ofile) {
    rotate($vfile);
    rename($ofile,$vfile1);
  }

  # turn old backup file into regular version
  if (-f $vfile) {
    rotate($vfile);
    rename($vfile,$vfile1);
  }

  # rotate if file has changed
  if (-f $vfile1 and not same_file($vfile1,$file)) {
    rotate($vfile);
  }

  my @s1 = lstat $file;
  my @s2 = lstat $vfile1;
  if (@s1 and @s2 and $s1[7] == $s2[7] and $s1[9] == $s2[9]) {
    exit;
  }
  
  copy($file,$vfile1);
  if (length $comment) {
    my $cf = $vfile1.'COMMENT';
    open $cf,'>',$cf or die "$prg: cannot write $cf - $!\n";
    print {$cf} $comment,"\n";
    close $cf;
  }
  exit;
}

# special post rotating from -e : delete last version if no change
if ($opt_0) {
  my @sb = stat $file or die "vv: '$file' - $!\n";
  if (-f $vfile1) {
    while (my @sv = stat $vfile1) {
      # no version change?
      if ($sb[7] == $sv[7] and $sb[9] == $sv[9]) {
        rotate_back($vfile);
      } else {
        last;
      }
    }
  }
  exit;
}

# delete named version, roll back
if (length($opt_D)) {
  option_usage('-D') if @ARGV or not $file;
  stat $file or die "vv: '$file' - $!\n";
  # version 0 is special! ==> backup from recovery operation
  if ($opt_D =~ /^0\b/) {
    if (-f $vfile0) {
      if (unlink $vfile0) {
        unlink $vfile0.'COMMENT';
        printf "delete %s\n",quote($vfile0) if $opt_v;
      } else {
        die "vv: cannot delete '$vfile0' - $!\n";
      }
    } else {
      die "vv: no version 0 for '$file'\n";
    }
    $opt_D =~ s/^0-/1-/;
  }
  if ($opt_D eq 'today') {
    my $dels = 0;
    while ($vfile1 and -f $vfile1) {
      my $today = isodate(time);
      $today =~ s/ .*//;
      my $vday = isodate(mtime($vfile1));
      $vday =~ s/ .*//;
      if ($today eq $vday) {
        printf "delete %s\n",quote($vfile1) if $opt_v;
        rotate_back($vfile);
        $dels++;
      }
      last if $today gt $vday;
    }
    rotate($vfile);
    copy($file,$vfile1);
    if ($dels) {
      $_ = "$dels version";
      $_ .= 's' if $dels > 1;
      warn "prg: $_ deleted\n";
    }
    exit;
  }
  if ($opt_D eq 'reduce') {
    # keep only last version every day
    @vfiles = glob("$vfile~*~");
    if (not @vfiles or $vfiles[-1] =~ /~0[12]~$/) {
      exit;
    }
    my $dels = 0;
    for (my $i=$#vfiles-1; $i; $i--) {
      last if $vfiles[$i] =~ /~01~$/;
      my $vft1 = filedate($vfiles[$i]);
      my $vft0 = filedate($vfiles[$i-1]);
      if ($vft1 eq $vft0) {
        printf "delete %s\n",quote($vfiles[$i]) if $opt_v;
        rotate_back($vfile,$i+1);
        $dels++;
      }
    }
    if ($dels) {
      $_ = "$dels version";
      $_ .= 's' if $dels > 1;
      warn "$prg: $_ deleted\n";
    }
    exit;
  }
  if ($opt_D eq 'all') {
    # equivalent to -D 1-
    unlink glob("$vfile~*~*");
    exit;
  }
  if ($opt_D eq '-') {
    # 0 version?
    if (-f $vfile0) {
      if (unlink $vfile0) {
        unlink $vfile0.'COMMENT';
        printf "delete %s\n",quote($vfile0) if $opt_v;
        exit;
      } else {
        die "vv: cannot delete '$vfile0' - $!\n";
      }
    } else {
      if (-f $vfile1) {
        printf "delete %s\n",quote($vfile1) if $opt_v;
        rotate_back($vfile)
      } else {
        die "vv: no versions for '$file'\n"
      }
    }
  } else {
    if ($opt_D =~ /^(\d\d?)$/) {
      $opt_D = int($1);
      if (-f $vfile1) {
        $dfile = sprintf "$vfile~%02d~",$opt_D;
        if (not -f $dfile) { die "vv: no version $opt_D for '$file'\n" }
        printf "delete %s\n",quote($dfile) if $opt_v;
        rotate_back($vfile,$opt_D);
      } else {
        die "vv: no versions found\n";
      }
    } elsif ($opt_D =~ /^(\d\d?)-(\d\d?)?$/) {
      $ds = $1;
      $de = $2;
      if (-f $vfile1) {
        $dfile = sprintf "$vfile~%02d~",$ds;
        if (not -f $dfile) { die "vv: no version $ds for '$file'\n" }
        if ($de) {
          for($n = $ds; $n <= $de and -f $dfile; $n++) {
            printf "delete %s\n",quote(sprintf "$vfile~%02d~",$n) if $opt_v;
            rotate_back($vfile,$ds);
          }
        } else {
          for($n = $ds; $n <= 99; $n++) {
            $dfile = sprintf("$vfile~%02d~",$n);
            if (unlink $dfile) {
              unlink $dfile.'COMMENT';
              printf "delete %s\n",quote($dfile) if $opt_v;
            }
          }
        }
      } else {
        die "vv: no versions found\n";
      }
    }
  }
  exit;
}

# delete all versions (legacy option)
if ($opt_P) {
  unless ($bfile) { die $short_usage }
  if (not($vfile0 or $vfile1)) { die "vv: '$file' has no versions\n" }
  unlink fmatch(quotemeta($vfile).'(~\d\d?~(COMMENT)?)?');
  exit;
}

if ($opt_v and $bfile and not $opt_l) {
  if (-f "$vfile~0$opt_v~") { $vfile .= "~0$opt_v~" }
  else                      { $vfile .= "~$opt_v~" }
  if (-f $vfile) {
    if (@ARGV) {
      exec @ARGV,$vfile;
    } else {
      if (-t STDOUT) {
        if (($ENV{EDITOR}||$0) =~ /jed/) {
          $ENV{JEDINIT} = "SAVE_STATE=0";
          exec 'jed','-most',$vfile;
          # exec 'jed',$vfile,qw'-tmp -f set_readonly(1)';
        } elsif ($ENV{PAGER}) {
          exec $ENV{PAGER},$vfile;
        } else {
          exec 'view',$vfile;
        }
      } else {
        exec 'cat',$vfile;
      }
    }
  } else {
    die "vv: no '$vfile'\n";
  }
  exit;
}

# default!
if (scalar(@ARGV)) { die $short_usage }
&vv_list;
exit;


sub vv_list {
  my (@stat,%v,$ct,$cols);

  checkvdir($vdir);
  if (opendir $vdir,$vdir) {
    while (defined(my $vfile = readdir($vdir))) {
      if (-f "$vdir/$vfile") {
        if ($bfile) {
          if ($vfile =~ /^\Q$bfile\E~(\d\d?)~$/) {
            push @{$v{$file}},$1;
          }
        } else {
          if ($vfile =~ /^(.+)~(\d\d?)~$/) {
            push @{$v{$1}},$2;
          }
        }
      }
    }
    closedir $vdir;
    `stty -a` =~ /columns (\d+)/;
    $cols = $1||80;
    $tw = $cols-40;
    $ct = '';
    foreach my $file (sort keys %v) {
      my @v = sort @{$v{$file}};
      my $ct_ = '';
      if ($bfile) {
        @stat = stat $file; # or die "vv: $file - $!\n";
        print "version      bytes       date time";
        if (${'opt_+'}) {
          print "     content";
          $ct_ = content($file);
          $ct_ =~ s/(.{$tw}).+/$1*/;
        } else {
          print "     comment";
        }
        print "\n";
        if (length($v[0]) == 1) { $lf = "%s  %15s %s %s\n" }
        else                    { $lf = "%2s %15s %s %s\n" }
        if (-t STDOUT) {
          foreach my $v (reverse @v) {
            my $vfile = "$vdir/$bfile~$v~";
            @stat = stat $vfile or next;
            if (${'opt_+'}) {
              $ct = content($vfile);
            } else {
              $ct = slurp($vfile.'COMMENT');
            }
            chomp $ct;
            $ct =~ s/[\t\n]/ /g;
            $ct =~ s/ +/ /g;
            $ct =~ s/\000-\037/_/g;
            $ct =~ s/(.{$tw}).+/$1*/;
            printf $lf,int($v),size($stat[7]),isodate($stat[9]),$ct;
          }
          @stat = stat $file;
          printf $lf,'.',size($stat[7]),isodate($stat[9]),$ct_ if @stat;
        } else {
          printf $lf,'.',size($stat[7]),isodate($stat[9]),$ct_ if @stat;
          foreach my $v (@v) {
            my $vfile = "$vdir/$bfile~$v~";
            @stat = stat $vfile or next;
            if (${'opt_+'}) {
              $ct = content($vfile);
            } else {
              $ct = slurp($vfile.'COMMENT');
            }
            chomp $ct;
            $ct =~ s/[\t\n]/ /g;
            $ct =~ s/ +/ /g;
            $ct =~ s/\000-\037/_/g;
            $ct =~ s/(.{$tw}).+/$1*/;
            printf $lf,int($v),size($stat[7]),isodate($stat[9]),$ct;
          }
        }
      } else {
        if (my $n = scalar(@v)) {
          $n-- if $v[0] == 0; # do not count zero version
          my $orphaned = -f $file?'':' (orphaned)';
          if ($noopt) {
            printf "%2d %s%s\n",$n,quote($file),$orphaned;
          } else {
            my $mtime = mtime(".versions/$file~01~")||
                        mtime(".versions/$file~1~");
            printf "%s %s %s%s\n",isodate($mtime),$n,quote($file),$orphaned;
          }
        }
      }
    }
  }
  unless (%v) {
    if ($opt_d) {
      die "vv: no versions found\n";
    } else {
      warn "vv: no versions found, usage: vv -h, help: vv -H\n";
    }
  }
}


sub vv_command {
  my @cmd = ();
  foreach my $a (@ARGV) {
    if ($a =~ /(.+)~$/) {
      my $file = $1;
      my $v = dirname($file).'/.versions/'.basename($file);
      $v =~ s/^\.\///;
      my @v = ();
      push @v,$file if -f $file and not -l $file;
      push @v,glob(shellquote($v).'~[0-9]~');
      push @v,glob(shellquote($v).'~[0-9][0-9]~');
      if (@v) {
        push @cmd,@v;
      } else {
        die "$prg: $file does not exist and has no versions\n";
      }
    } else {
      push @cmd,$a;
    }
  }
  warn "\$ @cmd\n";
  system @cmd;
}


sub vv_versions {
  my $x = shift;
  local $_;
  
  if (-d $x) {
    my $dir = abs_path($x);
    my $n = readlink("$dir/.versions/.versions/.versions");
    return $n if $n and $n =~ /^\d+$/;
    return $versions if $dir eq '/';
    return vv_versions(dirname($dir));
  } else {
    $x = basename($x);
    foreach my $rx (keys %versions) {
      return $versions{$rx} if $x =~ /^$rx$/;
    }
  }
}


sub shellquote {
  local $_ = shift;
  s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
  return $_;
}


sub size {
  my $s = shift;
#  if    ($s > 9999999999) { $s = int($s/2**30).'G' }
#  elsif ($s > 9999999)    { $s = int($s/2**20).'M' }
#  elsif ($s > 9999)       { $s = int($s/2**10).'k' }
#  else                    { $s = d3($s) }
  $s = d3($s);
  return $s;
}


sub d3 {
  local $_ = shift;
  while (s/(\d)(\d\d\d\b)/$1,$2/) {};
  return $_;
}


sub content {
  my $file = shift;
  my $qfile = quotemeta($file);
  my $ct;
  local $_;

  chomp ($ct = `file $qfile`);
  $ct =~ s/.*?: //;
  $ct =~ s/,.*//;

  if ($ct =~ /text/ and open $file,$file) {
    read $file,$_,1024;
    close $file;
    s/[\x00-\x20]+/ /g;
    s/^ */\"/;
    s/ *$/\"/;
    $ct = $_;
  }

  return $ct;
}


sub isodate {
  my @d = localtime shift;
  return sprintf('%d-%02d-%02d %02d:%02d:%02d',
                 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
}


sub filedate {
  my @d = localtime(mtime(shift));
  return sprintf('%d-%02d-%02d',$d[5]+1900,$d[4]+1,$d[3]);
}


sub rotate {
  my $vf = shift; # version base file
  my $vf0 = "$vf~00~";
  my $vf1 = "$vf~01~";
  my $dir = dirname($vf);
  my $n = readlink("$dir/.versions/".basename($vf))||
          vv_versions($vf)||
          vv_versions($dir);
  (my $today) = (isodate(time) =~ /(\S+) /);
  
  if (same_file($vf0,$vf1)) {
    unlink $vf0;
    unlink $vf0.'COMMENT';
  }
  for (my $i = $n-1; $i >= 0; $i--) {
    my $vfi = sprintf("%s~%02d~",$vf,$i);
    my $vfn = sprintf("%s~%02d~",$vf,$i+1);
    
    # keep last version not from today
    if ($i+1 == $n and -f $vfn) {
      my $vfid = filedate($vfi);
      my $vfnd = filedate($vfn);
      next if $vfnd lt $today and $vfid eq $today;
    }
    
    if (-e $vfi) {
      rename $vfi,$vfn or die "vv: '$vfi' -> '$vfn' : $!\n";
      unlink $vfn.'COMMENT';
      rename $vfi.'COMMENT',$vfn.'COMMENT';
    }
  }

  # delete superflous versions
  for (my $i = $n+1; $i <= 99; $i++) {
    unlink sprintf("%s~%02d~",$vf,$i);
    unlink sprintf("%s~%02d~COMMENT",$vf,$i);
  }

  if ($n > 1) {
    # was there a version 0?
    if (-e $vf1) {
      my $bf = $vf;
      $bf =~ s:/\.versions/:/:;
      my @sb = stat $bf;
      my @sv = stat $vf1;
      # same version? (size and mtime)
      if (@sb and @sv and $sb[7] == $sv[7] and $sb[9] == $sv[9]) {
        # same version
        unlink $vf1;
        unlink $vf1.'COMMENT';
      } else {
        # new version
        rotate($vf);
      }
    }
  }
}


sub copy {
  my $from = rrp(shift);
  my $to = rrp(shift);
  my @copy = @rsync;
  my $qfrom = quote($from);

  unless (-e $from) {
    die "vv: '$from' does not exist\n";
  }
  if (-l $from or not -f $from) {
    die "vv: '$from' is not a regular file\n";
  }
  if (-l $to or -e $to and not -f $to) {
    die "vv: '$to' is not a regular file\n";
  }

  if (`stat -f -c %T $qfrom 2>/dev/null` =~ /^btrfs/) {
    @copy = qw'cp -ax --reflink';
  }

  if (open $to,'>>',$to) {
    close $to;
# warn "@copy $from $to";    
    if (system(@copy,$from,$to) == 0) {
      if ($ENV{VIMRUNTIME}) {
        print "\n";
      } else {
        # print "'$file' has up to $n versions\n" if $opt_v;
        print "$from -> $to\n" if $opt_v;
      }
    } else {
      exit $?;
    }
  } else {
    die "\r\nvv: cannot write '$to' - $!\n";
  }
}


sub realfilename {
  my $file = shift;

  return $file unless -e $file;

  if (-l $file) {
    my $link = readlink($file);
    if ($link !~ /^\// and $file =~ m:(.*/).:) {
      $link = $1 . $link;
    }
    return realfilename($link);
  } else {
    return $file;
  }
}


sub migrate {
  my $dir = shift;
  my $vdir = norm("$dir/.versions");
  my $dfile;

  unless (opendir $dir,$dir) {
    warn "vv: cannot read directory $dir - $!\n";
    return
  }
  while (defined($file = readdir($dir))) {
    $dfile = "$dir/$file";
    next if -l $dfile or $file eq '.' or $file eq '..';
    if (-d $dfile and $opt_R and $file ne '.versions') {
      migrate($dfile);
    } elsif (-f $dfile and $file =~ /(.+)~$/) {
      my $bfile = $1;
      if (-d $vdir) {
        rotate("$vdir/$bfile");
      } else {
        mkdir $vdir or die "vv: cannot mkdir $vdir - $!\n";
        if (my @s = stat("$vdir/..")) {
          chmod $versionsdir_perms||$s[2],$vdir;
          chown $s[4],$s[5],$vdir;
        }
      }
      $nfile = sprintf("%s/%s01~",$vdir,$file);
      rename $dfile,$nfile or die "vv: cannot move '$dfile' to '$nfile' - $!\n";
      print "$dfile -> $nfile\n" if $opt_v;
    }
  }
  closedir $dir;
}


sub mtime {
  my @s = stat shift;
  return @s ? $s[9] : 0;
}


sub md5f {
  my $file = shift;
  my $md5 = Digest::MD5->new;
  my $MB = 2**20;
  local $_;

  if (open $file,$file) {
    while (read($file,$_,$MB)) {
      $md5->add($_);
    }
    close $file;
  }

  return $md5->hexdigest;
}


sub same_file {
  my $file1 = shift;
  my $file2 = shift;

  return unless -f $file1 and -f $file2;
  return 0 if -s $file1 != -s $file2;
  return 1 if mtime($file1) == mtime($file2);
  return 1 if md5f($file1) eq md5f($file2);
  return 0;
}


# if ARGV is empty use last saved file as default file argument
sub check_ARGV {
  local $_;
  local *V;

  if (not @ARGV) {
    if (-d '.versions' and open V,'ls -at .versions|') {
      while (<V>) {
        chomp;
        if (-f ".versions/$_") {
          close V;
          s/~\d+~$//;
          warn "$prg: using $_\n";
          @ARGV = ($_);
          return;
        }
      }
    }
  }

}


sub rotate_back {
  my $vfile = shift;
  my $start = shift||1;

  for (my $i = $start; $i <= 98; $i++) {
    my $vfi = sprintf("%s~%02d~",$vfile,$i);
    my $vfn = sprintf("%s~%02d~",$vfile,$i+1);
    if (-f $vfn) {
      rename $vfn,$vfi;
      rename $vfn.'COMMENT',$vfi.'COMMENT';
    } else {
      if ($i == $start) {
        unlink $vfi;
        unlink $vfi.'COMMENT';
      }
      last;
    }
  }
}


sub pathsearch {
  my $prg = shift;

  foreach my $dir (split(':',$ENV{PATH})) {
    return "$dir/$prg" if -x "$dir/$prg";
  }
}


# subdirectory file
sub sdf {
  my $file = shift;
  my $dir = shift;
  return rrp(sprintf("%s/%s/%s",dirname($file),$dir,basename($file)));
}


sub option_usage {
  my $o = shift;
  if ($usage =~ /\n *(#.*\n) *vv $o/) {
    my $ou = $1;
    foreach (split "\n",$usage) {
      last if /examples/;
      $ou .= "usage: $1\n" if /(vv $o.*)/;
    }
    die  $ou."help: vv -H\n";
  } else {
    die $short_usage;
  }
}


# zz is the generic clipboard program
#
# to use zz with vim, write to your .vimrc:
#
# noremap <silent> zz> :w !zz<CR><CR>
# noremap <silent> zz< :r !zz --<CR>
sub zz {
  my $bs = 2**16;
  my $wm = '>';
  my ($file,$tee,$x);

  if ("@ARGV" =~ /^(-h|--help)$/) {
    print '
zz is the generic clipboard program. It can hold any data, ASCII or binary.
The clipboard itself is $ZZ (default: $HOME/.zz) and has up to 9 versions.
See also the clipboard editor "ezz".
Limitation: zz does not work across accounts or hosts! Use xxx or yy instead.

Options and modes are:

  "zz"              write $ZZ to STDOUT (it STDIN is a tty)
  "zz --"           write $ZZ to STDOUT (anyway)
  "zz FILE(s)"      write content of FILE(s) to $ZZ
  "zz -"            write STDIN (keyboard, mouse buffer) to $ZZ
  "zz +"            add STDIN (keyboard, mouse buffer) to $ZZ
  "... | zz"        write STDIN from pipe to $ZZ
  "... | zz +"      add STDIN from pipe to $ZZ
  "... | zz -"      write STDIN from pipe to $ZZ and STDOUT
  "zz | ..."        write $ZZ to pipe
  "... | zz | ..."  save pipe data to $ZZ (like tee)
  "zz -v"           show clipboard versions (history)
  "zz -N"           write $ZZ version N to STDOUT
  "zz - N [M ...]"  write $ZZ versions N M ... to $ZZ
  "zz + N [M ...]"  add $ZZ versions N M ... to $ZZ
  "zz -N-"          delete $ZZ version N
  "zz -d [N[:M]]"   show diff of $ZZ version N [to M]

Examples:

  zz *.txt              # write contents of *.txt to $ZZ
  ls -l | zz            # pipe output of ls to $ZZ
  zz | wc -l            # pipe $ZZ to wc
  zz -5                 # write $ZZ version 5 to STDOUT
  zz -5-                # delete $ZZ version 5
  (within vi)   :w !zz  # write buffer to $ZZ
  (within vi)   :r !zz  # read $ZZ into buffer
  (within mutt) |zz     # write mail to $ZZ
';
    exit;
  }

  if ("@ARGV" eq '-V') {
    warn "$prg: version $version\n";
    warn "see: vv -vV\n";
    exit;
  }

  if ("@ARGV" eq '-v') {
    exec qw'vv -+l',$ZZ;
  }

  if ("@ARGV" =~ /^-(\d)$/) {
    exec "vv -v $1 '$ZZ' | cat";
  }

  if ("@ARGV" =~ /^-d/) {
    shift @ARGV;
    my $d = 1;
    if ("@ARGV" =~ /^((\d+)(:\d+)?)/ and $2) {
      $d = $1;
    }
    exec "vv -d $d '$ZZ'";
  }

  if ("@ARGV" =~ /^-(\d)-$/) {
    exec "vv -D $1 '$ZZ'";
  }

  if ("@ARGV" =~ /^\+ \d/) {
    my $tmp = "$ZZ.tmp";
    shift @ARGV;
    foreach my $n (@ARGV) {
      if ($n !~ /^\d$/) {
        die "usage: $prg + NUMBER [...]\n";
      }
    }
    system 'cp',$ZZ,$tmp;
    foreach my $n (@ARGV) {
      system "vv -v $n '$ZZ' >> '$tmp'";
    }
    system qw'vv -s',$ZZ if -s $ZZ;
    rename $tmp,$ZZ or die "$prg: cannot rename $tmp to $ZZ - $!\n";
    exit;
  }

  if ("@ARGV" =~ /^- \d/) {
    my $tmp = "$ZZ.tmp";
    shift @ARGV;
    foreach my $n (@ARGV) {
      if ($n !~ /^\d$/) {
        die "usage: $prg + NUMBER [...]\n";
      }
    }
    open $tmp,'>',$tmp or die "$prg: cannot write $tmp - $!\n";
    close $tmp;
    foreach my $n (@ARGV) {
      if ($n) {
        system "vv -v $n '$ZZ' >> '$tmp'";
      } else {
        system "cat '$ZZ' >> '$tmp'";
      }
    }
    system qw'vv -s',$ZZ if -s $ZZ;
    rename $tmp,$ZZ or die "$prg: cannot rename $tmp to $ZZ - $!\n";
    exit;
  }

  # read mode
  if (-t STDIN and not @ARGV or "@ARGV" eq '--') {
    exec 'cat',$ZZ;
  }

  # write mode
  system "vv -s '$ZZ' >/dev/null 2>&1" if -s $ZZ;

  if (@ARGV and $ARGV[0] eq '+') {
    shift @ARGV;
    $wm = '>>';
  }

  if ("@ARGV" eq '-') {
    @ARGV = ();
    $tee = 1 unless -t STDIN;
  }

  unless (@ARGV or -t STDIN or -t STDOUT) {
    $tee = 1;
  }

  open $ZZ,$wm,$ZZ or die "zz: cannot write $ZZ - $!\n";

  if (@ARGV) {
    while ($file = shift @ARGV) {
      if (-f $file) {
        if (open $file,$file) {
          while (read($file,$x,$bs)) {
            my $s = syswrite $ZZ,$x;
            defined($s) or die "zz: cannot write to $ZZ - $!\n";
          }
          close $file;
        } else {
          warn "zz: cannot read '$file' - $!\n";
        }
      } elsif (-e $file) {
        warn "zz: '$file' is not a regular file\n";
      } else {
        warn "zz: '$file' does not exist\n";
      }
    }
    close $ZZ;
    $ZZ1 = $ZZ.'~1~';
    $ZZ1 =~ s:(.*)/(.*):$1/.versions/$2:;
    if (-e $ZZ and not -s $ZZ and -s $ZZ1 ) {
      system @rsync,$ZZ1,$ZZ;
    }
  } else {
    while (read(STDIN,$x,$bs)) {
      syswrite $ZZ,$x;
      syswrite STDOUT,$x if $tee;
    }
  }

  exit;
}


sub ezz {
  my $bs = 2**16;
  my $wm = '>';
  my $editor = $ENV{EDITOR} || 'vi';
  my ($out,$file,$x);

  $ENV{JEDINIT} = "SAVE_STATE=0";

  if ("@ARGV" =~ /^(-h|--help)$/) {
    print <<'EOD';
ezz is the edit helper for the zz clipboard program.
The clipboard itself is $ZZ (default: $HOME/.zz) and has up to 9 versions.

Options and modes are:

  "ezz"                    edit $ZZ with $EDITOR
  "ezz -1"                 edit $ZZ version 1 (see: zz -v)
  "ezz -9"                 edit $ZZ version 9 (see: zz -v)
  "... | ezz"              write STDIN from pipe to $ZZ and call $EDITOR
  "... | ezz +"            add STDIN from pipe to $ZZ and call $EDITOR
  "ezz 'PERL COMMANDS'"    execute PERL COMMANDS on $ZZ
  "ezz - 'PERL COMMANDS'"  execute PERL COMMANDS on $ZZ and show result
  "ezz FILTER [ARGS]"      run FILTER [with ARGS] on $ZZ
  "ezz - FILTER [ARGS]"    run FILTER [with ARGS] on $ZZ and show result

Examples:

  ls -l | ezz
  ezz 's/ /_/g'
  ezz head -3
  ezz - head -3
EOD
    exit;
  }

  system "vv -s '$ZZ' >/dev/null 2>&1" if -s $ZZ;

  unless (-t STDIN) {
    if ("@ARGV" eq '+') {
      @ARGV = ();
      $wm = '>>';
    }
    open $ZZ,$wm,$ZZ or die "zz: cannot write $ZZ - $!\n";
    syswrite $ZZ,$x while read(STDIN,$x,$bs);
    close $ZZ;
  }

  if (-t STDIN and "@ARGV" =~ /^-([1-9])$/) {
    my $v = $1;
    $ZZ =~ s:(.+)/(.+):$1/.versions/$2~0$v~:;
    if (-f $ZZ) {
      close STDIN;
      open STDIN,'/dev/tty';
      $ENV{EZZ} = $ZZ;
      exec $editor,$ZZ;
    } else {
      die "$0: no zz version $v\n";
    }
  }

  if (@ARGV) {
    $out = shift @ARGV if $ARGV[0] eq '-';
    $cmd = shift @ARGV or exec 'cat',$ZZ;
    rename $ZZ,"$ZZ~" or die "zz: cannot move $ZZ to $ZZ~ - $!\n";
    $cmd = quotemeta $cmd;
    @ARGV = map { quotemeta } @ARGV;
    if (pathsearch($cmd)) {
      system "$cmd @ARGV <'$ZZ~'>'$ZZ'";
    } else {
      system "perl -pe $cmd @ARGV <'$ZZ~'>'$ZZ'";
    }
    if ($? == 0) { unlink "$ZZ~" }
    else         { rename "$ZZ~",$ZZ }
    exec 'cat',$ZZ if $out;
  } else {
    close STDIN;
    open STDIN,'/dev/tty';
    exec $editor,$ZZ;
  }
  exit;
}


# tt is the multi-user clipboard program
sub tt {
  my $bs = 2**16;
  my $args  = "$TT/1/args";
  my $data  = "$TT/1/data";
  my $args0 = "$TT/0/args";
  my $data0 = "$TT/0/data";
  my $keep = '-k';
  my @tar = qw'tar --no-same-owner';
  my ($file,$tee,$x,$mtime,$mode,$cols);

  my $usage = '
tt is the multi-user clipboard program for files or STDIN/STDOUT.
The clipboard itself is $TT (default: /var/tmp/tt) and has up to 9 versions.
Limitation: tt does not work across hosts! Use xxx or yy instead.

Options and modes are:

  "tt"              interactive clipboard selector
  "tt FILE(s)"      write tar of FILE(s) to $TT
  "tt -"            write STDIN (keyboard, mouse buffer) to $TT
  "tt +"            add STDIN (keyboard, mouse buffer) to $TT
  "tt --"           write $TT to STDOUT or extract tar from $TT
  "tt ---"          like "tt --" but overwrite existing file
  "tt | ..."        write $TT to pipe
  "... | tt"        write STDIN from pipe to $TT
  "... | tt +"      add STDIN from pipe to $TT
  "... | tt -"      write STDIN from pipe to $TT and STDOUT
  "... | tt | ..."  save pipe data to $TT (like tee)

Examples:

  tt *.jpg          # write tar of *.jpg to $TT
  ls -l | tt        # pipe output of ls to $TT
  tt | wc -l        # pipe $TT to wc
  tt --             # extract $TT
';

  if ("@ARGV" =~ /^(-h|--help)$/) {
    print $usage;
    exit;
  }
  
  if (not @ARGV and -t STDIN and -t STDOUT) {
    unless (-f $data) { die "tt: no data in $TT\n" }
    local $| = 1;
    $mode = 'Get';
    for (;;) {
      $mtime = mtime($TT);
      `stty -a` =~ /columns (\d+)/;
      $cols = $1||80;
      print "\n";
      for (my $i=1; $i<=9; $i++) {
        $data = "$TT/$i/data";
        $args = "$TT/$i/args";
        my @s = stat($data) or last;
        my $date = isodate($s[9]);
        my $user = getpwuid($s[4])||'??';
        my $size = d3($s[7]);
        my $type = -f "$TT/$i/args" ? 'tar' : 'data';
        printf "[%d] %s %s %s\n",$i,$date,$user,$size;
        $_ = '    ';
        if (open $args,$args) {
          chomp(my $a = <$args>);
          close $args;
          $_ .= "tar: $a";
        } else {
          $_ .= content($data);
        }
        $_ = substr($_,0,$cols-1).'*' if length>$cols;
        print "$_\n";
      }
      if ($mode eq 'Get') {
        print "Enter number or d (delete) or o (overwrite mode) or q (quit)\n";
      }
      if ($mode eq 'Delete') {
        print "Enter number or g (get) or q (quit)\n";
      }
      print "$mode [ ]\b\b";
      for (;;) {
        my $k = ReadKey();
        if ($k eq "\n") {
          print "\r";
          exit;
        }
        if ($k eq 'q') {
          print "$k]\n";
          exit;
        }
        if ($k eq 'o') {
          print "$k]\n";
          print "$mode [ ]\b\b";
          $keep = '';
          redo;
        }
        if ($k eq 'g') {
          print "$k]\n";
          $mode = 'Get';
          last;
        }
        if ($k eq 'd') {
          print "$k]\n";
          $mode = 'Delete';
          last;
        }
        if ($k =~ /\d/ and -d "$TT/$k") {
          print "$k]\n";
          if ($mtime ne mtime($TT)) {
            print "\n$TT has been modified! Press ENTER for renumbering";
            $_ = <STDIN>;
            last;
          }
          if ($mode eq 'Delete') {
            tt_del($k);
          }
          if ($mode eq 'Get') {
            $data = "$TT/$k/data";
            $args = "$TT/$k/args";
            if (-f $args) {
              if ($keep) {
                exec @tar,'-xkvf',$data;
              } else {
                exec @tar,'-xvf',$data;
              }
            } else {
              my $term = new Term::ReadLine $0;
              $term->ornaments(0) unless $ENV{PERL_RL};
              my $cmd = $term->readline('Pipe data to: ');
              if ($cmd =~ /^\s*$/ or $cmd eq '-') {
                exec 'cat',$data;
              } else {
                exec "cat $data | $cmd";
              }
            }
          }
          last;
        }
      }
    }
  }

  if (not -t STDIN and "@ARGV" !~ /^(-+$|\+)?$/) {
    die $usage;
  }
  
  # read mode
  
  if (-t STDIN) {
    if (@ARGV) {
      if ("@ARGV" !~ /^---?$/ and not -t STDOUT) {
        die $usage;
      }
      if (-f $args) {
        if (-t STDOUT) {
          if ("@ARGV" eq '---') {
            # overwrite existing files  
            exec @tar,'-xvf',$data;
          } elsif ("@ARGV" eq '--') {
            # do not overwrite existing files  
            exec @tar,'-xkvf',$data;
          }
        } else {
          # write to pipe
          exec 'cat',$data;
        }
      } elsif ("@ARGV" =~ /^---?$/)  {
        exec 'cat',$data;
      }
    } elsif (not -t STDOUT) {
      # write to pipe
      exec 'cat',$data;
    }
  }
    
  # write mode
  
  umask 0;
  
  unless (-d $TT) {
    mkdir $TT or die "tt: cannot mkdir $TT - $!\n";
  }

  if ("@ARGV" =~ /^--?$/) {
    @ARGV = ();
    $tee = not -t STDIN;
  }

  unless (@ARGV or -t STDIN or -t STDOUT) {
    $tee = 1;
  }

  if ("@ARGV" eq '+') {
    @ARGV = ();
    if (-f $args) {
      die "tt: $TT/1 contains a tar archive, cannot add data\n";
    }
    open $data,'>>',$data or die "tt: cannot write $data - $!\n";
    while (read(STDIN,$x,$bs)) {
      syswrite $data,$x;
      syswrite STDOUT,$x if $tee;
    }
    close $data;
    exit;
  }

  if (-f $data0) {
    if (mtime($data0)+3 > time) {
      die "tt: $data0 is currently written by another process\n";
    }
    unlink $data0,$args0;
  }

  mkdir "$TT/0";
  open $data,'>',$data0 or die "tt: cannot write $data0 - $!\n";

  if (@ARGV) {
    foreach my $a (@ARGV) {
      unless (-f $a or -d $a) {
        die "tt: $a is neither a regular file or directory\n";
      }
      unless (-r $a) {
        die "tt: $a is not readable\n";
      }
      $a .= '/' if -d $a;
    }
    if (open $args,'>',$args0) {
      print {$args} "@ARGV\n";
      close $args;
    } else {
      warn "tt: cannot write $args0 - $!\n";
    }
    close $data;
    system qw'tar cvf',$data0,@ARGV;
  } else {
    while (read(STDIN,$x,$bs)) {
      syswrite $data,$x;
      syswrite STDOUT,$x if $tee;
    }
    close $data;
#    if ($tee) {
#      system 'tee',$data0;
#    } else {
#      system 'dd','status=none',"bs=$bs","of=$data0";
#    }
  }
  exit $? if $?;

  if (-s $data0) {
    system qw'rm -rf',"$TT/9" if -e "$TT/9";
    for (my $i=9; $i; $i--) {
      my $tt1 = sprintf "$TT/%d",$i-1;
      my $tt2 = sprintf "$TT/%d",$i;
      system 'mv',$tt1,$tt2 if -d $tt1;
    }
  } else {
    system qw'rm -rf',"$TT/0";
  }
  
  exit;
}


sub tt_del {
  my $d = shift;
  
  system qw'rm -rf',"$TT/$d";
  for (my $i=$d; $i<9; $i++) {
    my $tt1 = sprintf "$TT/%d",$i;
    my $tt2 = sprintf "$TT/%d",$i+1;
    last unless -d $tt2;
    system 'mv',$tt2,$tt1;
  }
}

sub del {
  my $usage = <<EOD;
del: (un)delete file(s) (with versions)
usage: del [-v] [-f] file(s)
       del [-v] -u file(s)
       del [-v] -p file(s)
       del [-v] -P [-d days] [-r] [directory]
       del [-v] -D file(s)
       del [-v] -l
options: -v   verbose mode
         -f   force delete
         -u   undelete file(s)
         -p   purge deleted file(s)
         -P   purge all deleted files [older than -d days] [-r recursive]
         -D   real delete file(s) and versions (NO WAY BACK!)
         -l   list deleted files
examples: del *.tmp         # delete all *.tmp files
          del -u project.pl # undelete project.pl
          del -vprd 2       # verbose purge deleted files older than 2 days
see also: vv -h
EOD

  $error = 0;

  $opt_h = $opt_v = $opt_V = 0;
  $opt_u = $opt_p = $opt_P = $opt_D = $opt_r = $opt_l = $opt_d = $opt_f = 0;
  getopts('hvVufpPDlrd:') or die $usage;

  if ($opt_h) {
    print $usage;
    exit 0;
  }

  if ($opt_V) {
    warn "$prg: version $version\n";
    warn "see: vv -vV\n";
    exit;
  }


  if ($opt_u) {
    die $usage unless @ARGV;
    foreach $file (@ARGV) {
      $dfile = sdf($file,'.del');
      unless (lstat $dfile) {
        warn "del: '$dfile' does not exist\n";
        $error++;
        next;
      }
      if (lstat $file) {
        warn "del: '$file' already exists\n";
        $error++;
        next;
      }
      if (move($dfile,dirname($file))) {
        print "'$dfile' -> '$file'\n" if $opt_v;
      } else {
        warn "del: moving '$dfile' to '$file' failed\n";
        $error++;
      }
    }
  }

  elsif ($opt_p) {
    die $usage if $opt_d or $opt_r or not @ARGV;
    $error = 0;
    foreach $file (@ARGV) {
      $dfile = ".del/$file";
      if (unlink $dfile) {
        printf "purge %s\n",quote($dfile) if $opt_v;
      } else {
        warn "del: '$dfile' dies not exist\n";
        $error++;
      }
    }
    exit $error;;
  }

  elsif ($opt_P) {
    die $usage if $opt_d !~ /^(\d+)$/;
    $dir = shift @ARGV || '.';
    die $usage if @ARGV;
    die "del: $dir is not a directory\n" if -l $dir or not -d $dir;
    if ($opt_r) {
      $find = "find $dir -type d -name .del 2>/dev/null";
      open $find,"$find|" or die "del: cannot run find - $!\n";
      while (<$find>) {
        chomp;
        purge($_);
      }
    } else {
      purge("$dir/.del");
    }
    rmdir("$dir/.del");
    exit;
  }

  elsif ($opt_D) {
    unless (@ARGV) { die $usage }
    $error = 0;
    foreach $p (@ARGV) {
      $p =~ s:/+$::;
      @x = ();
      push @x,$p if lstat $p;
      $d = sdf($p,'.del');
      push @x,$d if lstat $d;
      push @x,fmatch(quotemeta(sdf($p,'.versions')).'~\d\d?~(COMMENT)?'),
              fmatch(quotemeta(sdf($p,'.del/.versions')).'~\d\d?~(COMMENT)?');
      if (@x) {
        if ($opt_v) {
          foreach my $x (@x) {
            printf "purge %s\n",quote($x);
          }
        }
        vsystem(qw'rm -rf',@x);
      } else {
        warn "del: cannot purge '$p': not found\n";
        $error++;
      }
    }
    exit $error;;
  }

  elsif ($opt_l) {
    $dir = $ARGV[0] || '.';
    $deldir = "$dir/.del";
    chdir $deldir or die "del: $deldir - $!\n";
    opendir $deldir,'.' or die "del: cannot open $deldir - $!\n";
    @files = grep { not /^(\.\.?|\.del|\.versions)$/ } readdir($deldir);
    die "del: no deleted files found\n" unless @files;
    foreach $file (sort { lc $a cmp lc $b } @files) {
      printf "%s %15s ",mdtime($file),d3size($file) if $opt_v;
      print quote($file);
      print '/' if -d $file;
      print ' -> '.quote(readlink $file) if $opt_v and -l $file;
      print "\n";
    }
    exit;
  }

  else {
    die $usage unless @ARGV;

    foreach $file (@ARGV) {
      if ($file =~ /(.+)\/+$/ and not -d $1 and not -l $1 and -e $1) {
        warn "del: '$1' is a file, not a directory\n";
        $error++;
        next;
      }
      if (not -l $file and -e $file) {
        if (-d $file) {
          $dir = abs_path($file);
          if (not $opt_f and $dir !~ m:^/tmp/: and
             ($dir =~ m:^/[^/]*$: or $dir =~ m:^/[^/]*/[^/]*$:)
          ) {
            warn "del: cannot delete '$dir/', too dangerous\n";
            $error++;
            next;
          }
          if (not $file =~ s/\/+$// and not $opt_f) {
            warn "del: '$file' is a directory, use '$file/' to delete it\n";
            $error++;
            next;
          }
        }
      }
      unless (lstat $file) {
        warn "del: '$file' does not exist\n";
        $error++;
        next;
      }
      $dir = dirname($file);
      $deldir = norm("$dir/.del");
      unless (-d $deldir) {
        unless (mkdir($deldir)) {
          warn "del: cannot mkdir $deldir - $!\n";
          $error++;
          next;
        }
        if (my @s = stat($dir)) {
          chmod $s[2],$deldir;
          chown $s[4],$s[5],$deldir;
        }
      }
      if ($macos) {
        # workaround for broken cp in MacOS
        vsystem(qw'rm -rf',$deldir.'/'.basename($file));
      }
      if (cpal($file,$deldir)) {
        if (-l $file or not -d $file) {
          vsystem(qw'rm -f',$file);
          my $vfile = "$dir/.versions/".basename($file);
          if (my @versions = fmatch(quotemeta($vfile).'(~\d\d?~(COMMENT)?)?')) {
            my $vdir = norm($deldir.'/.versions');
            mkdir $vdir;
            vsystem('mv',@versions,$vdir) if -d $vdir;
          }
        } else {
          vsystem(qw'rm -rf',$file);
        }
      } else {
        warn "del: moving '$file' to '$deldir/' failed";
        $error++;
      }
    }
  }

  exit ($error ? 1 : 0);
}

sub cpal {
  my $f = shift;
  my $ddir = shift;
  my @cp;
  if ($macos) {
    @cp = qw'cp -a'; # workaround for broken cp in MacOS
  } else {
    @cp = qw'cp -f -l -a';
  }
  push @cp,'-v' if $opt_v;

  $f =~ s:/+$::;
  unless (lstat $f) {
    warn "$0: cannot access '$a'\n";
    return 0;
  }
  unless (lstat $ddir) {
    warn "$0: cannot access '$ddir'\n";
    return 0;
  }
  if (-l $ddir or not -d $ddir) {
    warn "$0: '$ddir' is not a directory\n";
    return 0;
  }

  my @files = ();
  if (open my $find,'-|',qw'find',$f,'-print0') {
    local $_;
    local $/ = "\0";
    while (<$find>) {
      chomp;
      push @files,$_;
    }
    close $find;
  }
  my $dir = dirname($f).'/';
  $dir = '' if $dir eq './';
  foreach my $s (@files) {
    my $d = $s;
    $d =~ s:^\Q$dir:$ddir/:;
    if ((-l $s or not -d $s) and (-d $d and not -l $d)) {
      system qw'rm -rf',$d;
    } elsif ((-d $s and not -l $s) and (-l $d or not -d $d)) {
      unlink $d;
    }
  }

  my @cmd = (@cp,$f,$ddir);
  # print "\$ @cmd\n" if $opt_v;
  vsystem(@cmd);
  if ($?) {
    return 0;
    # $error = (($? >> 8) || 1);
  } else {
    return 1;
  }
}


# move file and versions
sub move {
  my $file = rrp(shift);
  my $ddir = rrp(shift);
  my $fdir = dirname($file);
  my $dfile = basename($file);
  my $ddf;

  unless (lstat $file) {
    warn "del: '$file' does not exist\n";
    $error++;
    return;
  }

  unless (-d $ddir) {
    die "del: destination '$ddir' is not a directory (internal error)";
  }

  if ($dfile =~ m:^/*$: or $dfile eq '.' or $dfile eq '..') {
    die "del: \$dfile == '$dfile' ?!"
  }

  $ddf = norm("$ddir/$dfile");
  if (lstat $ddf) {
    if ($ddf =~ m:/\.del/:) {
      # not used any more, but cpal()
      unlink $ddf or system qw'rm -rf',$ddf;
      if (lstat $ddf) {
        unlink $ddf;
        $error++;
        return;
      }
    } else {
      warn "del: '$ddf' already exists\n";
      $error++;
      return;
    }
  }
  if (rename $file,$ddf) {
    my $vfile = "$fdir/.versions/$dfile";
    if (my @versions = fmatch(quotemeta($vfile).'(~\d\d?~(COMMENT)?)?')) {
      my $vdir = norm($ddir.'/.versions');
      mkdir $vdir;
      if (-d $vdir) {
        system 'mv',@versions,$vdir;
      }
    }
    # printf "%s -> %s\n",quote(rrp($file)),quote(rrp($ddf)) if $opt_v;
    @s = lstat($ddf);
    utime($s[8],$s[9],$ddf); # set CTIME
    return $ddf;
  } else {
    $error++;
  }
}


# relative path without leading ./
sub rrp {
  local $_ = shift;
  s:^\./:: if m:/.+/:;
  s://+:/:g;
  return $_;
}


sub norm {
  local $_ = shift;
  s://+:/:g;
  s:^\./(.):$1:;
  return $_;
}


sub mdtime {
  my @d = localtime((lstat shift)[9]);
  return sprintf('%d-%02d-%02d %02d:%02d:%02d',
                 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
}


sub purge {
  my $ddir = shift;
  my $after = $opt_d * 60*60*24;
  my $now = time();
  my $cwd = getcwd();
  local $_;

  die "del: purge $ddir ?!\n" if $ddir !~ /\.del$/;
  die "del: $ddir is a symlink\n" if -l $ddir;
  if ($< + $opt_d == 0) {
    opendir $ddir,$ddir or die "del: cannot open $ddir - $!\n";
    my @f = ();
    while (defined(my $f = readdir $ddir)) {
      push @f,"$ddir/$f" if $f ne '.' and $f ne '..';
    }
    if ($opt_v and @f) {
      printf "purge %s\n",quote($_) foreach (sort @f);
    }
    system qw'rm -rf',$ddir;
    if (lstat $ddir) {
      warn "del: cannot purge '$ddir'\n";
      $error++;
    } else {
      printf "purge %s\n",quote($ddir) if $opt_v;
    }
    return;
  }
  chdir($ddir) or die "del: cannot chdir($ddir) - $\n";
  if (opendir my $dir,'.') {
    foreach my $file (sort(readdir($dir))) {
      next if $file =~ /^\.\.?$/;
      my @stat = lstat $file;
      unless (@stat) {
        warn "del: cannot purge '$ddir/$file' - $!\n";
        $error++;
        next;
      }
      if ($now > $stat[10]+$after) {
        local ($(,$>,$)) = (0,@stat[4,5]) if $< == 0;
        unlink $file or system qw'rm -rf',$file;
        if (lstat $file) {
          unlink $file;
          warn "del: cannot purge '$ddir/$file' - $!\n";
          $error++;
        } else {
          unlink fmatch("$ddir/.versions/".quotemeta($file).'~\d\d?~(COMMENT)?');
          printf "purge %s\n",quote("$ddir/$file") if $opt_v;
        }
      }
    }
  } else {
    warn "del: cannot open $ddir - $!\n";
    $error++;
  }
  chdir($cwd);
}


sub quote {
  local $_ = shift;
  my $mc = '\'\[\]\\\\ `"$?&<>$*()|{};';

  unless (defined $_) {
    die "@_";
    @x = caller;
    die "@x";
  }
  if (s/[\000-\037\200-\237\241-\250]/?/g or /\'/) {
    s/([$mc])/\\$1/g;
    s/^~/\\~/;
  } elsif (/[$mc]/) {
    $_ = "'$_'";
  }
  return $_;
}


sub checkvdir {
  my $vdir = shift;
  my $mode = shift||'';

  if (-d $vdir) {
    if (not -x $vdir or not -r $vdir) {
      die "$prg: cannot read $vdir\n";
    }
    if ($mode eq 'w' and not -w $vdir) {
      die "$prg: cannot write to $vdir\n";
    }
  }
}


# file regexp match
sub fmatch {
  my $m = shift;
  my $dir = dirname($m);
  my $fre = basename($m);
  my @match = ();

  $dir =~ s/\\//g;

  if (opendir $dir,$dir) {
    while (defined(my $file = readdir($dir))) {
      next if $file eq '.' or $file eq '..';
        if ($file =~ /^$fre$/) {
        if ($dir eq '.') {
          push @match,$file;
        } else {
          push @match,"$dir/$file";
        }
      }
    }
    closedir $dir;
  }

  return sort(@match);
}


sub d3size {
  $file = shift;
  if (-f $file and not -l $file) {
    return d3(-s $file);
  } else {
    return '-';
  }
}


sub update {
  $0 = abs_path($0);
  die "vv: cannot write $0\n" unless -w $0;
  my $url = 'https://fex.belwue.de/fstools/bin/vv';
  warn "vv: downloading $url\n";
  my $new = `wget -qO- $url`;
  if ($new !~ /visual versioning/) { die "vv: bad update\n" }
  if ($new =~ /version = (\d+)/ and $1 le $version) {
    warn "vv: version $version is up-to-date\n";
    exit;
  }
  system qw'vv -s',$0;
  exit $? if $?;
  open $0,'>',$0 or die "vv: cannot write $0 - $!\n";
  print {$0} $new;
  close $0;
  exit;
}


sub vars {
  my $a = shift;

  $file = realfilename($a);
  $ofile = "$file~";
  $bfile = basename($file);
  $dir = dirname($file);
  $vdir = norm("$dir/.versions");
  $vfile = "$vdir/$bfile";
  $vfile0 = "$vfile~00~";
  $vfile1 = "$vfile~01~";
}


sub vsystem {
  print "\$ @_\n" if $opt_v;
  return system @_;
}


sub slurp {
  my $file = shift;
  local $/;
  local $_ = '';
  
  if (open $file,$file) {
    $_ = <$file>;
    close $file;
  }
  
  return $_;
}


# read one key from terminal in raw mode
sub ReadKey {
  my $key;
  local $SIG{INT} = sub { stty('reset'); exit };

  stty('raw');
  # loop necessary for ESXi support
  $key = getc(STDIN) while not defined $key;
  stty('reset');
  return $key;
}


sub stty {
  if (shift eq 'raw') {
    system qw'stty -echo -icanon eol',"\001";
  } else {
    system qw'stty echo icanon eol',"\000";
  }
}


sub install {
  my ($dir);
  local $| = 1;

  $0 = abs_path($0);

  print "Installation directory [.]: ";
  $dir = <STDIN>||'';
  chomp $dir;
  $dir =~ s:/+$::;
  $dir ||= '.';
  if ($dir eq '.') {
    if ($0 =~ /vvzz$/) {
      unlink qw'zz ezz del vv tt';
      rename $0,'vv'    or die "$0: cannot create vv - $!\n";
    }
    symlink 'vv','zz'   or die "$0: cannot create zz - $!\n";
    symlink 'vv','tt'   or die "$0: cannot create tt - $!\n";
    symlink 'vv','ezz'  or die "$0: cannot create ezz - $!\n";
    symlink 'vv','del'  or die "$0: cannot create del - $!\n";
  } else {
    if (not -e $dir) { die "$0: $dir does not exist\n" }
    if (not -d $dir) { die "$0: $dir is not a directory\n" }
    if (not -w $dir) { die "$0: $dir is not writable\n" }
    chdir $dir or die "$0: cannot cd $dir - $!\n";
    unlink qw'zz ezz del vv tt';
    system qw'rsync -a',$0,'vv';
    exit $? if $?;
    symlink 'vv','zz'  or die "$0: cannot create $dir/zz - $!\n";
    symlink 'vv','tt'  or die "$0: cannot create $dir/tt - $!\n";
    symlink 'vv','ezz' or die "$0: cannot create $dir/ezz - $!\n";
    symlink 'vv','del' or die "$0: cannot create $dir/del - $!\n";
  }
  print "Installation completed. See:\n";
  print "$dir/vv -h\n";
  print "$dir/zz -h\n";
  print "$dir/tt -h\n";
  print "$dir/ezz -h\n";
  print "$dir/del -h\n";
  exit;
}
