Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Restructuring and additional functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcantrell committed Feb 14, 2011
1 parent 9e131fb commit 3b1e8c1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 24 deletions.
97 changes: 84 additions & 13 deletions bin/bashful-files
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Filename: bashful-files
# Description: Miscellaneous utility functions for dealing with files.
# Maintainer: Jeremy Cantrell <jmcantrell@gmail.com>
# Last Modified: Thu 2010-11-11 23:33:53 (-0500)
# Last Modified: Sun 2011-02-13 22:50:05 (-0500)

# doc bashful-files {{{
#
Expand Down Expand Up @@ -63,6 +63,40 @@ commonpath() #{{{1
echo "$prefix"
}

commontail() #{{{1
{
# doc commontail {{{
#
# Gets the common tails of the paths passed on stdin.
# Alternatively, paths can be passed as arguments.
#
# Usage: commontail [PATH...]
#
# Usage examples:
# commontail /foo/bar /boo/bar #==> bar
# commontail /foo/bar /boo/far #==>
#
# doc-end commontail }}}

local path

# Make sure command line args go to stdin
if (( $# > 0 )); then
for path in "$@"; do
echo "$path"
done | commontail
return
fi

local suffix=$(
while read -r; do
echo "$(abspath "$REPLY")"
done | commonsuffix
)

echo "${suffix#*/}"
}

extname() #{{{1
{
# doc extname {{{
Expand Down Expand Up @@ -133,20 +167,20 @@ filename() #{{{1
basename "$1" $(extname "$@")
}

increment_file() #{{{1
increment() #{{{1
{
# doc increment_file {{{
# doc increment {{{
#
# Get the next filename in line for the given file.
#
# Usage: increment_file FILENAME
# Usage: increment FILENAME
#
# Usage examples:
# increment_file does_not_exist #==> does_not_exist
# increment_file does_exist #==> does_exist (1)
# increment_file does_exist #==> does_exist (2)
# increment does_not_exist #==> does_not_exist
# increment does_exist #==> does_exist (1)
# increment does_exist #==> does_exist (2)
#
# doc-end increment_file }}}
# doc-end increment }}}

local file=$1
local count=1
Expand Down Expand Up @@ -174,6 +208,22 @@ listdir() #{{{1
find "$dir" -maxdepth 1 -mindepth 1 "$@"
}

files() #{{{1
{
# doc files {{{
#
# List all the files in the given directory (recursively).
# Will not display hidden files.
# Accepts the same options as the find command.
#
# Usage: files DIR [OPTIONS]
#
# doc-end files }}}

local dir=$1; shift
find "$dir" \( -type f -o -type l \) \! -wholename "*/.*" "$@"
}

abspath() #{{{1
{
# doc abspath {{{
Expand All @@ -199,13 +249,14 @@ abspath() #{{{1
# Path looks like: ~/...
[[ $path == ~* ]] && path=${path/\~/$HOME}

[[ $path == /* ]] || path=$PWD/$path
# Path is not absolute
[[ $path != /* ]] && path=$PWD/$path

path=$(squeeze "/" <<<"$path")

local elms=()
local elm
OIFS=$IFS; IFS="/"
local OIFS=$IFS; IFS="/"
for elm in $path; do
IFS=$OIFS
[[ $elm == . ]] && continue
Expand Down Expand Up @@ -285,13 +336,13 @@ link() #{{{1
ln -snT $(interactive_option) $(verbose_echo -v) "$@"
}

link_relative() #{{{1
linkrel() #{{{1
{
# doc link_relative {{{
# doc linkrel {{{
#
# Like link, but uses relpath to make the paths relative.
#
# Usage: link_relative SOURCE [DESTINATION]
# Usage: linkrel SOURCE [DESTINATION]
#
# doc-end link }}}

Expand Down Expand Up @@ -330,6 +381,26 @@ copy() #{{{1
cp -Tr $(interactive_option) $(verbose_echo -v) "$@"
}

stow() #{{{1
{
# doc stow {{{
#
# Replicate a directory tree and link regular files.
#
# doc-end stow }}}

local src=$1
local dst=${2:-$PWD}

local OIFS=$IFS; IFS=$'\n'
for f in $(files "$src"); do
IFS=$OIFS
local nf=$dst/${f#$src/}
mkdir -p "$(dirname "$nf")"
linkrel "$f" "$nf"
done
}

remove() #{{{1
{
# doc remove {{{
Expand Down
25 changes: 24 additions & 1 deletion bin/bashful-utils
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Filename: bashful-utils
# Description: Miscellaneous utility functions for use in other scripts.
# Maintainer: Jeremy Cantrell <jmcantrell@gmail.com>
# Last Modified: Sun 2010-10-24 01:25:47 (-0400)
# Last Modified: Sat 2011-02-12 21:56:01 (-0500)

# doc bashful-utils {{{
#
Expand Down Expand Up @@ -402,6 +402,29 @@ commonprefix() #{{{1
done | tail -n1
}

commonsuffix() #{{{1
{
# doc commonsuffix {{{
#
# Gets the common suffix of the strings passed on stdin.
#
# Usage examples:
# echo -e "foobar\nbabar" | commonsuffix #==> bar
# echo -e "broom\ngroom" | commonsuffix #==> room
#
# doc-end commonsuffix }}}

if (( $# > 0 )); then
local str
for str in "$@"; do
echo "$str"
done | commonsuffix
return
fi

rev | commonprefix | rev
}

sort_list() #{{{1
{
# doc sort_list {{{
Expand Down
16 changes: 6 additions & 10 deletions setup.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

set -e

NAME=bashful
HERE=$(dirname "$0")

# DESTDIR is provided for staged installs (used for packagers only)
DESTDIR=${DESTDIR:-}
PREFIX=${PREFIX:-/usr/local}

Expand All @@ -13,10 +12,6 @@ MANDIR="${DESTDIR}${PREFIX}/share/man/man1"

USAGE="Usage: setup.sh install|uninstall"

CP='cp -v'
RM='rm -vf'
LN_S='ln -vsf'

if (( $# == 0 )); then
echo "$USAGE"
exit 1
Expand All @@ -35,13 +30,14 @@ cd "$(dirname "$0")"

case $1 in
uninstall)
$RM "$BINDIR"/bashful*
$RM "$MANDIR"/bashful*.1
for fn in $HERE/bin/* $HERE/share/man/man1/*; do
rm -rf "$fn"
done
;;
install)
mkdir -p "$BINDIR" "$MANDIR"
$CP bin/bashful* "$BINDIR"
$CP share/man/man1/bashful*.1 "$MANDIR"
cp -v $HERE/bin/* "$BINDIR"
cp -v $HERE/share/man/man1/* "$MANDIR"
;;
*)
echo "$USAGE"
Expand Down
File renamed without changes.

0 comments on commit 3b1e8c1

Please sign in to comment.