From fe3244ca9c8c9a38ea700851e36667b1015d11e6 Mon Sep 17 00:00:00 2001 From: Mike Burns Date: Fri, 2 Aug 2013 14:27:44 -0400 Subject: Extra argument parsing into rcm.sh Pull the `-V`, `-v`, `-q`, `-t`, and `-d` out into the `handle_common_flags` and `handle_metadata_flags` functions, shared between the different programs. --- bin/Makefile.in | 4 ++-- bin/lsrc | 45 ++++++-------------------------------- bin/mkrc | 28 ++++-------------------- bin/rcup | 52 ++++++-------------------------------------- man/Makefile.in | 4 ++-- share/Makefile.in | 4 ++-- share/rcm.sh.in | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 87 insertions(+), 114 deletions(-) diff --git a/bin/Makefile.in b/bin/Makefile.in index a0d8196..5c3880f 100644 --- a/bin/Makefile.in +++ b/bin/Makefile.in @@ -180,9 +180,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) exit 1;; \ esac; \ done; \ - echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign bin/Makefile'; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu bin/Makefile'; \ $(am__cd) $(top_srcdir) && \ - $(AUTOMAKE) --foreign bin/Makefile + $(AUTOMAKE) --gnu bin/Makefile .PRECIOUS: Makefile Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status @case '$?' in \ diff --git a/bin/lsrc b/bin/lsrc index 766aa2f..d5e335e 100755 --- a/bin/lsrc +++ b/bin/lsrc @@ -1,13 +1,7 @@ #!/bin/sh -#set -x - -DEST_DIR=$HOME -DEFAULT_DOTFILES_DIR=$HOME/.dotfiles -HOSTNAME=`hostname -s` -DEBUG=: -PRINT=echo -VERBOSE=: +: ${RCM_LIB:=`dirname $0`/../share/rcm} +. $RCM_LIB/rcm.sh pushdir() { DIR_STACK="$DIR_STACK:$PWD/$1" @@ -91,7 +85,8 @@ handle_command_line() { arg_tags="" verbosity=0 version=0 - dotfiles_dirs= + dotfiles_dirs="" + while getopts Vqvt:d: opt; do case "$opt" in t) arg_tags="$arg_tags $OPTARG";; @@ -103,35 +98,8 @@ handle_command_line() { done shift $(($OPTIND-1)) - if [ $version -eq 1 ]; then - version rcup - exit 0 - elif [ $verbosity -ge 2 ]; then - DEBUG=echo - VERBOSE=echo - PRINT=echo - elif [ $verbosity -eq 1 ]; then - DEBUG=: - VERBOSE=echo - PRINT=echo - elif [ $verbosity -eq 0 ]; then - DEBUG=: - VERBOSE=: - PRINT=echo - else - DEBUG=: - VERBOSE=: - PRINT=: - fi - - if [ "x$arg_tags" != "x" ]; then - TAGS=$arg_tags - fi - - if [ "x$dotfiles_dirs" != "x" ]; then - DOTFILES_DIRS=$dotfiles_dirs - fi - + handle_common_flags lsrc $version $verbosity + handle_metadata_flags $arg_tags $dotfiles_dirs FILES=$@ } @@ -141,7 +109,6 @@ if [ -e $HOME/.rcrc ]; then . $HOME/.rcrc fi -. `dirname $0`/../share/rcm/rcm.sh handle_command_line $* if [ "x$DOTFILES_DIRS" = "x" ]; then diff --git a/bin/mkrc b/bin/mkrc index 5de894c..057c172 100755 --- a/bin/mkrc +++ b/bin/mkrc @@ -1,12 +1,7 @@ #!/bin/sh -# set -x - -DEFAULT_DOTFILES_DIR=$HOME/.dotfiles -MV=mv -INSTALL=rcup -ROOT_DIR=$HOME - +: ${RCM_LIB:=`dirname $0`/../share/rcm} +. $RCM_LIB/rcm.sh destination() { if [ $# -eq 2 ]; then @@ -20,8 +15,6 @@ if [ -e $HOME/.rcrc ]; then . $HOME/.rcrc fi -. `dirname $0`/../share/rcm/rcm.sh - if [ $# -eq 0 ]; then echo "Usage: mkrc [-d dir] [-t tag] [-v] [-q] filename ..." exit 1 @@ -46,25 +39,12 @@ while getopts Vvqt:d: opt; do done shift $(($OPTIND-1)) -if [ $version -eq 1 ]; then - version mkrc - exit 0 -elif [ $verbosity -ge 2 ]; then - MV="$MV -v" - INSTALL="$INSTALL -vv" -elif [ $verbosity -eq 1 ]; then - MV="$MV -v" - INSTALL="$INSTALL -v" -elif [ $verbosity -eq 0 ]; then - MV="$MV -v" -else - INSTALL="$INSTALL -q" -fi +handle_common_flags mkrc $version $verbosity files=$@ for file in $files; do - dotless=`echo $file | sed -e "s|$ROOT_DIR/||" | sed -e 's/^\.//'` + dotless=`echo $file | sed -e "s|$DEST_DIR/||" | sed -e 's/^\.//'` dest=`destination $DOTFILES_DIR $dotless $tag` mkdir -p $dest/`dirname $dotless` $MV $file $dest/$dotless diff --git a/bin/rcup b/bin/rcup index 4c3529d..c2e33dd 100755 --- a/bin/rcup +++ b/bin/rcup @@ -1,22 +1,10 @@ #!/bin/sh -#set -x +: ${RCM_LIB:=`dirname $0`/../share/rcm} +. $RCM_LIB/rcm.sh REPLACE_ALL=0 -DEST_DIR=$HOME -DEBUG=: -PRINT=echo -PROMPT=echo_n -VERBOSE=: -MKDIR=mkdir -LN=ln -RM=rm - -echo_n() { - printf "%s " "$*" -} - link_file() { local src=$1 local dest=$2 @@ -88,8 +76,9 @@ handle_command_line() { arg_tags="" verbosity=0 version=0 - dotfiles_dirs= + dotfiles_dirs="" LS_ARGS= + while getopts Vqvt:d: opt; do case "$opt" in t) @@ -107,44 +96,17 @@ handle_command_line() { done shift $(($OPTIND-1)) - if [ $version -eq 1 ]; then - version rcup - exit 0 - elif [ $verbosity -ge 2 ]; then - DEBUG=echo - VERBOSE=echo - PRINT=echo - elif [ $verbosity -eq 1 ]; then - DEBUG=: - VERBOSE=echo - PRINT=echo - elif [ $verbosity -eq 0 ]; then - DEBUG=: - VERBOSE=: - PRINT=echo - else - DEBUG=: - VERBOSE=: - PRINT=: - fi - - if [ "x$arg_tags" != "x" ]; then - TAGS=$arg_tags - fi - - if [ "x$dotfiles_dirs" != "x" ]; then - DOTFILES_DIRS=$dotfiles_dirs - fi - + handle_common_flags rcup $version $verbosity + handle_metadata_flags $arg_tags $dotfiles_dirs FILES=$@ LS_ARGS="$LS_ARGS $FILES" + $DEBUG "LS_ARGS: $LS_ARGS" } if [ -e $HOME/.rcrc ]; then . $HOME/.rcrc fi -. `dirname $0`/../share/rcm/rcm.sh handle_command_line $* for dest_and_src in `lsrc $LS_ARGS`; do diff --git a/man/Makefile.in b/man/Makefile.in index 0d9e7ce..fecc814 100644 --- a/man/Makefile.in +++ b/man/Makefile.in @@ -184,9 +184,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) exit 1;; \ esac; \ done; \ - echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign man/Makefile'; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu man/Makefile'; \ $(am__cd) $(top_srcdir) && \ - $(AUTOMAKE) --foreign man/Makefile + $(AUTOMAKE) --gnu man/Makefile .PRECIOUS: Makefile Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status @case '$?' in \ diff --git a/share/Makefile.in b/share/Makefile.in index 8b2661d..448b28c 100644 --- a/share/Makefile.in +++ b/share/Makefile.in @@ -181,9 +181,9 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) exit 1;; \ esac; \ done; \ - echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign share/Makefile'; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu share/Makefile'; \ $(am__cd) $(top_srcdir) && \ - $(AUTOMAKE) --foreign share/Makefile + $(AUTOMAKE) --gnu share/Makefile .PRECIOUS: Makefile Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status @case '$?' in \ diff --git a/share/rcm.sh.in b/share/rcm.sh.in index 44131ec..c057e87 100644 --- a/share/rcm.sh.in +++ b/share/rcm.sh.in @@ -1,5 +1,25 @@ VERSION="@PACKAGE_VERSION@" +#set -x + +DEBUG=: +DEST_DIR=$HOME +PRINT=echo +PROMPT=echo_n +VERBOSE=: +MKDIR=mkdir +LN=ln +RM=rm +DEFAULT_DOTFILES_DIR=$HOME/.dotfiles +MV=mv +INSTALL=rcup +ROOT_DIR=$HOME +HOSTNAME=`hostname -s` + +echo_n() { + printf "%s " "$*" +} + version() { cat << EOV $1 (rcm) $VERSION @@ -9,3 +29,47 @@ License BSD: BSD 3-clause license Written by Mike Burns. EOV } + +handle_common_flags() { + local prog_name=$1 + local version=$2 + local verbosity=$3 + + if [ $version -eq 1 ]; then + version $prog_name + exit 0 + elif [ $verbosity -ge 2 ]; then + DEBUG=echo + VERBOSE=echo + PRINT=echo + MV="$MV -v" + INSTALL="$INSTALL -vv" + elif [ $verbosity -eq 1 ]; then + DEBUG=: + VERBOSE=echo + PRINT=echo + MV="$MV -v" + INSTALL="$INSTALL -v" + elif [ $verbosity -eq 0 ]; then + DEBUG=: + VERBOSE=: + PRINT=echo + MV="$MV -v" + else + DEBUG=: + VERBOSE=: + PRINT=: + INSTALL="$INSTALL -q" + fi +} + +handle_metadata_flags() { + local arg_tags=$1 + local dotfiles_dirs=$2 + + : ${TAGS:=$arg_tags} + : ${DOTFILES_DIRS:=$dotfiles_dirs} + + $DEBUG "TAGS: $TAGS" + $DEBUG "DOTFILES_DIRS: $DOTFILES_DIRS" +} -- cgit v1.2.3