aboutsummaryrefslogtreecommitdiffstats
path: root/_bin/disable.sh
blob: eb4549f65bc3b26667d9d52dc7175ca0c04c1a07 (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
#!/bin/sh
#
# Disable specified dotfiles by removing the symbolic links of the dotfiles
# from home directory.
#
# Aaron LI
# Created: 2015-01-06
# Updated: 2015-01-06
#

usage() {
    echo "Usage:"
    echo "    `basename $0` [ -fhknv ] [ -H <home> ] <df1> ..."
    echo ""
    echo "    -h: show this help"
    echo "    -f: force remove target link even it is not the link to the source dotfile"
    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 '..'"
}

disable_dotfile() {
    df="$1"
    home="$2"
    df_abs=`realpath ${df}`
    if [ "x${arg_keep_us}" = "xyes" ]; then
        target_df="${df}"
    else
        target_df=`echo "${df}" | sed 's|^_|.|'`
    fi
    ( cd ${home}; target_df_abs=`realpath "${target_df}"`; \
        if [ ! -e "${target_df}" ]; then \
            echo "WARNING: target file do NOT exist"; \
        elif [ "x${arg_force}" = "xyes" ] || [ "x${target_df_abs}" = "x${df_abs}" ]; then \
            eval ${disable_cmd} "${target_df}"; \
        else \
            echo "WARNING: target is NOT a link to source dotfile"; \
        fi; \
    )
}


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

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

# should NOT use "$@" here
args=`getopt fhH:knv $*`
if [ $? != 0 ]; then
    usage
    exit 1
fi
set -- ${args}
for i; do
    case "$i" in
        -h)
            usage
            exit 0;;
        -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 "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_rm="-f"
[ "x${arg_verbose}" = "xyes" ] && arg_rm="${arg_rm} -v"
disable_cmd="rm ${arg_rm}"
if [ "x${arg_dry}" = "xyes" ]; then
    disable_cmd="echo DRY_RUN: ${disable_cmd}"
fi

for dotfile in $@; do
    for df in `find "${dotfile}" \( -type f -o -type l \)`; do
        # strip the beginning './'
        df=`echo "${df}" | sed 's|^\./||'`
        echo "disabling: '${df}'"
        disable_dotfile "${df}" "${arg_home}"
    done
done

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