Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 79 lines (62 sloc) 1.55 KB
#!/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() {
LNAME=""
while [ "x$LNAME" == "x" ]; do
LNAME=`rlwrap -S "Insert label: " -D2 -f $LFILE -H $LFILE -o cat`
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
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
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