#!/usr/bin/env bash
#
# List keys, tree version.
#

# Thanks http://www.centerkey.com/tree/
function keyringer_tree {
  find $* | sed -e 's/[^-][^\/]*\//|  /g' | sed -r -e 's/\|  ([^|])/|- \1/'
}

# Load functions
LIB="`dirname $0`/../functions"
source "$LIB" maintenance $* || exit 1

# Aditional parameters
CWD="`pwd`"

# Avoid leading slash
shift
ARGS="`echo "$*" | sed -e "s|^/*||"`"

# on *BSD, find expects at least one argument with the path
if [ "$ARGS" == "" ]; then
  ARGS="./"
fi

# Check implementation
if which tree &> /dev/null; then
  TREE="tree"
else
  TREE="keyringer_tree"
fi

# Run list command
cd "$KEYDIR/$RELATIVE_PATH" && $TREE $ARGS
cd "$CWD"
