tdb

tag database, a booru in your terminal
Log | Files | Refs | README

tdb (2210B)


      1 #!/bin/env -S rc
      2 
      3 tmp=/tmp/$pid
      4 
      5 dbt=tags
      6 dbf=sha
      7 
      8 # Util functions:
      9 
     10 fn echo2 {
     11 	echo $* >[1=2]
     12 }
     13 
     14 fn _exit {
     15 	rm -rf $tmp
     16 	exit $"*
     17 }
     18 
     19 fn _sha {
     20 	sha1sum $1 | sed 's/  .*//'
     21 }
     22 
     23 fn usage {
     24 	echo2 'usage: tdb cmd [args]'
     25 	echo2 'commands:'
     26 	echo2 ' - get tags...'
     27 	echo2 ' - tag file tags...'
     28 	echo2 ' - untag file tags...'
     29 	echo2 ' - hash file...'
     30 	_exit usage
     31 }
     32 
     33 # Commands:
     34 
     35 fn get {
     36 	count=$#*
     37 
     38 	if (test $count -lt 1) {
     39 		echo2 get: not enough arguments
     40 		usage
     41 	}
     42 
     43 	for (tag in $*) {echo '^'^$tag^'	.*'} > $tmp/tags
     44 
     45 	grep -f $tmp/tags $dbt      \
     46 		| uniq -c -1        \
     47 		| grep '^ *'^$count \
     48 		| sed 's/.*	//' \
     49 		> $tmp/sha
     50 
     51 	if (test -s $tmp/sha) {
     52 		grep -f $tmp/sha $dbf \
     53 			| sed 's/[0-9a-z]*  //'
     54 	}
     55 	if not {
     56 		echo2 No files.
     57 		_exit get: no files
     58 	}
     59 }
     60 
     61 fn tag {
     62 	if (test $#* -lt 2) {
     63 		usage
     64 	}
     65 
     66 	file=$1
     67 	shift
     68 
     69 	if (! test -f $file) {
     70 		echo2 file $file not found
     71 		_exit
     72 	}
     73 
     74 	sha=`{_sha $file}
     75 	tags=$*
     76 	for (tag in $tags) {
     77 		line=$tag^'	'^$sha
     78 		if(! grep $"line $dbt > /dev/null) echo $line >> $dbt
     79 	}
     80 }
     81 
     82 fn untag {
     83 	if (test $#* -lt 2) {
     84 		usage
     85 	}
     86 
     87 	file=$1
     88 	shift
     89 
     90 	if (! test -f $file) {
     91 		echo2 file $file not found
     92 		_exit
     93 	}
     94 
     95 	sha=`{_sha $file}
     96 	tags=$*
     97 	cp $dbt $tmp/db
     98 	for (tag in $tags) {
     99 		line='/'^$tag^'	'^$sha^'/d'
    100 		sed -e $"line $tmp/db > $tmp/db.untag
    101 		mv $tmp/db.untag $tmp/db
    102 	}
    103 	cp $tmp/db $dbt
    104 }
    105 
    106 fn hash {
    107 	if (test $#* -lt 1) {
    108 		echo2 hash: not enough arguments
    109 		usage
    110 	}
    111 
    112 	files=$*
    113 	dirs=()
    114 	while (test $#files -gt 0) {
    115 		for (file in $files) {
    116 			if (test -d $"file) dirs=($dirs $"file)
    117 			if (test -f $"file) sha1sum $"file
    118 		}
    119 		dir=$dirs(1)
    120 		dirs=$dirs(2-)
    121 		if (test -d $"dir) files=$"dir/*
    122 		if not files=()
    123 	}
    124 }
    125 
    126 # Main:
    127 
    128 if (! test $"*) {
    129 	specify command
    130 	usage
    131 }
    132 
    133 if (! test -f $"dbt){
    134 	msg=$"dbt': tags db does not exist!'
    135 	echo2 $msg
    136 	_exit $msg
    137 }
    138 
    139 if (! test -f $"dbf){
    140 	msg=$"dbf': sha db does not exist!'
    141 	echo2 $msg
    142 	_exit $msg
    143 }
    144 
    145 cmd=$1
    146 shift
    147 
    148 mkdir -p $tmp
    149 
    150 # in theory we could just exec the $cmd
    151 # in practice it would exec anything
    152 # and we don't actually want to exec anything
    153 
    154 switch ($cmd){
    155 case 'get'
    156 	get   $*
    157 case 'tag'
    158 	tag   $*
    159 case 'untag'
    160 	untag $*
    161 case 'hash'
    162 	hash $*
    163 case *
    164 	echo2 unknown command: $cmd
    165 	usage
    166 }
    167 
    168 _exit