summaryrefslogtreecommitdiff
path: root/bin/checklinks
diff options
context:
space:
mode:
authorDaniel Friesel <derf@derf.homelinux.org>2008-05-19 20:18:58 +0200
committerDaniel Friesel <derf@derf.homelinux.org>2008-05-19 20:18:58 +0200
commitedf8df64f7a5c123f8d64e0afd9784563f9da886 (patch)
tree1488ab6e89313773bc1d55a820763beb4f583ac6 /bin/checklinks
initial commit
Diffstat (limited to 'bin/checklinks')
-rwxr-xr-xbin/checklinks90
1 files changed, 90 insertions, 0 deletions
diff --git a/bin/checklinks b/bin/checklinks
new file mode 100755
index 0000000..2f03551
--- /dev/null
+++ b/bin/checklinks
@@ -0,0 +1,90 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Term::ANSIColor;
+
+my $base = $ENV{HOME};
+my ($type, $src, $dst);
+my $quiet = 0;
+
+unless (-f ".links") {
+ exit 0;
+}
+
+if (defined($ARGV[0]) and $ARGV[0] eq '-q') {
+ $quiet = 1;
+}
+
+open(LINKS, '<.links') or die($!);
+while(<LINKS>) {
+ chomp;
+ ($type, $src, $dst) = split(/ +/);
+ if ($type eq 'soft') {
+ check_symlink($src, $dst);
+ } elsif ($type eq 'hard') {
+ check_hardlink($src, $dst);
+ }
+}
+close(LINKS);
+
+sub check_symlink {
+ my $src = shift;
+ my $dst = shift;
+
+ #if (not -e "$base/$dst") {
+ # print_format('no dst!!', $src, $dst, 'red bold');
+ #}
+ if (not -l "$base/$src" and not -e "$base/$src") {
+ symlink($dst, "$base/$src");
+ print_format('created', $src, $dst, 'cyan');
+ }
+ elsif (readlink("$base/$src") eq $dst) {
+ print_format('ok', $src, $dst, 'green') unless ($quiet);
+ }
+ elsif (readlink("$base/$src") eq "$base/$dst") {
+ print_format('absolute', $src, $dst, 'yellow') unless ($quiet);
+ }
+ elsif (not -l "$base/$src" and -e "$base/$src") {
+ print colored ("$base/$src: File exists but is not a symlink. Not updating.\n", 'bold red');
+ }
+ elsif (-l "$base/$src") {
+ unlink("$base/$src");
+ symlink($dst, "$base/$src");
+ print_format('fixed', $src, $dst, 'cyan');
+ }
+}
+
+sub check_hardlink {
+ my $src = shift;
+ my $dst = shift;
+
+ if (not -e "$base/$dst") {
+ print_format('no dst!!', $src, $dst, 'red bold');
+ }
+ elsif (not -f "$base/$src") {
+ link("$base/$dst", "$base/$src") or warn($!);
+ print_format('created', $src, $dst, 'cyan');
+ }
+ elsif ((stat("$base/$src"))[1] != (stat("$base/$dst"))[1]) {
+ unlink("$base/$src");
+ link("$base/$dst", "$base/$src") or warn($!);
+ print_format('updated', $src, $dst, 'cyan');
+ }
+ elsif ((stat("$base/$src"))[1] == (stat("$base/$dst"))[1]) {
+ print_format('ok', $src, $dst, 'green') unless ($quiet);
+ }
+}
+
+sub print_format {
+ my ($message, $src, $dst, $color) = @_;
+
+ $message .= ' 'x(9-length($message));
+ $src .= ' 'x(15-length($src));
+ $dst .= ' 'x(15-length($dst));
+ if (defined($color)) {
+ print colored ($message, $color);
+ } else {
+ print $message;
+ }
+ print "$src -> $dst\n";
+}