#!/usr/bin/perl -w
#
# -----------------------------------------------------------------------------
#
# findbrokensymlinks -- generate report for broken symlinks in a directory tree
# Copyright (C) 2003 Fabian "zzznowman" Pietsch
# Licensed under GPLv2 or, at your option, any later version
#
# -----------------------------------------------------------------------------
#
# Version 0.1.3 (2003-12-12)
#

use strict;
use File::Find;

# scalars for statistical evaluation
#   (explicitly initialized to 0 in case they're never incremented,
#   which would otherwise lead to "Use of uninitialized value" when printing)
my ($stats_link, $stats_broken) = (0, 0);

# search directories given as arguments (or the current directory, if none)
find(sub {
	# only consider symlinks
	return unless (-l);
	$stats_link++;

	# does symlink's target exist?
	unless (-e) {
		print("$File::Find::name\n",
		      "  to " . readlink() . "\n");
		$stats_broken++;
	}
  }, scalar(@ARGV) ? @ARGV : '.');

# output summary
print("\n",
      "summary:\t$stats_broken/$stats_link symlinks broken\n");

# set exit code appropriately
exit(0) if ($stats_broken);	# findbrokensymlinks found some
exit(1) if ($stats_link);	# no symlinks found that are broken
exit(2);			# no symlinks found at all

