| | | |
| 1 | 1 | | #!/bin/bash |
| 2 | 2 | | # |
| 3 | 3 | | #A script to compile .tex files to .pdf (and optionally, .rtf or .html) |
| 4 | 4 | | #with bibliography support. |
| 5 | 5 | | # |
| 6 | 6 | | #Depends on: latex, bibtex, dvips, ps2pdf, |
| 7 | 7 | | #Suggests: latex2rtf, latex2html, evince (or any other pdf viewer) |
| 8 | 8 | | # |
| 9 | 9 | | #Written by: J.Barney |
| 10 | 10 | | #Last updated: 22.10.10 |
| 12 | 12 | | #Save working directory to come back to |
| 13 | 13 | | OLDDIR="`pwd`" |
| 15 | 15 | | #Check directory |
| 16 | 16 | | echo "" |
| 17 | 17 | | read -p "Is you latex project in the current directory? y|[n]" switchdir |
| 18 | 18 | | echo "" |
| 20 | 20 | | #Select directory besides the working one |
| 21 | | - | if [ "$switchdir" = "n" ] || [ "$switchdir" = "N" ] || [ "$switchdir" = "no" ] || [ "$switchdir" = "No" ] || [ "$switchdir" = "" ]; then |
| 21 | + | if [[ $switchdir =~ n|N|(no)|(No) ] || [[ -z $switchdir ]]; then |
| 22 | 22 | | texdirectory=$(zenity --file-selection --directory --title="Select DIRECTORY of the .tex file"); |
| 23 | 23 | | cd "$texdirectory" |
| 24 | 24 | | fi |
| 26 | 26 | | #Optional backup |
| 27 | 27 | | read -p "Would you like to create a backup before compiling? [y]|n" backup |
| 28 | | - | if [ "$backup" = "y" ] || [ "$backup" = "Y" ] || [ "$backup" = "yes" ] || [ "$backup" = "Yes" ] || [ "$backup" = "" ] ; then |
| 28 | + | if [[ $backup =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $backup ]]; then |
| 29 | 29 | | CURDIR="`pwd`" |
| 30 | | - | if [ ! -e "$CURDIR/backups" ]; then |
| 30 | + | if [[ ! -e $CURDIR/backups ]]; then |
| 31 | 31 | | echo "Backups directory not detected, making one..." |
| 32 | 32 | | mkdir "$CURDIR/backups" |
| 33 | 33 | | fi |
| 34 | 34 | | BACKUPDATE=`date +%m_%d_at_%H_%M` |
| 35 | 35 | | cd "$CURDIR/backups" |
| 36 | 36 | | nice tar cvpzf backup_$BACKUPDATE.tar --exclude="$CURDIR/backups" "$CURDIR/*" |
| 37 | 37 | | cd "$CURDIR" |
| 38 | 38 | | else |
| 39 | 39 | | echo "Warning: We are NOT making a backup!" |
| 40 | 40 | | echo "" |
| 41 | 41 | | fi |
| 43 | 43 | | #Select .tex file |
| 44 | 44 | | #Check to see if there is only one file -- use it if so." |
| 45 | | - | if [ `ls *.tex | wc -l` -gt 1 ]; then |
| 45 | + | if [[ `ls *.tex | wc -l` -gt 1 ]]; then |
| 46 | 46 | | TEXLIST="`ls *.tex | sed 's/.tex$//'`" |
| 47 | 47 | | echo "Select .tex file" |
| 48 | 48 | | # TODO: warning this does not work with spaces in filenames! |
| 49 | 49 | | select texfile in $TEXLIST |
| 50 | 50 | | do |
| 51 | 51 | | break |
| 52 | 52 | | done |
| 53 | 53 | | else |
| 54 | 54 | | texfile="`ls *.tex`" |
| 55 | 55 | | texfile="{texfile%.tex}" |
| 56 | 56 | | echo "Using 1 found .tex file: $texfile" |
| 57 | 57 | | fi |
| 58 | 58 | | echo "" |
| 60 | 60 | | #Check for .bib |
| 61 | | - | if [ -e *.bib ]; then |
| 61 | + | if [[ -e *.bib ]] ; then |
| 62 | 62 | | read -p ".bib file found. Include it? [y]|n" biblio |
| 63 | 63 | | echo "" |
| 64 | | - | if [ "$biblio" = "y" ] || [ "$biblio" = "Y" ] || [ "$biblio" = "yes" ] || [ "$biblio" = "Yes" ] || [ "$biblio" = "" ]; then |
| 64 | + | if [[ $biblio =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $biblio ]]; then |
| 65 | 65 | | #reset biblio for easy reuse (see further down) |
| 66 | 66 | | biblio="yes" |
| 67 | 67 | | #Select if more than 1 .bib file |
| 68 | | - | if [ `ls *.bib | wc -l` -gt 1 ]; then |
| 68 | + | if [[ `ls *.bib | wc -l` -gt 1 ]]; then |
| 69 | 69 | | BIBLIST="`ls *.bib | sed 's/.bib$//'`" |
| 70 | 70 | | echo "Multiple .bibs found, select one" |
| 71 | 71 | | # TODO: warning this does not work with spaces in filenames! |
| 72 | 72 | | select biblioname in $BIBLIST |
| 73 | 73 | | do |
| 74 | 74 | | break |
| 75 | 75 | | done |
| 76 | 76 | | echo "" |
| 77 | 77 | | else |
| 78 | 78 | | biblioname="`ls *.bib`" |
| 79 | 79 | | biblioname="{biblioname%.bib}" |
| 80 | 80 | | echo "Using 1 found .bib file: $biblioname" |
| 81 | 81 | | sleep 2s |
| 82 | 82 | | fi |
| 83 | 83 | | else |
| 84 | 84 | | #reset biblio for easy reuse (see further down) |
| 85 | 85 | | biblio="no" |
| 86 | 86 | | fi |
| 87 | 87 | | fi |
| 89 | 89 | | latex "${texfile}.tex" |
| 90 | 90 | | #biblio was reset to be either "yes" or "no" |
| 91 | | - | if [ "$biblio" = "yes" ] ; then |
| 91 | + | if [ $biblio = yes ]] ; then |
| 92 | 92 | | bibtex "${biblioname}.bib" |
| 93 | 93 | | fi |
| 94 | 94 | | latex "${texfile}.tex" |
| 95 | 95 | | latex "${texfile}.tex" |
| 96 | 96 | | dvips "${texfile}.div" |
| 97 | 97 | | ps2pdf "${texfile}.ps" "${texfile}.pdf" |
| 99 | 99 | | echo "" |
| 100 | 100 | | echo "Compilation complete." |
| 101 | 101 | | echo "" |
| 102 | 102 | | read -p "Create .rtf version now? [y]|n" rtfnow |
| 103 | | - | if [ "$rtfnow" = "y" ] || [ "$rtfnow" = "Y" ] || [ "$rtfnow" = "yes" ] || [ "$rtfnow" = "Yes" ] || [ "$rtfnow" = "" ]; then |
| 103 | + | if [[ $rtfnow =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $rtfnow ]]; then |
| 104 | 104 | | latex2rtf "${texfile}.tex" |
| 105 | 105 | | fi |
| 106 | 106 | | echo "" |
| 107 | 107 | | read -p "Create .html version now? [y]|n" htmlnow |
| 108 | | - | if [ "$htmlnow" = "y" ] || [ "$htmlnow" = "Y" ] || [ "$htmlnow" = "yes" ] || [ "$htmlnow" = "Yes" ] || [ "$htmlnow" = "" ]; then |
| 108 | + | if [[ $htmlnow =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $htmlnow ]]; then |
| 109 | 109 | | latex2html "${texfile}.tex" |
| 110 | 110 | | fi |
| 111 | 111 | | echo "" |
| 112 | 112 | | read -p "Open .pdf now? (Y/n): " opennow |
| 113 | | - | if [ "$opennow" = "y" ] || [ "$opennow" = "Y" ] || [ "$opennow" = "yes" ] || [ "$opennow" = "Yes" ] || [ "$opennow" = "" ]; then |
| 113 | + | if [[ $opennow =~ y|Y|(yes)|(Yes)|(YES) ]] || [[ -z $opennow ]]; then |
| 114 | 114 | | evince "${texfile}.pdf" & |
| 115 | 115 | | fi |
| 116 | 116 | | cd "$OLDDIR" |
| 117 | 117 | | exit 0 |
| 118 | 118 | | #end |