aaa/lib/a3.sh

82 lines
1.9 KiB
Bash
Raw Normal View History

2024-06-06 02:01:36 +02:00
#!/bin/sh
# General purpose for a3
# a3ff <func> [args]: runs functions and automatically makes the first arg the file
# a3ff => a3 file function
A3_FILE=${A3_FILE:-}
a3ff() {
[ -z "$A3_FILE" ] && return 1
local func=$1
[ -z "$func" ] && return 1
shift
$func $A3_FILE $@
}
A3_INDEX_ROOT=${A3_INDEX_ROOT:-/var/lib/a3/index}
a3index_create() {
file=$1
name=$2
[ -z "$file" ] && return 1
[ -z "$name" ] && return 1
[ "$file" = "$A3_INDEX_ROOT/pending/$name.a3" ] && return 0
mkdir -p $A3_INDEX_ROOT/pending
cp $file $A3_INDEX_ROOT/pending/$name.a3
if ! grep -q "declare_workdir" $A3_INDEX_ROOT/pending/$name.a3; then
echo -e "\ndeclare_workdir $(pwd)" >> $A3_INDEX_ROOT/pending/$name.a3
fi
}
a3index_find() {
name=$1
[ -z "$name" ] && return 1
find $A3_INDEX_ROOT -iname "$name.a3" | head -n 1
}
a3index_current_status() {
name=$1
[ -z "$name" ] && return 1
file=$(a3index_find $name)
[ -z "$file" ] && return 1
basename $(dirname $file)
}
a3index_update() {
name=$1
status=$2
[ -z "$name" ] && return 1
[ -z "$status" ] && return 1
[ -f "$A3_INDEX_ROOT/$status/$name.a3" ] && return 0
file=$(a3index_find $name)
[ -z "$file" ] && return 1
mkdir -p $A3_INDEX_ROOT/$status
mv $file $A3_INDEX_ROOT/$status/$name.a3
}
a3index_is() {
name=$1
status=$2
[ -z "$name" ] && return 1
[ -z "$status" ] && return 1
[ -f "$A3_INDEX_ROOT/$status/$name.a3" ]
}
a3read_declaration() {
file=$1
declaration=$2
[ -z "$file" ] && return 1
[ -z "$declaration" ] && return 1
shift; shift
grep "$declaration" $file | sed "s/$declaration//" | awk '{$1=$1};1'
}
a3read_meta() {
file=$1
prop=$2
[ -z "$file" ] && return 1
[ -z "$prop" ] && return 1
shift; shift
grep "#a3.$prop:" $file | sed "s/#a3.$prop://" | xargs
}