~hokiegeek/dotfiles

ref: bb95ccbbd653fccb856a24b2e0ac62fb1a9e6c86 dotfiles/setup.sh -rwxr-xr-x 2.1 KiB
bb95ccbbHokieGeek This thing.... 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash

[ $# -gt 0 ] && destinationDir=$1 || destinationDir=$HOME
dotfilesDir=$(cd $(dirname $0); pwd)
configDir="${destinationDir}/.config"

echo "Destination directory: $destinationDir"

cd $dotfilesDir

doLink() {
    dfpath=$1
    target=$2
    df=`basename ${dfpath}`
    # echo "doLink($1, $2)"

    # Output what is being linked
    echo -n "Linking "
    [ -d ${dfpath} ] && echo -n "directory " || echo -n "file "
    [ $target = $configDir ] && echo -n "config/"
    echo "${df}"

    [ "${target}" = "${configDir}" ] && trueTarget=${target}/${df} || trueTarget=$target
    # echo "tT = $trueTarget"

    ## If a file already exists with this name
    # If it's a symlink
    [ -L ${trueTarget} ] && {
        # Ignore if it's just a link to the respective dotfiles file. Ask to delete it otherwise
        foundTarget="`ls -la ${trueTarget} | awk -F'->' '{ print $2 }' | cut -d' ' -f2`"
        # echo "fT = $foundTarget"
        [ $foundTarget = "${dfpath}" ] && {
            echo "  Already exists"
            continue
        } || {
            echo -n "   "
            rm -i ${trueTarget}
        }
    }

    # If a file still exists (directory, symlink, whatever) then create a backup of it
    [ -e ${trueTarget} ] && {
        echo "  Backing up existing"
        mv ${trueTarget}{,.orig}
    }

    # Will not attempt to create the link if a file of the same name still exists after all of that
    [ -e ${trueTarget} ] && {
        echo "Did not create link for ${df}" 
        continue
    }

    #echo "$target" 
    #[ ! -d `dirname ${target}` ] && mkdir -p ${target}
    ln -s ${dfpath} ${target}
}

ignores='`basename $0` README.md *~'
for df in `ls -1`; do
    # If the file is in the ignores list, then just skip the rest
    [ `echo ${ignores} | grep -c "${df}"` -eq 0 ] || continue

    [ "$df" = "config" ] && {
        [ ! -d "${configDir}" ]  && mkdir ${configDir}
        # echo ">>>>>> ok: ${df}"
        for cf in `ls -1 ${df}`; do
            # echo "cf: $cf"
            doLink ${dotfilesDir}/${df}/${cf} ${configDir}
        done
    } || doLink ${dotfilesDir}/${df} "${destinationDir}/.${df}"
done
# ls ${destinationDir}*(-@)