--- /dev/null
+#!/usr/bin/env bash
+#################################################################################
+#
+# NB 08.09.16 - (C) 2016 Nicolas Boisselier
+# /opt/nb/bin/ownCloud-sync
+#
+#################################################################################
+declare -r NAME="$(basename "${0}")"
+
+################################################################################
+#
+# Default value options
+#
+#################################################################################
+VERBOSE=0
+DEBUG=0
+
+#################################################################################
+#
+# Args
+#
+#################################################################################
+while [ $# -gt 0 ]; do
+
+ case "$1" in
+
+ -*help|-h) usage | pod2text --width 250; exit 0 ;;
+ --man) usage | pod2man | man -l -; exit 0 ;;
+ --help2man) usage | pod2man; exit 0 ;;
+
+ --verbose|-v) VERBOSE=$(($VERBOSE+1)) ;;
+
+ --debug|-d) DEBUG=$(($DEBUG+1)) ;;
+
+ *) echo "Unknow option: $1 at $0!"; exit -1; ;;
+
+ esac
+ shift
+
+done
+
+#################################################################################
+#
+# Main
+#
+#################################################################################
+main() {
+
+ if [ "$UID" == "0" ]; then
+ owncloud_all "/{home,Users}/*/ownCloud"
+ else
+ owncloud_sync "~/ownCloud" https://owncloud.nbdom.net
+ fi
+}
+################################################################################
+#
+# Functions
+#
+#################################################################################
+usage() {
+echo "
+=head1 NAME
+
+${NAME}
+
+=head1 SYNOPSIS
+
+Sync all ownCloud directories found.
+
+ -v, --verbose Print verbose mode
+ -d, --debug Print debug messages
+ -h, --help Print this help (alternatives: --man, --help2man)
+
+=head1 LICENSE
+
+Copyright (C) 2016 Nicolas Boisselier
+
+This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+See <http://www.gnu.org/licenses/>.
+
+=head1 AUTHOR
+
+Nicolas Boisselier nicolas.boisselier@gmail.com
+"
+}
+
+dir_mounted() {
+ mount -l | awk '$3 == "'$1'" { exit 1 }' && return 1
+ return 0
+}
+
+err() {
+ exit_code=$1
+ msg=$2
+ echo "ERR: $msg" >&2
+ exit $exit_code
+}
+
+warn() {
+ msg=$1
+ echo "WARN: $msg" >&2
+}
+
+verbose() {
+ msg=$1
+
+ level=$2
+ [ -z "$level" ] && level=1
+ level=$(($level-1))
+
+ [ "$VERBOSE" -gt "$level" ] || return
+
+ echo "VERBOSE: $msg" >&2
+}
+
+owncloud_sync() {
+ lock="~/$NAME.lock"
+ if [ -e "$lock" ]; then
+ [ "$VERBOSE" -gt 0 ] && echo "$lock already exists"
+ return 1
+ fi
+ if ! touch "$lock"; then
+ [ "$VERBOSE" -gt 0 ] && echo "Can't create $lock"
+ rm "$lock"
+ return 1
+ fi
+ # /OCC::(.*?) created for "(*.?)" . "(.*?)/"
+ echo owncloudcmd -n --non-interactive "$1" "$2" 2>&1 | perl -ne 'print "$1 $2 $3\n" iand next; if /OCC::(\S+).*?"(.*?)" . "(.*?)" "(.*?)"/; print'
+ rm "$lock"
+ return 0
+}
+
+owncloud_all() {
+ [ -z "$*" ] && return 1
+ IFS=$'\n'
+ for dir in `eval ls -1d "$*" 2>/dev/null`; do
+ user=`ls -ld "$dir" |awk '{print $3}'`
+ cd "$dir" || continue
+
+ [ "$VERBOSE" -gt 0 ] && echo "$user: $dir"
+ su - "$user" -c "$NAME" | sed "s/^/$user /"
+
+ done
+}
+main