#!/usr/bin/perl

use strict;
my $DEBUG = 0;
my $h = '';
my $n = '';
my $s = '';
my $t = '';
my $b = '';
my $d = '';
my $cmd = '';

use Proc::PID::File;
if (Proc::PID::File->running()){
  print "fixbackuplinks already running\n";
  exit();
}

# create daily/weekly dirs if they don't exist
foreach my $dir  ("daily", "weekly", "monthly") {
  unless ( -e "/backup/cpbackup/$dir" ){
    $cmd = "mkdir -p /backup/cpbackup/$dir";
    system("$cmd") == 0 or print "fail: $!\n";
  }
}

# Make sure we only have symlinks in /backup/
symlinkcheck();

my %mnts;

foreach my $user (`ls /var/cpanel/users`) {
  chomp $user;
  next if ($user =~ /(^\.|^root$)/);
  $h = (getpwnam($user))[7];
  $n = '';
  $n = $1 if ($h =~ /\/home(\d+)\//);
  $mnts{"/backup$n"} = 1;
  if ($n) {
    next unless( -e "/backup$n/cpbackup" );
    foreach my $top ('archived-backups', 'cpbackup') {
      foreach my $back ("daily", "weekly", "monthly") {
        $s = "/backup$n/$top/$back/$user";
        $t = "/backup/$top/$back/$user";
        if ( -e $s && !-l $t ) {
          print "$t missing\n";
          $cmd = "ln -s $s $t";
          system("$cmd") == 0 or print "fail: $!\n";
        }
      }
    }
  }
}

sub runcmd {
  my $cmd = shift;
  print "$cmd\n" if $DEBUG;
  system("$cmd") == 0 or print "fail: $!\n";
}

sub symlinkcheck {
    use Sys::Syslog;
    use File::Slurp;
    my $symdir = '/backup';
    if ( `/bin/mountpoint /backup` =~ m/ is a mountpoint$/ || system("[ -w /backup ]") != 0 ) {
        #return if /backup is not just a collection of symlinks
        return;
    }
    my @dirs = qw( daily weekly monthly );
    my $gy = '/GRAVEYARD';

    foreach my $top ("archived-backups", "cpbackup") {
        foreach my $dir (@dirs) {
            system("mkdir -p $symdir/$top/$dir") if ( !-e "$symdir/$top/$dir" );
            my @files = read_dir("$symdir/$top/$dir");
            foreach my $file (@files) {
                # Remove broken symlinks
                if ( -l "$symdir/$top/$dir/$file" ) {
                    my $link = readlink("$symdir/$top/$dir/$file");
                    my $target_dir = $1 if ( $link =~ m/^(\/backup\d*\/$top\/$dir)\/.*/ );
                    unless ( -e $link ) {
                        syslog("info", "Found broken symlink: $symdir/$top/$dir/$file . unlinking...");
                        print "Found broken symlink: $symdir/$top/$dir/$file . unlinking...\n";
                        unlink("$symdir/$top/$dir/$file");
                    }
                    next;
                }
                # Move anything that is not a symlink to /GRAVEYARD
                unless ( -l "$symdir?$top/$dir/$file" ) {
                    syslog("info", "$symdir/$top/$dir/$file is not a symlink, moving to /GRAVEYARD");
                    print "$symdir/$top/$dir/$file is not a symlink, moving to /GRAVEYARD...\n";
                    system("/bin/mkdir", "$gy") unless -e $gy;
                    system("/bin/mv", "-f", "$symdir/$top/$dir/$file", "$gy/") == 0 or
                        warn "Something went wrong with the move: $symdir/$top/$dir/$file";
                }
            }
        }
    };
    return;
}
