#!/usr/bin/perl ### ### by Scott Lawrence @INC = ( @INC, "/localib/gnutools/contrib/perl" ); require "getargs.pl"; ( $myName = $0 ) =~ s|^.*/([^/]+)$|\1|; $Verbose = 0; $Quiet = 0; $Force = 0; &getargs( '-', 'v', 0, 'Verbose' ,'-', 'q', 0, 'Quiet' ,'-', 'f', 0, 'Force' ,'m', 'new-target', 1, 'NewTarget' ,'m', 'link-name', 1, 'LinkName' ) || exit; $Feedback = $Verbose ? "VERBOSE" : ( $Quiet ? "QUIET" : "NORMAL" ); if ( ! -e $NewTarget ) { $Force || die "$myName: '$NewTarget' does not exist; aborting\n"; ( $Feedback eq "QUIET" ) || warn "$myName: '$NewTarget' does not exist; proceeding anyway\n"; } if ( -e $LinkName ) { ## ## LinkName exists ## if ( ! -l $LinkName ) { ## ## LinkName exists and is not a symbolic link ## if ( -d $LinkName ) { ( $Feedback eq "VERBOSE" ) && warn "$myName: '$LinkName' is a directory - creating link within it\n"; ( $NewBase = $NewTarget ) =~ s|^.*/||; ( $LinkName = "$LinkName/$NewBase" ) =~ s|//|/|; } elsif ( $Force ) { ( $Feedback eq "VERBOSE" ) && warn "$myName: '$LinkName' is not a link - proceeding anyway\n"; $OldValue = $LinkName; } else { die "$myName: '$LinkName' is not a link - aborted\n"; } } else { ## ## LinkName exists and is a symbolic link ## $OldValue = readlink( $LinkName ); if ( $OldValue eq $NewTarget ) { ( $Feedback eq "VERBOSE" ) && warn "$myName: $LinkName already -> $NewTarget\n"; exit 0; } } unlink $LinkName; } elsif ( -l $LinkName ) { ## ## LinkName exists, but is a 'dangling' link ## unlink $LinkName; } symlink( $NewTarget, $LinkName ) || die "$myName: Failed to create link\n\t$!\n"; ( $Feedback eq "QUIET" ) || warn " $LinkName -> $NewTarget\n";