]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
2016! :-)
[ax-zsh.git] / bin / axzshctl
1 #!/bin/zsh
2 #
3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2016 Alexander Barton <alex@barton.de>
5 #
6
7 # Include "ax-common.sh", if available:
8 for dir ("$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr); do
9         [[ -z "$ax_common_sourced" ]] || break
10         ax_common="${dir}/lib/ax/ax-common.sh"
11         [[ -r "$ax_common" ]] && source "$ax_common"
12 done
13 if [[ -z "$ax_common_sourced" ]]; then
14         function ax_msg {
15                 case "$1" in
16                   "1"|"2") echo -n "! "; ;;
17                   *) echo -n "* "; ;;
18                 esac
19                 shift
20                 echo "$@"
21         }
22 fi
23 unset dir ax_common ax_common_sourced
24
25 function Usage {
26         echo "Usage: $NAME <command> [...]"
27         echo
28         echo "  enable-plugin <name|directory> [<name|directory> [...]]"
29         echo "    Enable plugin(s)."
30         echo
31         echo "  disable-plugin <name> [<name> [...]]"
32         echo "    Disable plugin(s)."
33         echo
34         echo "  reset-plugins"
35         echo "    Reset active plugins to the default set."
36         echo
37         echo "  enable-default-plugins"
38         echo "    Enable all default plugins."
39         echo
40         echo "  set-theme <name>|-"
41         echo "    Set active theme to <name>, or to the default."
42         echo
43         echo "  upgrade"
44         echo "    Upgrade AX-ZSH installation (requires Git)."
45         echo
46         exit 2
47 }
48
49 function EnablePlugin {
50         local dir="$AXZSH/active_plugins"
51
52         if [[ -h "$dir/$1" ]]; then
53                 ax_msg 1 "Plugin \"$1\" already active!"
54                 return 1
55         fi
56
57         for dname (
58                 "$plugin:A"
59                 "$AXZSH_PLUGIN_D/$plugin"
60                 "$ZSH_CUSTOM/$plugin"
61                 "$AXZSH/custom_plugins/$plugin"
62                 "$AXZSH/plugins/$plugin"
63                 "$AXZSH/default_plugins/$plugin"
64                 "$AXZSH/core/$plugin"
65         ); do
66                 [[ ! -d "$dname" ]] && continue
67                 mkdir -p "$dir"
68                 (
69                         cd "$dir" || exit 9
70                         ln -sv "$dname" "$PWD"
71                 )
72                 return $?
73         done
74
75         ax_msg 2 "Plugin \"$1\" not found!"
76         return 1
77 }
78
79 function DisablePlugin {
80         local dir="$AXZSH/active_plugins"
81
82         if [[ ! -h "$dir/$1" ]]; then
83                 ax_msg 1 "Plugin \"$1\" not active?"
84                 return 1
85         fi
86
87         rm -v "$dir/$1"
88         return $?
89 }
90
91 function ResetPlugins {
92         local dir="$AXZSH/active_plugins"
93
94         if [[ -e "$dir" ]]; then
95                 ax_msg - "Removing all symbolic links in $dir ..."
96                 find "$dir" -type l -print -delete
97         fi
98         return $?
99 }
100
101 function EnableDefaultPlugins {
102         local dir="$AXZSH/active_plugins"
103
104         ax_msg - "Activating (linking) default plugins ..."
105         mkdir -p "$dir"
106         (
107                 cd "$dir" || exit 9
108                 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
109         )
110         return $?
111 }
112
113 function SetTheme {
114         local link_name="$AXZSH/active_theme"
115
116         if [ $# -ne 1 ]; then
117                 echo "Usage: axzsh_set_theme <name|->"
118                 return 1
119         fi
120
121         rm -f "$link_name" || return 1
122
123         if [ "$1" = "-" ]; then
124                 echo "Theme settings have been reset."
125                 return 0
126         fi
127
128         if [ -r "$1" ]; then
129                 theme="$1"
130         elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
131                 theme="$AXZSH/custom_themes/$1.axzshtheme"
132         elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
133                 theme="$AXZSH/themes/$1.axzshtheme"
134         else
135                 echo "Theme \"$1\" not found!"
136                 return 1
137         fi
138         ln -sv "$theme" "$link_name" || return 1
139         return $?
140 }
141
142 function UpgradeAXZSH {
143         if [[ $+commands[git] -eq 0 ]]; then
144                 ax_msg 2 "The git(1) command is not available!"
145                 return 1
146         fi
147         if [[ ! -d "$AXZSH/.git" ]]; then
148                 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
149                 return 1
150         fi
151
152         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
153         ( cd "$AXZSH" && git pull --ff-only )
154 }
155
156 NAME="$0:t"
157
158 [[ $# -gt 0 ]] || Usage
159
160 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
161         ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
162         exit 3
163 fi
164
165 cmd="$1"
166 shift
167
168 case "$cmd" in
169         "enable-plugin")
170                 [[ $# -gt 0 ]] || Usage
171                 for plugin in "$@"; do
172                         EnablePlugin "$plugin"
173                 done
174                 ;;
175         "disable-plugin")
176                 [[ $# -gt 0 ]] || Usage
177                 for plugin in "$@"; do
178                         DisablePlugin "$plugin"
179                 done
180                 ;;
181         "reset-plugins")
182                 [[ $# -eq 0 ]] || Usage
183                 ResetPlugins
184                 EnableDefaultPlugins
185                 ;;
186         "enable-default-plugins")
187                 [[ $# -eq 0 ]] || Usage
188                 EnableDefaultPlugins
189                 ;;
190         "set-theme")
191                 [[ $# -eq 1 ]] || Usage
192                 SetTheme "$1"
193                 ;;
194         "upgrade")
195                 [[ $# -eq 0 ]] || Usage
196                 UpgradeAXZSH
197                 ;;
198         *)
199                 Usage
200 esac