gencard (984B)
1 #!/bin/sh 2 3 # generates a card with the next available id in the provided deck 4 # ledger. a folder is created for it and the templates files are copied in. 5 # finally the new card is opened in an editor 6 7 . ./config 8 9 usage() { 10 echo "$0 name.deck [extra]" 11 exit 1 12 } 13 14 [ $# -lt 1 ] && usage 15 [ $# -gt 2 ] && usage 16 17 tmp=$(mktemp) 18 die() { 19 [ -f $tmp ] && rm -f $tmp 20 } 21 trap die EXIT INT TERM 22 23 line=$(grep "$DELIM$2\$" $1) 24 [ "$line" ] && echo "$2 already in deck:" && echo "$line" && exit 1 25 26 id=$(nextcardid "$1") 27 time=$(date $TIMEFMT) 28 29 chmod +w "$1" 30 printf "%s"$DELIM"%s"$DELIM"%s"$DELIM"%d"$DELIM"%s\n" \ 31 "$id" "$time" "$time" 0 "$2" >> "$1" 32 33 sort -r "$1">|"$tmp" 34 mv "$tmp" "$1" 35 chmod -w "$1" 36 37 deck=$(echo "$1" | cut -d '.' -f 1) 38 path=$(printf "%s/%s" "$deck" "$id") 39 front=${FRONT_TEMP:-front} 40 back=${BACK_TEMP:-back} 41 42 mkdir -p $path 43 sed s:==TARGET==:$2:g "$front" >> "$path/$front" 44 sed s:==TARGET==:$2:g "$back" >> "$path/$back" 45 46 $EDITOR "$path/$back" "$path/$front" "$path/extra.html" "$1"