]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
Use "ax-common" when available, implement wrapper functions
[ax-zsh.git] / bin / axzshctl
1 #!/bin/zsh
2 #
3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015 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 "$NAME <command> [...]"
27         echo
28         echo "  enable-plugin <p> [<p> [...]]"
29         echo "    Enable plugin(s)."
30         echo
31         echo "  disable-plugin <p> [<p> [...]]"
32         echo "    Disable plugin(s)."
33         echo
34         echo "  reset-plugins"
35         echo "    Reset active plugins to the default list."
36         echo
37         exit 2
38 }
39
40 function EnablePlugin {
41         local dir="$AXZSH/active_plugins"
42
43         if [[ -h "$dir/$1" ]]; then
44                 ax_msg 1 "Plugin \"$1\" already active!"
45                 return 1
46         fi
47
48         for dname (
49                 "$AXZSH_PLUGIN_D/$plugin"
50                 "$ZSH_CUSTOM/$plugin"
51                 "$AXZSH/plugins/$plugin"
52                 "$AXZSH/default_plugins/$plugin"
53                 "$AXZSH/core/$plugin"
54         ); do
55                 [[ ! -d "$dname" ]] && continue
56                 mkdir -p "$dir"
57                 (
58                         cd "$dir" || exit 9
59                         ln -sv "$dname" "$PWD"
60                 )
61                 return $?
62         done
63
64         ax_msg 2 "Plugin \"$1\" not found!"
65         return 1
66 }
67
68 function DisablePlugin {
69         local dir="$AXZSH/active_plugins"
70
71         if [[ ! -h "$dir/$1" ]]; then
72                 ax_msg 1 "Plugin \"$1\" not active?"
73                 return 1
74         fi
75
76         rm -v "$dir/$1"
77         return $?
78 }
79
80 function ResetPlugins {
81         local dir="$AXZSH/active_plugins"
82
83         if [[ -e "$dir" ]]; then
84                 ax_msg - "Removing all symbolic links in $dir ..."
85                 find "$dir" -type l -print -delete
86         fi
87
88         ax_msg - "Activating (linking) default plugins ..."
89         mkdir -p "$dir"
90         (
91                 cd "$dir" || exit 9
92                 ln -sv "$AXZSH/default_plugins/"* "$PWD"
93         )
94         return $?
95 }
96
97 NAME="$(basename "$0")"
98
99 [[ $# -gt 0 ]] || Usage
100
101 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
102         ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
103         exit 3
104 fi
105
106 cmd="$1"
107 shift
108
109 case "$cmd" in
110         "enable-plugin")
111                 [[ $# -gt 0 ]] || Usage
112                 for plugin in "$@"; do
113                         EnablePlugin "$plugin"
114                 done
115                 ;;
116         "disable-plugin")
117                 [[ $# -gt 0 ]] || Usage
118                 for plugin in "$@"; do
119                         DisablePlugin "$plugin"
120                 done
121                 ;;
122         "reset-plugins")
123                 [[ $# -eq 0 ]] || Usage
124                 ResetPlugins
125                 ;;
126         *)
127                 Usage
128 esac