My dotfiles. Period.

mklinks 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/perl
  2. # ============================================
  3. # mklinks
  4. # --------------------------------------------
  5. #
  6. # usage: mklinks DOTROOT [PREFIX]
  7. #
  8. #
  9. # This script will go through each file or
  10. # sub-folder of DOTROOT and create symbolic
  11. # link to it under your HOME named with "."
  12. # prefix.
  13. #
  14. # This is useful for sharing your dotfiles
  15. # (e.g. using service like Dropbox or keeping
  16. # them in a shared VCS repository like git)
  17. #
  18. # DOTROOT - path where your shared dot-files
  19. # are.
  20. # PREFIX - where you want your links to
  21. # dot-files to be created ($HOME
  22. # by default)
  23. #
  24. # --------------------------------------------
  25. # By Alois Mahdal; no warranty
  26. # ============================================
  27. use strict;
  28. use warnings;
  29. use Getopt::Long;
  30. use File::Spec;
  31. sub usage { print STDERR "usage: $0: DOTROOT [PREFIX]\n"; exit 1; }
  32. my $dotroot;
  33. my $prefix;
  34. my @masks;
  35. my $opts = GetOptions(
  36. 'dotroot=s' => \$dotroot,
  37. 'prefix=s' => \$prefix,
  38. 'masks=s' => \@masks
  39. ) or usage;
  40. @masks = @masks ? @masks : qw ( .config .local/share );
  41. $dotroot = $dotroot // shift;
  42. $prefix = $prefix // shift;
  43. $prefix = $prefix // $ENV{HOME};
  44. $dotroot or usage;
  45. sub mklinks {
  46. my $dotroot = shift;
  47. my $mask = shift;
  48. $dotroot =~ s|/?$||;
  49. if ($mask) {
  50. $dotroot .= $mask;
  51. }
  52. if (not -d $dotroot) { warn "not a directory: $dotroot\n"; return }
  53. my @dirs = `ls $dotroot`;
  54. foreach my $dir (@dirs) {
  55. chomp $dir;
  56. my $source = File::Spec->rel2abs("$dotroot/$dir");
  57. my $target;
  58. if ($mask) {
  59. $target = "$prefix/$mask/$dir";
  60. `mkdir -p $prefix/$mask`;
  61. } else {
  62. $target = "$prefix/.$dir";
  63. `mkdir -p $prefix`;
  64. }
  65. if (-e $target) {
  66. warn "target exists: $target\n";
  67. } else {
  68. # $source = File::Spec->abs2rel($source, $target);
  69. `ln -sr $source $target`;
  70. }
  71. }
  72. }
  73. mklinks($dotroot);
  74. mklinks($dotroot, $_) foreach (@masks);