1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #!/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 {
- `ln -s $source $target`;
- }
- }
- }
-
- mklinks($dotroot);
- mklinks($dotroot, $_) foreach (@masks);
|