1#!/bin/bash
2# ARG_OPTIONAL_SINGLE([path],[p],[Path to DokuWiki instance; overrides DW_PATH])
3# ARG_POSITIONAL_INF([extension],[Extensions to install; start templates with template:])
4# ARG_USE_ENV([DW_PATH],[./],[Path to DokuWiki instance])
5# ARG_USE_ENV([DW_EXTENSIONS],[],[Extensions to install, space-separated])
6# ARG_HELP([Install a plugin into a DokuWiki instance],[The DokuWiki instance must exist in the current directory, or be given via the\nenvironment variable DW_PATH, or as a command line option, which overrides the\nenvironment.\n \nExtensions can be given as command line arguments, or via the environment\nnvariable DW_EXTENSIONS, or both.])
7# ARGBASH_SET_INDENT([  ])
8# ARGBASH_GO()
9# needed because of Argbash --> m4_ignore([
10### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
11# Argbash is a bash code generator used to get arguments parsing right.
12# Argbash is FREE SOFTWARE, see https://argbash.io for more info
13# Generated online by https://argbash.io/generate
14
15# Setting environmental variables
16# Setting environmental variables
17
18
19die()
20{
21  local _ret="${2:-1}"
22  test "${_PRINT_HELP:-no}" = yes && print_help >&2
23  echo "$1" >&2
24  exit "${_ret}"
25}
26
27
28begins_with_short_option()
29{
30  local first_option all_short_options='ph'
31  first_option="${1:0:1}"
32  test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
33}
34
35# THE DEFAULTS INITIALIZATION - POSITIONALS
36_positionals=()
37_arg_extension=()
38# THE DEFAULTS INITIALIZATION - OPTIONALS
39_arg_path=
40
41
42print_help()
43{
44  printf '%s\n' "Install a plugin into a DokuWiki instance"
45  printf 'Usage: %s [-p|--path <arg>] [-h|--help] [<extension-1>] ... [<extension-n>] ...\n' "$0"
46  printf '\t%s\n' "<extension>: Extensions to install; start templates with template:"
47  printf '\t%s\n' "-p, --path: Path to DokuWiki instance; overrides DW_PATH (no default)"
48  printf '\t%s\n' "-h, --help: Prints help"
49  printf '\nEnvironment variables that are supported:\n'
50  printf '\t%s\n' "DW_PATH: Path to DokuWiki instance. (default: './')"
51  printf '\t%s\n' "DW_EXTENSIONS: Extensions to install, space-separated."
52
53  printf '\n%s\n' "The DokuWiki instance must exist in the current directory, or be given via the
54environment variable DW_PATH, or as a command line option, which overrides the
55environment.
56
57Extensions can be given as command line arguments, or via the environment
58nvariable DW_EXTENSIONS, or both."
59}
60
61
62parse_commandline()
63{
64  _positionals_count=0
65  while test $# -gt 0
66  do
67    _key="$1"
68    case "$_key" in
69      -p|--path)
70        test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
71        _arg_path="$2"
72        shift
73        ;;
74      --path=*)
75        _arg_path="${_key##--path=}"
76        ;;
77      -p*)
78        _arg_path="${_key##-p}"
79        ;;
80      -h|--help)
81        print_help
82        exit 0
83        ;;
84      -h*)
85        print_help
86        exit 0
87        ;;
88      *)
89        _last_positional="$1"
90        _positionals+=("$_last_positional")
91        _positionals_count=$((_positionals_count + 1))
92        ;;
93    esac
94    shift
95  done
96}
97
98
99assign_positional_args()
100{
101  local _positional_name _shift_for=$1
102  _positional_names=""
103  _our_args=$((${#_positionals[@]} - 0))
104  for ((ii = 0; ii < _our_args; ii++))
105  do
106    _positional_names="$_positional_names _arg_extension[$((ii + 0))]"
107  done
108
109  shift "$_shift_for"
110  for _positional_name in ${_positional_names}
111  do
112    test $# -gt 0 || break
113    eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
114    shift
115  done
116}
117
118parse_commandline "$@"
119assign_positional_args 1 "${_positionals[@]}"
120
121# OTHER STUFF GENERATED BY Argbash
122test -n "$DW_PATH" || DW_PATH="./"
123
124
125### END OF CODE GENERATED BY Argbash (sortof) ### ])
126# [ <-- needed because of Argbash
127
128# Configuration
129# ======================================================================
130
131declare -A paths
132
133# Set paths
134paths[old_wd]=$(pwd)
135if [[ -n $_arg_path ]] ; then
136  paths[dw]=$(realpath --canonicalize-missing "$_arg_path")
137else
138  paths[dw]=$(realpath --canonicalize-missing "$DW_PATH")
139fi
140paths[cli]="${paths[dw]}/bin/plugin.php"
141
142cd "${paths[dw]}" || die "CRITICAL: Failed to change directory to ${paths[dw]}" 1
143[[ -e ${paths[cli]} ]] || die "CRITICAL: DokuWiki plugin CLI not found at ${paths[dw]}" 1
144
145paths[tmp]=$(mktemp --directory)
146[[ -d ${paths[tmp]} ]] || die "CRITICAL: Temporary directory ${paths[tmp]} does not exist" 1
147
148# Set extensions to install
149read -ra _env_extension <<< "$DW_EXTENSIONS"
150extensions=( "${_env_extension[@]}" "${_arg_extension[@]}" )
151
152# Functions
153# ======================================================================
154
155function clean_up {
156  if [[ -d ${paths[tmp]} ]] ; then rm -rf "${paths[tmp]}" ; fi
157  cd "${paths[old_wd]}" || exit
158}
159
160# Script
161# ======================================================================
162
163trap clean_up EXIT
164
165for extension in "${extensions[@]}" ; do
166  echo >&2 "DEBUG: Installing extension ${extension}"
167
168  php "${paths[cli]}" extension install "$extension"
169done
170
171# ] <-- needed because of Argbash
172