#!/bin/bash # $1 is the action (append, show, remove) # $2 is the filename # Configuration LFILE="$HOME/.labels" ACTION="$1" FNAME="$2" NFNAME="/tmp/editlabels-`basename "$2"`.$$" function asklabel() { read -e -p "Insert label: " $1 CVAL=${!1} while ( ! grep -q "^$CVAL$" "$LFILE" ) || ( [ "$CVAL" == "" ] ) ; do echo "Invalid label \"$CVAL\"" read -e -p "Insert label: " $1 CVAL=${!1} done } if [ "$ACTION" == "menu" ]; then function getact() { read -p "Append/Remove/Show/Clean/List: " ACT if [ "$ACT" == "a" ]; then ACTION=append elif [ "$ACT" == "r" ]; then ACTION=remove elif [ "$ACT" == "s" ]; then ACTION=show elif [ "$ACT" == "c" ]; then ACTION=clean elif [ "$ACT" == "l" ]; then ACTION=list else echo "Invalid action" getact fi } getact fi if [ "$ACTION" == "append" ]; then ACT=`formail -c -X X-Label < "$FNAME"` asklabel LNAME if [ "$ACT" == "" ]; then NEW="X-Label: $LNAME" else NEW="$ACT, $LNAME" fi formail -I "$NEW" < "$FNAME" > "$NFNAME" elif [ "$ACTION" == "remove" ]; then ACT=`formail -c -X X-Label < "$FNAME"` asklabel LNAME NEW=`echo $ACT | sed "s/, $LNAME//g" | sed "s/$LNAME, //g" | sed "s/: $LNAME/:/g"` formail -I "$NEW" < "$FNAME" > "$NFNAME" elif [ "$ACTION" == "show" ]; then formail -c -X "X-Label:" < "$FNAME" read -p "Press any key to continue" elif [ "$ACTION" == "clean" ]; then formail -I "X-Label:" < "$FNAME" > "$NFNAME" elif [ "$ACTION" == "list" ]; then echo "Available labels (from ~/.labels):" cat $LFILE read -p "Press any key to continue" fi # if we created a new file, step over the old one if [ -f "$NFNAME" ]; then mv "$NFNAME" "$FNAME" fi