aboutsummaryrefslogtreecommitdiffstats
path: root/_bin/enable.sh
blob: b13bfae8fe1efb4aa0afef21f6ad8adb97d5f12a (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh
#
# Enalbe specified dotfiles by linking the dotfiles to home directory.
#
# Aaron LI
# Created: 2015-01-06
# Updated: 2015-01-06
#

usage() {
    echo "Usage:"
    echo "    `basename $0` [ -Ffhknv ] [ -H <home> ] <df1> ..."
    echo ""
    echo "    -h: show this help"
    echo "    -F: do not use find to list files, take the df* as is"
    echo "    -f: overwrite target links of already exists"
    echo "    -H: use the specified target home instead of $HOME"
    echo "    -k: keep the beginning underscore '_'"
    echo "    -n: dry-run"
    echo "    -v: verbose"
    echo "    df*: dotfiles or directory; NOTE: dot not use '.' or '..'"
}

enable_dotfile() {
    df="$1"
    home="$2"
    df_abs=`realpath ${df}`
    if [ "x${arg_keep_us}" = "xyes" ]; then
        df_dir=`dirname   ${df}`
        df_base=`basename ${df}`
    else
        df_dir=`dirname   ${df} | sed 's|^_|.|'`
        df_base=`basename ${df} | sed 's|^_|.|'`
    fi
    ( cd ${home}; \
        if [ ! -d "${df_dir}" ]; then mkdir -p "${df_dir}"; fi; \
        cd "${df_dir}"; curdir=`realpath .`; \
        df_rel=`${this_dir}/relpath.sh "${curdir}" "${df_abs}"`; \
        eval ${enable_cmd} "${df_rel}" "${df_base}"; \
    )
}


this=`realpath $0`
this_dir=`dirname ${this}`

# default arguments
arg_nofind=no
arg_force=no
arg_home="$HOME"
arg_keep_us=no
arg_dry=no
arg_verbose=no

# should NOT use "$@" here
args=`getopt FfhH:knv $*`
if [ $? != 0 ]; then
    usage
    exit 1
fi
set -- ${args}
for i; do
    case "$i" in
        -h)
            usage
            exit 0;;
        -F)
            arg_nofind=yes
            shift;;
        -f)
            arg_force=yes
            shift;;
        -H)
            arg_home="$2"; shift;
            shift;;
        -k)
            arg_keep_us=yes
            shift;;
        -n)
            arg_dry=yes
            shift;;
        -v)
            arg_verbose=yes
            shift;;
        --)
            shift; break;;
    esac
done

if [ $# -eq 0 ]; then
    usage
    exit 2
fi

echo "no_find: ${arg_nofind}"
echo "force: ${arg_force}"
echo "target_home: ${arg_home}"
echo "keep_underscore: ${arg_keep_us}"
echo "dry_run: ${arg_dry}"
#echo "dotfiles: $@"

[ "x${arg_force}"   = "xyes" ] && arg_ln="-f"
[ "x${arg_verbose}" = "xyes" ] && arg_ln="${arg_ln} -v"
enable_cmd="ln -s ${arg_ln}"
if [ "x${arg_dry}" = "xyes" ]; then
    enable_cmd="echo DRY_RUN: ${enable_cmd}"
fi

for dotfile in $@; do
    if [ "x${arg_nofind}" = "xyes" ]; then
        echo "enabling: '${dotfile}'"
        enable_dotfile "${dotfile}" "${arg_home}"
    else
        for df in `find "${dotfile}" \( -type f -o -type l \)`; do
            # strip the beginning './'
            df=`echo "${df}" | sed 's|^\./||'`
            echo "enabling: '${df}'"
            enable_dotfile "${df}" "${arg_home}"
        done
    fi
done

# vim: set ts=8 sw=4 tw=0 fenc=utf-8 ft=sh: #