123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #!/usr/bin/perl
-
- # ============================================
- # mklinks
- # --------------------------------------------
- #
- # usage: mklinks DOTROOT [PREFIX]
- #
- #
- # This script will go through each file or
- # sub-folder of DOTROOT and create symbolic
- # link to it under your HOME named with "."
- # prefix.
- #
- # This is useful for sharing your dotfiles
- # (e.g. using service like Dropbox or keeping
- # them in a shared VCS repository like git)
- #
- # DOTROOT - path where your shared dot-files
- # are.
- # PREFIX - where you want your links to
- # dot-files to be created ($HOME
- # by default)
- #
- # --------------------------------------------
- # By Alois Mahdal; no warranty
- # ============================================
-
-
- use strict;
- use warnings;
- use Getopt::Long;
- use File::Spec;
-
- sub usage { print STDERR "usage: $0: DOTROOT [PREFIX]\n"; exit 1; }
-
- my $dotroot;
- my $prefix;
- my @masks;
-
- my $opts = GetOptions(
- 'dotroot=s' => \$dotroot,
- 'prefix=s' => \$prefix,
- 'masks=s' => \@masks
- ) or usage;
-
- @masks = @masks ? @masks : qw ( .config .local/share );
-
- $dotroot = $dotroot // shift;
- $prefix = $prefix // shift;
- $prefix = $prefix // $ENV{HOME};
- $dotroot or usage;
-
- sub mklinks {
- my $dotroot = shift;
- my $mask = shift;
- $dotroot =~ s|/?$||;
- if ($mask) {
- $dotroot .= $mask;
- }
- if (not -d $dotroot) { warn "not a directory: $dotroot\n"; return }
- my @dirs = `ls $dotroot`;
- foreach my $dir (@dirs) {
- chomp $dir;
- my $source = File::Spec->rel2abs("$dotroot/$dir");
- my $target;
- if ($mask) {
- $target = "$prefix/$mask/$dir";
- `mkdir -p $prefix/$mask`;
- } else {
- $target = "$prefix/.$dir";
- `mkdir -p $prefix`;
- }
- if (-e $target) {
- warn "target exists: $target\n";
- } else {
- # $source = File::Spec->abs2rel($source, $target);
- `ln -sr $source $target`;
- }
- }
- }
-
- mklinks($dotroot);
- mklinks($dotroot, $_) foreach (@masks);
|