2020-11-03 12:07:49 -08:00
|
|
|
#!/bin/bash
|
2020-11-20 08:48:48 -08:00
|
|
|
|
2020-12-03 20:48:04 -08:00
|
|
|
# export BASH_DEBUG=true
|
2022-03-28 10:02:17 -07:00
|
|
|
module_load debug
|
2022-02-26 17:19:37 -08:00
|
|
|
module_load iecho
|
2020-12-03 20:48:04 -08:00
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
isFile() {
|
|
|
|
if [[ -f $1 ]]
|
|
|
|
then
|
|
|
|
echo "true"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:48:04 -08:00
|
|
|
build_file () {
|
2022-12-26 10:21:30 -08:00
|
|
|
# local file
|
|
|
|
# echo build file $1 $2
|
2022-12-30 09:18:39 -08:00
|
|
|
# todo merge no cr
|
2022-12-26 10:21:30 -08:00
|
|
|
[[ $1 == "-f" ]] && file=true && shift 1
|
2022-12-30 09:18:39 -08:00
|
|
|
[[ -f "$2" ]] || { iecho "output file $2 does not exist"; return 1; }
|
2020-12-03 20:48:04 -08:00
|
|
|
if [[ -f "$1" ]]; then
|
2022-12-30 09:18:39 -08:00
|
|
|
# echo adding file $1 to $2
|
2022-12-26 10:21:30 -08:00
|
|
|
[[ $file ]] && echo -e "\n####### ADDED $1 ########" >> $2
|
|
|
|
# remove comment lines, remove blank last line
|
2020-12-03 20:48:04 -08:00
|
|
|
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
|
2022-12-30 09:18:39 -08:00
|
|
|
[[ ! $file ]] && echo -e "\n" >> $2
|
|
|
|
else iecho "no such file $1 to append to $2"
|
2020-12-03 20:48:04 -08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-01-05 12:36:23 -08:00
|
|
|
# TODO need to test
|
2020-11-20 08:48:48 -08:00
|
|
|
function lines_2_str () {
|
|
|
|
[[ ! -f "$1" ]] && return 1
|
|
|
|
local str=''
|
|
|
|
# echo the lines: $lines >&2
|
|
|
|
# lines="$(cat "$1")"
|
|
|
|
while read line; do
|
|
|
|
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
|
|
|
# echo line: "${line}" >&2
|
|
|
|
str="$str ${line}"
|
|
|
|
# echo str: $str >&2
|
2023-01-05 12:36:23 -08:00
|
|
|
done < $1
|
2020-11-20 08:48:48 -08:00
|
|
|
# for line in $lines ; do
|
|
|
|
# str+='"'$line'" '
|
|
|
|
# done
|
|
|
|
echo $str
|
|
|
|
}
|
|
|
|
|
2023-02-22 11:39:34 -08:00
|
|
|
|
|
|
|
|
2020-12-03 20:48:04 -08:00
|
|
|
# find, superceeds find use `command find` to get the super
|
|
|
|
function _find () {
|
2023-02-22 11:39:34 -08:00
|
|
|
|
|
|
|
# BASH_DEBUG=find
|
|
|
|
|
2020-12-03 20:48:04 -08:00
|
|
|
# USAGE
|
2020-11-03 12:07:49 -08:00
|
|
|
# all option arguments that contain globs/wildcards must be quoted to avoid expansion
|
2020-11-20 08:48:48 -08:00
|
|
|
# f sets path and file excludes from a supplied file path
|
|
|
|
# all lines ending in / will be treated as directory names to ignore, otherwise files
|
|
|
|
# p option explictly excludes paths(directories)
|
2020-11-03 12:07:49 -08:00
|
|
|
# d option sets the directory depth which is current directy by default, 0 is all
|
2020-11-20 08:48:48 -08:00
|
|
|
# x excitly excludes file globs as a string
|
|
|
|
# n inclucdes only file globs otherwise it's all except .files
|
2020-11-03 12:07:49 -08:00
|
|
|
# if no directory is given it will attempt to source the present working directory
|
|
|
|
# example:
|
|
|
|
# source_dir -p "archive" -x '"*.off" "*.md"' -d 0 # $DIR/$SUBDIR
|
2020-11-20 08:48:48 -08:00
|
|
|
local EXCLUDE_FILE
|
2020-11-03 12:07:49 -08:00
|
|
|
local PATHS
|
|
|
|
local NAMES
|
|
|
|
local ENAMES
|
|
|
|
local DEPTH=1
|
2020-12-03 20:48:04 -08:00
|
|
|
local HIDDEN
|
2020-11-03 12:07:49 -08:00
|
|
|
|
2020-12-03 20:48:04 -08:00
|
|
|
declare OPTION
|
|
|
|
declare OPTARG
|
|
|
|
declare OPTIND
|
2023-02-23 10:03:47 -08:00
|
|
|
while getopts 't:p:d:e:n:f:hl' OPTION; do
|
2020-11-03 12:07:49 -08:00
|
|
|
case "$OPTION" in
|
2021-11-20 17:20:44 -08:00
|
|
|
t)
|
|
|
|
TYPE=$OPTARG
|
|
|
|
# echo "TYPE $TYPE"
|
|
|
|
;;
|
2020-11-20 08:48:48 -08:00
|
|
|
f)
|
|
|
|
EXCLUDE_FILE=$OPTARG
|
|
|
|
# echo EXCLUDE FILE $EXCLUDE_FILE >&2
|
|
|
|
;;
|
2020-11-03 12:07:49 -08:00
|
|
|
p)
|
|
|
|
# PATHS=("$OPTARG")
|
|
|
|
IFS=',' read -r -a PATHS <<< "$OPTARG"
|
|
|
|
# echo EXCLUDING THESE PATHS ${PATHS[*]}
|
|
|
|
;;
|
2020-11-20 08:48:48 -08:00
|
|
|
e)
|
2020-11-03 12:07:49 -08:00
|
|
|
IFS=',' read -r -a ENAMES <<< "${OPTARG}"
|
|
|
|
# echo EXCLUDING THESE FILE NAMES ${ENAMES[*]}
|
|
|
|
;;
|
2020-11-20 08:48:48 -08:00
|
|
|
n)
|
2020-11-03 12:07:49 -08:00
|
|
|
# NAMES=("$OPTARG")
|
|
|
|
IFS=',' read -r -a NAMES <<< "${OPTARG}"
|
2020-12-26 11:53:16 -08:00
|
|
|
# NAMES=$OPTARG
|
2020-11-03 12:07:49 -08:00
|
|
|
;;
|
|
|
|
d)
|
|
|
|
DEPTH=$OPTARG
|
|
|
|
# echo "SOURCING TO DEPTH (0=any)" "$DEPTH"
|
|
|
|
;;
|
2020-12-03 20:48:04 -08:00
|
|
|
h)
|
|
|
|
HIDDEN=true
|
|
|
|
# echo "SOURCING TO DEPTH (0=any)" "$DEPTH"
|
|
|
|
;;
|
2023-02-23 10:03:47 -08:00
|
|
|
|
|
|
|
l)
|
|
|
|
LINKS=-L
|
|
|
|
;;
|
2020-11-03 12:07:49 -08:00
|
|
|
*)
|
|
|
|
echo unknown option $OPTION
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $(( OPTIND - 1 ))
|
|
|
|
|
|
|
|
local DIR
|
2020-11-20 08:48:48 -08:00
|
|
|
DIR="$*"
|
2020-11-03 12:07:49 -08:00
|
|
|
if [ ! "$DIR" ]; then
|
|
|
|
if [ -v PS1 ]; then
|
2020-12-03 20:48:04 -08:00
|
|
|
echo no directory provided to search
|
|
|
|
echo searching present working directory $(pwd)
|
2020-11-03 12:07:49 -08:00
|
|
|
read -p "Do you want to continue? " -n 1 -r
|
|
|
|
[[ $REPLY =~ ^[Yy]$ ]] && DIR=$(pwd) || return 1
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ ! -d "$DIR" ] && echo " directory $DIR does not exist, aborting" && return 1
|
|
|
|
|
|
|
|
# echo dir $DIR
|
|
|
|
|
|
|
|
local FIND
|
2023-02-23 10:03:47 -08:00
|
|
|
FIND="command find $LINKS $DIR"
|
2020-11-03 12:07:49 -08:00
|
|
|
FIND+=$([ ! $DEPTH == 0 ] && echo " -maxdepth $DEPTH ")
|
2021-11-20 17:20:44 -08:00
|
|
|
# FIND+=" -type $([ $TYPE ] && echo "$TYPE" || echo "f")"
|
|
|
|
TYPE=${TYPE:-f}
|
|
|
|
FIND+=" -type $TYPE "
|
2020-12-03 20:48:04 -08:00
|
|
|
# include HIDDEN files and directories IS FALSE BY DEFULT
|
|
|
|
[[ ! $HIDDEN ]] && FIND+="! -path \"*/.*/*\" ! -name \".*\" "
|
2020-11-03 12:07:49 -08:00
|
|
|
|
|
|
|
local name
|
|
|
|
local path
|
|
|
|
|
2020-11-20 08:48:48 -08:00
|
|
|
if [[ -f $EXCLUDE_FILE ]]; then
|
|
|
|
local ignores=$(lines_2_str "$EXCLUDE_FILE")
|
|
|
|
# echo ignores: $ignores >&2
|
|
|
|
for exclude in $ignores ; do
|
|
|
|
# echo exclude: ${exclude} >&2
|
|
|
|
[[ "$exclude" == */ ]] && PATHS+=("${exclude::-1}") || ENAMES+=("$exclude")
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# echo paths ${PATHS[@]} >&2
|
|
|
|
# echo exclude names ${ENAMES[@]} >&2
|
|
|
|
|
2020-12-26 11:53:16 -08:00
|
|
|
set -o noglob
|
|
|
|
|
2020-11-03 12:07:49 -08:00
|
|
|
if [[ ${PATHS[0]} ]]; then
|
|
|
|
for path in ${PATHS[@]}; do
|
|
|
|
# echo excluding $path
|
|
|
|
FIND+=$(echo ' ! -path "*/'$path'/*"')
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ${ENAMES[0]} ]]; then
|
|
|
|
for name in ${ENAMES[@]}; do
|
2020-12-26 11:53:16 -08:00
|
|
|
debug excluding name "$name"
|
|
|
|
FIND+=$(echo ' ! -name '$name'')
|
2020-11-03 12:07:49 -08:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-12-26 11:53:16 -08:00
|
|
|
debug "INCLUDING ONLY THESE FILE NAMES ${NAMES[*]}"
|
2020-11-03 12:07:49 -08:00
|
|
|
if [[ ${NAMES[0]} ]]; then
|
2020-12-26 11:53:16 -08:00
|
|
|
for name in "${NAMES[@]}"; do
|
|
|
|
debug only finding $name
|
|
|
|
FIND+=$(echo " -name '${name}'")
|
2020-11-03 12:07:49 -08:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-12-26 11:53:16 -08:00
|
|
|
# if [[ $NAMES ]]; then
|
|
|
|
# debug names for find command $NAMES
|
|
|
|
# for name in $NAMES; do
|
|
|
|
# debug "xxonly finding '$name'"
|
|
|
|
# FIND+=$(set -o noglob;echo " -name '${name}'")
|
|
|
|
# done
|
|
|
|
# fi
|
|
|
|
|
|
|
|
|
2020-11-03 12:07:49 -08:00
|
|
|
# echo
|
2020-11-20 08:48:48 -08:00
|
|
|
# echo find dir: $DIR >&2
|
2020-12-03 20:48:04 -08:00
|
|
|
debug "find command: $FIND"
|
2020-12-26 11:53:16 -08:00
|
|
|
set +o noglob
|
2020-11-03 12:07:49 -08:00
|
|
|
local FILES
|
|
|
|
FILES=$(eval $FIND | sort)
|
2020-12-03 20:48:04 -08:00
|
|
|
[[ $FILES ]] && echo $FILES
|
2020-11-20 08:48:48 -08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
source_dir () {
|
2023-02-22 11:39:34 -08:00
|
|
|
# BASH_DEBUG=source_dir
|
|
|
|
|
|
|
|
debug source_dir passed arguments: $*
|
2020-12-03 20:48:04 -08:00
|
|
|
debug function: source_dir
|
2020-11-20 08:48:48 -08:00
|
|
|
local FILES
|
2023-02-23 10:03:47 -08:00
|
|
|
FILES=$(_find -l "$@") # find function
|
2020-12-03 20:48:04 -08:00
|
|
|
# echo $FILES >&2
|
2020-11-20 08:48:48 -08:00
|
|
|
[[ $? -ne 0 ]] && return 1
|
|
|
|
for f in $FILES; do
|
2023-02-22 11:39:34 -08:00
|
|
|
debug sourcing: $f >&2
|
2020-11-20 08:48:48 -08:00
|
|
|
source "$f"
|
|
|
|
done
|
2020-11-03 12:07:49 -08:00
|
|
|
|
|
|
|
}
|
2022-02-26 17:19:37 -08:00
|
|
|
|
|
|
|
prepend_file () {
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------------
|
|
|
|
# usage prepend_file <somefile> <anotherfile>
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------------
|
|
|
|
# Prepend the contents of <somefile> [$1], to <anotherfile> [$2], leaving the result in <anotherfile> [$2].
|
|
|
|
# insert a newline at the end of <somefile> [$1] if necessary
|
|
|
|
# ----------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# check # echo $1 $2
|
2022-03-01 22:44:26 -08:00
|
|
|
[[ -f $1 ]] || return 1
|
|
|
|
[[ -f $2 ]] || return 2
|
2022-02-26 17:19:37 -08:00
|
|
|
# init
|
|
|
|
tmp_fn=$( mktemp -t TEMP_FILE_prepend.XXXXXXXX )
|
|
|
|
chmod 600 "$tmp_fn"
|
|
|
|
\cp $1 $tmp_fn
|
|
|
|
sed -i '$a\' $tmp_fn
|
|
|
|
cat $2 >> $tmp_fn
|
|
|
|
\mv "$tmp_fn" "$2"
|
|
|
|
# cleanup
|
|
|
|
rm -f "$tmp_fn"
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# [End]
|
2022-03-28 10:02:17 -07:00
|
|
|
}
|