dmc

dynamic mail client
git clone git://git.suckless.org/dmc
Log | Files | Refs | README | LICENSE

dmc-tag (2663B)


      1 #!/bin/sh
      2 # dmc - dynamic mail client
      3 # See LICENSE file for copyright and license details.
      4 # TODO: rewrite in C
      5 
      6 VERSION="0.1"
      7 
      8 [ -z "${DMCTAG_ROOT}" ] && \
      9 	DMCTAG_ROOT="${HOME}/.dmctag"
     10 if [ ! -d "${DMCTAG_ROOT}" ]; then
     11 	mkdir -p "${DMCTAG_ROOT}"
     12 	if [ ! $? = 0 ]; then
     13 		echo "Cannot create \${DMCTAG_ROOT}"
     14 		exit 1
     15 	fi
     16 fi
     17 cd ${DMCTAG_ROOT}
     18 
     19 set_file () {
     20 	FILE=$1
     21 	if [ ! "`echo ${FILE} | cut -c 1`" = / ]; then
     22 		# autocomplete relative paths
     23 		FILE="${OLDPWD}/${FILE}"
     24 	fi
     25 	if [ ! -e "${FILE}" ]; then
     26 		echo "Cannot find ${FILE}"
     27 		exit 1
     28 	fi
     29 	export FILE
     30 }
     31 
     32 # untag this file . this is highly suboptimal. in C will be much faster
     33 # XXX: Only used with $FILE.. set_file is required
     34 untag () {
     35 	for TAG in `$0 -l` ; do
     36 		grep -v $1 $TAG > $TAG.tmp
     37 		mv $TAG.tmp $TAG
     38 	done
     39 }
     40 
     41 case "$1" in
     42 "-f")
     43 	cat * 2> /dev/null | sort | uniq
     44 	;;
     45 "-m")
     46 	# move cached files in from to
     47 	for a in `$0 -l` ; do
     48 		sed -e "s,^$1,$2," $a > $a.tmp
     49 		mv $a.tmp $a
     50 	done
     51 	;;
     52 "-c")
     53 	# check/cleanup for missing files
     54 	if [ -n "$2" ] ; then
     55 		set_file "$2"
     56 		if [ -z "`cat * 2> /dev/null | grep $FILE`" ]; then
     57 			rm -f "${FILE}"
     58 		fi
     59 	else
     60 		for a in `$0 -f` ; do
     61 			if [ ! -e $a ]; then
     62 				echo Untag missing file $a
     63 				untag ${FILE}
     64 			fi
     65 		done
     66 	fi
     67 	;;
     68 "-l")
     69 	if [ -z "$2" ]; then
     70 		ls | cat
     71 	else
     72 		# XXX buggy: does not supports filenames with spaces
     73 		tag0=$2
     74 		shift ; shift
     75 		for a in `cat ${DMCTAG_ROOT}/${tag0}`; do
     76 			for b in $* ; do
     77 				a="`grep "$a" ${DMCTAG_ROOT}/$b`"
     78 				[ -z "$a" ] && break
     79 			done
     80 			[ -n "$a" ] && echo $a
     81 		done
     82 	fi
     83 	;;
     84 "-u")
     85 	set_file "$2"
     86 	untag ${FILE}
     87 	;;
     88 "-v")
     89 	echo "dmc-tag v${VERSION}"
     90 	;;
     91 "")
     92 	echo "Usage: dmc-tag [-uhv] [-l [tag ..]] [-c [file ..]] [-m dir dir] [file ..]"
     93 	;;
     94 "-h")
     95 	$0
     96 	echo " dmc-tag file            # get tags of file"
     97 	echo " dmc-tag file tag1 tag2  # set tags of file"
     98 	echo " dmc-tag -u file         # untag file"
     99 	echo " dmc-tag -m dir1 dir2    # move files from dir1 to dir2 in tags"
    100 	echo " dmc-tag -f              # list all tagged files"
    101 	echo " dmc-tag -l              # list tags"
    102 	echo " dmc-tag -l tag1 tag2    # list files matching tag1+tag2"
    103 	echo " dmc-tag -c              # check tag consistency. untag all nonexistent files"
    104 	echo " dmc-tag -c file         # remove file if not tagged"
    105 	echo " dmc-tag -v              # show version"
    106 	echo " dmc-tag -h              # show this help"
    107 	;;
    108 *)
    109 	if [ -z "$2" ]; then
    110 		# get tags of given file
    111 		set_file "$1"
    112 		grep "$1" * | cut -d : -f 1
    113 	else
    114 		# set tags for a file
    115 		set_file "$1"
    116 		untag "${FILE}"
    117 		while [ -n "$2" ] ; do
    118 			[ -z "`grep $FILE ${DMCTAG_ROOT}/$2`" ] && \
    119 				echo "$FILE" >> ${DMCTAG_ROOT}/$2
    120 			shift
    121 		done
    122 	fi
    123 	;;
    124 esac