#! /bin/bash function set_messages () { MGROUP="Choose tematic groups the article belongs to" WSTEP="\ Addbiblio\n\ Add a bibliography entry to a BibTeX database \n (version $VERSION, 2000, JW3)" MENU="LABEL \$LABEL\ \ TYPE \"$TYPE\" \ AUTHOR \"$AUTHOR\" \ TITLE \"$TITLE\" \ JOURNAL \"$JOURNAL\" \ VOLUME \"$VOLUME\" \ PAGES \"$PAGES\" \ GROUP \"$GROUP\" \ ";} # ------------------------------------------------------------------ function param () { echo $1 ; } # ------------------------------------------------------------------ type_entry () { echo -e "@$TYPE={$LABEL, \n\tAUTHOR=\"{$AUTHOR}\", \n\tTITLE=\"{$TITLE}\", \ \n\tYEAR={$YEAR}, \n\tJOURNAL={$JOURNAL}, \n\tVOLUME={$VOLUME}, \ \n\tPAGES={$PAGES}, \n\tGROUP={$GROUP} }" ; } # ------------------------------------------------------------------ save_record () { if test -z "$LABEL" ; then dialog --msgbox "Record has no label!\n*not* saved...." 10 50 return fi test_label type_entry >> $TMPFIL.rec MODIF="" ; initialize return ; } # ------------------------------------------------------------------ browse () { end="" origdir=`pwd` while test -z "$end" ; do curdir=`pwd` dirlist=`find $pwd -maxdepth 1 -type d -printf "%P\n" | sort \ | tail +2 | sed "s/.*/&\/ dir /g" | egrep -v "^\." | tr '\n' ' '` filist=`find $pwd -maxdepth 1 -type f -printf "%P %s\n " |\ egrep -v "^ *\." | sort` BTITLE="Directory: $curdir" BMENU="../ dir $dirlist $filist" if ! dialog --menu "$BTITLE" 25 60 18 $BMENU 2> $TMPFIL then end="yes" else newfil=`cat $TMPFIL` if test -f "$newfil" ; then check_file $curdir/$newfil if [ "$CHECKRESULT" == "nasci" ] ; then msg="File $newfil is not a text file!\n" msg="$msg `file $curdir/$newfil`" dialog --msgbox "$msg" 10 60 elif [ "$curdir" == "$origdir" ] ; then SAVFIL="$newfil" end="yes" else SAVFIL="$curdir/$newfil" end="yes" fi else curdir="${curdir}/${newfil}" ; cd $curdir fi fi done return ; } # ------------------------------------------------------------------ function save_data () { if ! test -f $TMPFIL.rec ; then dialog --msgbox "No new records to save" 5 30 elif dialog --inputbox "Append records to file:" 10 50 "$SAVFIL" 2> $TMPFIL then SAVFIL=`cat $TMPFIL` recnum=`cat $TMPFIL.rec | tee -a $SAVFIL | grep "@" | wc -l` rm $TMPFIL.rec dialog --msgbox "$recnum record(s) saved in $SAVFIL" 5 40 else dialog --msgbox "Data *not* saved" 10 50 fi ; } # ------------------------------------------------------------------ function go_exit () { if test -f "$TMPFIL.rec" ; then if dialog --yesno "Exiting:\nSave data first?" 7 25 ; then save_data else dialog --msgbox "Data was not saved" 7 25 fi fi clear ; return ; } # ------------------------------------------------------------------ print_fortune () { lin=30 ; while [ "$lin" -gt 20 ] ; do fortune | fmt -65 > $TMPFIL.f lin=`cat $TMPFIL.f | wc -l` done lin=$[ $lin + 5 ] ; FTEXT="`cat $TMPFIL.f | tr '\n' '%' | \ sed 's/%/\\\\n/g;s/\"/\\\"/g'`" ; dialog --title "Fortune" --msgbox "$FTEXT" $lin 70 return ; } # ------------------------------------------------------------------ function generate_tempfile () { i=0 tmpcor=$1 postfix=$2 tmpfile=$tmpcor$postfix while test -f $tmpfile ; do i=$[ $i + 1 ] tmpfile=$tmpcor$i$postfix done ; echo $tmpfile ; } # ------------------------------------------------------------------ check_file () { res="new" if test -e "$1" ; then res="exists" ; FILETYP=`file $1` if ! echo "$FILETYP" | grep text ; then res="nasci" ; fi fi CHECKRESULT=$res return ; } # ------------------------------------------------------------------ choose_database () { if dialog --inputbox "Input the database" 10 50 "$SAVFIL" 2> $TMPFIL then set="yes" TSAVFIL=`cat $TMPFIL` check_file $TSAVFIL case $CHECKRESULT in exists) SAVFIL=$TSAVFIL MESG="Using existing database\n $SAVFIL" ;; new) SAVFIL=$TSAVFIL MESG="Creating a new file\n $SAVFIL" ;; nasci) MESG="File\n $TSAVFIL\n exists, but it is not a text file!\n" MESG="$MESG $FILETYP" ;; esac dialog --msgbox "$MESG" 10 40 fi ; } # ------------------------------------------------------------------ list_entries () { if ! test -f "$SAVFIL" ; then dialog --msgbox "No entries in $SAVFIL" 10 20 return fi ETITLE="\"Entries from $SAVFIL\nSelect record to see it!\"" EMENU="" dialog --infobox "Scanning file $SAVFIL" 5 40 cat $SAVFIL | tr '\n@' '\t\n' | sed "s/.\{1,\}/@&/" > $TMPFIL cat $TMPFIL | sed "s/[^{]*{[ ]*\([^ ,]*\).*/\1/;s/[ ]*=[ ]*/=/g" \ | sort > $TMPFIL.a EMENU="`cat $TMPFIL.a | tr '\n' ' ' | \ sed 's/[^ ]\{1,\}/& \"&\" /g'`" while eval "dialog --menu $ETITLE 25 40 18 $EMENU" 2> $TMPFIL.b do KEY=`cat $TMPFIL.b` EDIAL="\"Records matching label $KEY" EDIAL="$EDIAL\n"`cat $TMPFIL | grep "$KEY" | fmt -65 | \ tr '\n\t' '% ' | \ sed 's/%/\\\\n/g;s/\"/\\\\\"/g;s/ */ /g;'"s/$aps/$back$aps/g;s/@/$bb\n/g"`\" eval dialog --msgbox "$EDIAL" 20 70 done return ; } # ------------------------------------------------------------------ initialize () { MODIF="" ; YEAR=`date +%Y` TASK="INIT" AUTHOR="" ; TITLE="" ; JOURNAL="" ; VOLUME="" ; PAGES="" ; GROUP="MISC" ; LABEL="" ; TYPE="ARTICLE" ; } # ------------------------------------------------------------------ print_types () { echo -n " ARTICLE" \"Standard article\" on echo -n " MISC" \"Everything else\" off ; } # ------------------------------------------------------------------ choose_type () { CMLINE="dialog --radiolist \"$MGROUP\" 25 70 15 `print_types`" eval $CMLINE 2> $TMPFIL TYPE=`cat $TMPFIL | tr ' ' ','` return ; } # ------------------------------------------------------------------ print_groups () { echo -n " MYCOPLASMA" \"All about mycoplasmas\" off echo -n " RNA" \"All about RNA\" off echo -n " MISC" \"Everything else\" on ; } # ------------------------------------------------------------------ choose_group () { CMLINE="dialog --checklist \"$MGROUP\" 25 70 15 `print_groups`" eval $CMLINE 2> $TMPFIL GROUP=`cat $TMPFIL | tr ' ' ','` ; } # ------------------------------------------------------------------ make_label () { LABEL=`param $AUTHOR | tr A-Z a-z | \ sed "s/[^a-z]//g"``echo $YEAR | sed "s/^..//"` ; } test_label () { koniec="" ; while test -z "$koniec" && test -n "$SAVFIL" && grep "$LABEL" $SAVFIL do DTITLE="Label $LABEL already present in file\n$SAVFIL.\n" DTITLE="$DTITLE Change label name?" if dialog --yesno "$DTITLE" 10 60 then choose_setup_task LABEL else koniec="yes" fi done ; } # ------------------------------------------------------------------ choose_setup_task () { TASK=$1 SSTRING="`eval echo '$'$1`" STITLE="$TASK\n\nCurrent value:\n" STITLE="$STITLE `echo $SSTRING | fmt -40 | \ tr '\n' '@' | sed 's/@/\\\\n/g'`" if dialog --inputbox "$STITLE" 18 50 "$SSTRING" 2> $TMPFIL.b then #DUPA=`cat $TMPFIL.b | sed "s/$apss/$back$back$apss/g"` #echo "$TASK=\"$DUPA\"" > $TMPFIL.d #. $TMPFIL.d DUPA="`cat $TMPFIL.b | sed 's/\"/\\\\\"/g'`" eval $TASK=$apss$DUPA$apss MODIF="yes" ; if test $TASK = "AUTHOR" -o $TASK = "YEAR" ; then make_label ; test_label ; fi fi ; } # ------------------------------------------------------------------ add_records () { TASK="" while test "$TASK" != "EXIT" do if ! print_record_menu 2> $TMPFIL ; then TASK="EXIT" ; fi case "$TASK" in SAVE) save_record ;; EXIT) if [ -n "$MODIF" ] ; then if ! dialog --yesno "Record was modified. Really abort?" 10 50 then TASK="" fi fi ;; TYPE) choose_type ;; GROUP) choose_group ;; *) choose_setup_task $TASK ;; esac done return ; } # ------------------------------------------------------------------ print_record_menu () { DMENU="AUTHOR \"$AUTHOR\" YEAR \"$YEAR\"" DMENU="$DMENU TITLE \"`echo $TITLE | \ sed 's/\(.\{40\}\).*/\1.../'` \"" DMENU="$DMENU VOLUME \"$VOLUME\" PAGES \"$PAGES\"" DMENU="$DMENU GROUP \"$GROUP\" LABEL \"$LABEL\" TYPE \"$TYPE\"" DMENU="$DMENU SAVE \"Save current record\"" DMENU="$DMENU EXIT \"Exit this menu\"" DTITLE="\"Choose the field you want to change\n" DTITLE="$DTITLE\nCurrent database file: $SAVFIL " if test -f $SAVFIL ; then lines=`cat $SAVFIL | grep "@" | wc -l` DTITLE="$DTITLE\n $lines entries" fi if test -f $TMPFIL.rec ; then lines=`cat $TMPFIL.rec | grep "@" | wc -l` DTITLE="$DTITLE\n New records added so far: $lines" fi DTITLE="$DTITLE \nCurrent entry:\n `type_entry \ | fmt -70 | tr '\n' '%' | sed 's/%/\\\\n/g;s/"/\\\\"/g'`\"" if eval "dialog --title \"Record menu\" --menu $DTITLE 25 75 12 $DMENU" 2>$TMPFIL then TASK=`cat $TMPFIL` else TASK="EXIT" fi ; } # ------------------------------------------------------------------ save_options () { date=`date` cat << EOF # Configuration file for program Addbib # created on $date DEFDB=$DEFDB EOF return ; } # ------------------------------------------------------------------ edit_options () { OTITLE="\"Set the default database\"" OMENU="AUTO \"Automatic database\" on" OMENU="$OMENU CURRENT \"Choose current database as default\" off" OMENU="$OMENU DB \"Choose another file as the default database\" off" OMENU="$OMENU CREATE \"Create new, default database\" off" if eval "dialog --title Options --radiolist $OTITLE 15 70 10 $OMENU" 2> $TMPFIL then answer=`cat $TMPFIL` case "$answer" in AUTO) DEFDB="AUTO" ;; DB) tmp=$SAVFIL ; browse ; DEFDB=$SAVFIL ; SAVFIL=$tmp ;; CREATE) tmp=$SAVFIL ; dialog --inputbox "Type a name:" 10 50 "$SAVFIL" 2> $TMPFIL.b DEFDB=`cat $TMPFIL.b` ;; CURRENT) DEFDB=$SAVFIL ;; esac fi save_options > $RCFIL if [ "$DEFDB" != "$SAVFIL" ] ; then if dialog --yesno "Default database set to $DEFDB.\n\ Switch also the current database to default?" 7 60 then SAVFIL="$DEFDB" fi fi return ; } # ------------------------------------------------------------------ print_main_menu () { echo "Wait..." DMENU="ADD \"Add records\"" DMENU="$DMENU DB \"Create new database\"" DMENU="$DMENU BROWSE \"Choose an existing database\"" DMENU="$DMENU EDIT \"Edit database with external editor\"" DMENU="$DMENU LIST \"List entries in the database\"" if test -f $TMPFIL.rec ; then DMENU="$DMENU SAVE \"Save added record(s) in $SAVFIL\"" fi DMENU="$DMENU OPTIONS \"Edit options\"" if which fortune > /dev/null ; then DMENU="$DMENU SURPRISE \"Find your good luck...\"" fi DMENU="$DMENU QUIT \"Exit addbib\"" DTITLE="\"Choose the task you want to perform\n" DTITLE="$DTITLE\nCurrent database file: $SAVFIL " if test -f $SAVFIL ; then lines=`cat $SAVFIL | grep "@" | wc -l` DTITLE="$DTITLE \n-- $lines entries" fi if test -f $TMPFIL.rec ; then lines=`cat $TMPFIL.rec | grep "@" | wc -l` DTITLE="$DTITLE\n New records added so far: $lines" fi DTITLE="$DTITLE\"" DTIT="\"Main menu, `date +'%d/%m/%y, %H:%M'`\"" if eval "dialog --title $DTIT --menu $DTITLE 25 75 12 $DMENU" 2>$TMPFIL then TASK=`cat $TMPFIL` else TASK="QUIT" fi ; return ; } # ======================================================================= # # ======================================================================= if ! which dialog > /dev/null ; then echo echo "Addbib needs the 'dialog' program by Savio Lam" echo "in order to work properly... sorry, exiting" echo exit 1 fi VERSION="0.02" set_messages initialize back='\' ; aps="'" ; apss='"'; bb='\\' TMPFIL=`generate_tempfile /tmp/addbib` RCFIL="$HOME/.addbibrc" SAVFIL=`generate_tempfile biblio .bib` if test -f "$RCFIL" ; then . $RCFIL ; fi if test -a "$DEFDB" -a "$DEFDB" != "AUTO" ; then SAVFIL=$DEFDB ; fi dialog --title "ADDBIB" --backtitle "Addbib" --msgbox "$WSTEP" 10 70 while [ $TASK != "QUIT" ] ; do if ! print_main_menu ; then TASK="QUIT" ; fi if [ "$TASK" == "QUIT" ] ; then go_exit elif [ "$TASK" == "EDIT" ] ; then if test -z "$EDITOR" ; then vi $SAVFIL else $EDITOR $SAVFIL fi elif [ "$TASK" == "BROWSE" ] ; then browse elif [ "$TASK" == "ADD" ] ; then add_records elif [ "$TASK" == "OPTIONS" ] ; then edit_options elif [ "$TASK" == "DB" ] ; then choose_database elif [ "$TASK" == "LIST" ] ; then list_entries elif [ "$TASK" == "SURPRISE" ] ; then print_fortune elif [ "$TASK" == "SAVE" ] ; then save_data elif [ "$TASK" == "GROUP" ] ; then choose_group else choose_setup_task $TASK fi done rm $TMPFIL*