mklink.pl revision 55714
155714Skris#!/usr/local/bin/perl
255714Skris
355714Skris# mklink.pl
455714Skris
555714Skris# The first command line argument is a non-empty relative path
655714Skris# specifying the "from" directory.
755714Skris# Each other argument is a file name not containing / and
855714Skris# names a file in the current directory.
955714Skris#
1055714Skris# For each of these files, we create in the "from" directory a link
1155714Skris# of the same name pointing to the local file.
1255714Skris#
1355714Skris# We assume that the directory structure is a tree, i.e. that it does
1455714Skris# not contain symbolic links and that the parent of / is never referenced.
1555714Skris# Apart from this, this script should be able to handle even the most
1655714Skris# pathological cases.
1755714Skris
1855714Skrismy $from = shift;
1955714Skrismy @files = @ARGV;
2055714Skris
2155714Skrismy @from_path = split(/\//, $from);
2255714Skrismy $pwd = `pwd`;
2355714Skrischop($pwd);
2455714Skrismy @pwd_path = split(/\//, $pwd);
2555714Skris
2655714Skrismy @to_path = ();
2755714Skris
2855714Skrismy $dirname;
2955714Skrisforeach $dirname (@from_path) {
3055714Skris
3155714Skris    # In this loop, @to_path always is a relative path from
3255714Skris    # @pwd_path (interpreted is an absolute path) to the original pwd.
3355714Skris
3455714Skris    # At the end, @from_path (as a relative path from the original pwd)
3555714Skris    # designates the same directory as the absolute path @pwd_path,
3655714Skris    # which means that @to_path then is a path from there to the original pwd.
3755714Skris
3855714Skris    next if ($dirname eq "" || $dirname eq ".");
3955714Skris
4055714Skris    if ($dirname eq "..") {
4155714Skris	@to_path = (pop(@pwd_path), @to_path);
4255714Skris    } else {
4355714Skris	@to_path = ("..", @to_path);
4455714Skris	push(@pwd_path, $dirname);
4555714Skris    }
4655714Skris}
4755714Skris
4855714Skrismy $to = join('/', @to_path);
4955714Skris
5055714Skrismy $file;
5155714Skrisforeach $file (@files) {
5255714Skris#    print "ln -s $to/$file $from/$file\n";
5355714Skris    symlink("$to/$file", "$from/$file");
5455714Skris    print $file . " => $from/$file\n";
5555714Skris}
56