mirror of https://github.com/Chizi123/Dotfiles.git

Joel Grunbaum
2021-12-24 d2c44c5903537b478900c30c8255fc9d64549b6e
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/sh
 
install_links() {
    . ./DICT
    if [ -n "$FILES" ]; then
    for i in $(seq 1 "$(echo "$FILES" | wc -w)"); do
        file=$(echo "$FILES" | cut -d' ' -f "$i")
        loc=$(echo "$LOCATIONS" | cut -d' ' -f "$i")
        while true; do
            CHOICE="overwrite"
            if [ -e "$loc" ] && [ "$FORCE" = "0" ]; then
                echo "WARNING: \"$loc\" exists, (overwrite, change, nothing): "
                read CHOICE
            fi
            case "$CHOICE" in
                o|overwrite)
                    dir=$(dirname "$loc")
                    mkdir -p "$dir"
                    ln -sf "$(pwd)/$file" "$loc"; break;;
                c|change)
                    tmpfile=$(mktemp)
                    echo "$loc" >> "$tmpfile"
                    $EDITOR "$tmpfile"
                    loc=$(cat "$tmpfile")
                    rm "$tmpfile"
                    unset tmpfile;;
                n|nothing)
                    break;;
                *)
                    echo "INVALID CHOICE \"$CHOICE\", (overwrite, change, nothing)"
                    read CHOICE
            esac
        done
    done
    fi
    if [ -n "$SUDO_FILES" ]; then
    for i in $(seq 1 "$(echo "$SUDO_FILES" | wc -w)"); do
        file=$(echo "$SUDO_FILES" | cut -d' ' -f "$i")
        loc=$(echo "$SUDO_LOCATIONS" | cut -d' ' -f "$i")
        dir=$(echo "$loc" | rev | cut -d'/' -f2- | rev)
        sudo mkdir -p "$dir"
        sudo ln -sf "$(pwd)/$file" "$loc"
    done
    fi
    if type custom | grep -q "function" ; then
        custom install
    fi
}
 
remove_links() {
    . ./DICT
    if [ -n "$FILES" ]; then
    for i in $(seq 1 "$(echo "$FILES" | wc -w)"); do
        loc=$(echo "$LOCATIONS" | cut -d' ' -f "$i")
        rm "$loc" 2>/dev/null
        loc=$(dirname "$loc")
        while [ -z "$(ls -A "$loc")" ]; do
        rm -r "$loc"
        loc=$(dirname "$loc")
        done 2>/dev/null
    done
    fi
    if [ -n "$SUDO_FILES" ]; then
    for i in $(seq 1 "$(echo "$SUDO_FILES" | wc -w)"); do
        loc=$(echo "$SUDO_LOCATIONS" | cut -d' ' -f "$i")
        sudo rm "$loc" 2>/dev/null
        loc=$(echo "$loc" | rev | cut -d'/' -f2- | rev)
        while [ -z "$(ls -A "$loc")" ]; do
        sudo rm -r "$loc"
        loc=$(echo "$loc" | rev | cut -d'/' -f2- | rev)
        done 2>/dev/null
    done
    fi
    if type custom | grep -q "function"; then
        custom remove
    fi
}
 
handle_package() {
    echo "$1"
    if [ -d "$1" ]; then
    unset DEPS
    eval "$(grep "DEPS=" "$1/DICT")"
    if ! [ "$INSTALL" = "0" ] && [ "$REMOVE_DEPS" = "0" ]; then
        for i in $DEPS; do
            if ! grep -q "$i" "$PACKAGE_CACHE"; then
                (handle_package "$i")
            fi
        done
    fi
    echo "$1" >> "$PACKAGE_CACHE"
    (cd "$1" || exit; "$([ "$INSTALL" = "1" ] && echo install || echo remove)_links")
    else
    echo "No configuration found for $i"
    fi
}
 
usage() {
    echo "Install dotfiles with symlinks"
    echo "Usage with -h|--help"
    echo "Install with -i|--install (default)"
    echo "Remove with -r|--remove"
    echo "Force install with -f|--force"
    echo "Directories have the configurations for their programs/use cases"
    echo "List all configurations to be installed in the command line"
    echo "If there is a file at the install location, you will be prompted to change it"
    echo "Example usage: $0 -i bash zsh"
}
 
INSTALL=1
FORCE=0
REMOVE_DEPS=0
PACKAGE_CACHE="$(mktemp)"
DOTFILES_PATH="$(dirname "$0")"
 
cd "$DOTFILES_PATH" || exit
 
if [ -z "$1" ]; then
    usage
    exit
fi
 
while [ -n "$1" ]; do
      case "$1" in
      -h|--help|"") usage; exit;;
      -i|--install) INSTALL=1;;
      -r|--remove) INSTALL=0;;
      -d|--deps) REMOVE_DEPS=1;;
      -f|--force) FORCE=1;;
      --) shift; break;;
      -*) echo "Invalid argument"; usage; exit;;
      *) handle_package "$1" "$([ \"$INSTALL\" = \"1\" ] && echo install || echo remove)_links";;
      esac
      shift
done
 
while [ -n "$1" ]; do
    handle_package "$1"
done
 
rm "$PACKAGE_CACHE"