1#!/usr/bin/env bash
2
3if [[ ! $# == 2 ]] ; then
4  echo "Use two arguments for the DokuWiki path and protected directory"
5	echo
6	echo "Usage:"
7	echo "  $0 DW_PATH PROTECTED_DIR"
8	echo
9  echo "DW_PATH is any directory that can be resolved by the script, e.g."
10  echo "  /var/www/public"
11  echo "  ./"
12	echo "PROTECTED_DIR is a path relative to the DokuWiki root, e.g."
13	echo "  lib/plugins/usermanager"
14	exit 1
15fi
16
17# Configuration ========================================================
18
19# Script constants -----------------------------------------------------
20
21SUCCESS=0
22FAILURE=1
23
24# Script variables -----------------------------------------------------
25
26DW_PATH=$1
27PROTECTED_DIR=$2
28
29# Script ===============================================================
30
31# Check DW_PATH exists
32if [[ ! -d "${DW_PATH}" ]] ; then
33  echo "ERROR: DW_PATH ${DW_PATH} is not a directory"
34  exit ${FAILURE}
35fi
36# Check DW_PATH contains a DokuWiki installation
37if [[ ! -e "${DW_PATH}/doku.php" ]] ; then
38  echo "ERROR: DW_PATH ${DW_PATH} does not contain a DokuWiki installation (checked for doku.php)"
39  exit ${FAILURE}
40fi
41# Check PROTECTED_DIR exists
42if [[ ! -d "${DW_PATH}/${PROTECTED_DIR}" ]] ; then
43  echo "ERROR: PROTECTED_DIR ${DW_PATH}/${PROTECTED_DIR} is not a directory"
44  exit ${FAILURE}
45fi
46
47for file in $(find "${DW_PATH}" -type f -depth | grep -v "^${DW_PATH}/${PROTECTED_DIR}/") ; do
48  rm ${file}
49done
50
51for dir in $(
52  find "${DW_PATH}" -mindepth 1 -type d | \
53  grep -v "^${DW_PATH}/${PROTECTED_DIR}$" | \
54  grep -v "^${DW_PATH}/${PROTECTED_DIR}/" | \
55  grep -v "^${DW_PATH}/lib/plugins$" | \
56  grep -v "^${DW_PATH}/lib$" \
57) ; do
58  rmdir ${dir}
59done
60
61exit ${SUCCESS}
62