#!/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 $opts = GetOptions(
    'dotroot=s' =>  \$dotroot,
    'prefix=s' =>   \$prefix
) or usage;

$dotroot = $dotroot // shift;
$prefix = $prefix // shift;
$prefix = $prefix // $ENV{HOME};
$dotroot or usage;

sub mklinks {
    my $dotroot = shift;
    -d $dotroot or die "not a directory: $dotroot";
    my @dirs = `ls $dotroot`;
    foreach my $dir (@dirs) {
        chomp $dir;
        my $source = File::Spec->rel2abs("$dotroot/$dir");
        my $target = "$prefix/.$dir";
        `mkdir -p $prefix`;
        if (-e $target) {
            warn "target exists: $target\n";
        } else {
            `ln -s $source $target`;
        }
    }
}

mklinks($dotroot);