Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.2 KiB

  1. #!/bin/bash
  2. ## zaread - a simple script created by paoloap.
  3. # default variables
  4. zadir="$XDG_DATA_HOME"'/.zaread/'
  5. reader="zathura"
  6. # if ~/.zaread doesn't exist, we create it.
  7. if [[ ! -d "$zadir" ]]; then
  8. mkdir "$zadir"
  9. mkdir "$zadir"cksum
  10. fi
  11. # if no arguments exit.
  12. if [[ -z $@ ]]; then exit 1; fi
  13. # if zathura is not installed, we force the user to choose a pdf reader
  14. # after three wrong commands, the script exits 1
  15. # if the user inserts a command that exists but is not a pdf reader then... then fuck him.
  16. counter=0
  17. while [[ -z `command -v "$reader"` ]]; do
  18. if [ $counter -gt 3 ]; then exit 1; fi
  19. let counter+=1
  20. echo "Seems that you don't have zathura installed. Please choose an installed PDF reader:"
  21. read reader
  22. done
  23. echo "We'll read PDF with $reader."
  24. ## create position and file variables ##
  25. # complete file name (path excluded):
  26. file=`echo "$@" | rev | cut -d'/' -f1 | rev`
  27. # complete directory path:
  28. # if it has been inserted absolute path ($@ starts with '/')
  29. if [[ $@ =~ ^/ ]]; then
  30. directory=`echo "$@" | rev | cut -d'/' -f2- | rev`"/"
  31. # else (relative path inserted)
  32. else
  33. dir=`pwd`"/"`echo "$@" | sed 's|.[^/]*$||'`"/"
  34. directory=`echo "$dir" | sed 's|//|/|'`
  35. fi
  36. echo "$directory""$file"
  37. # if file type is pdf, then just read the file
  38. if [[ `file "$directory""$file" | cut -d':' -f2 | cut -d' ' -f2` == "PDF" ]]; then
  39. echo "The file is already in PDF format."
  40. $reader "$directory""$file"
  41. # else check if you already have its pdf version (if not, create it)
  42. else
  43. pdffile=`echo "$file" | rev | cut -d'.' -f2- | rev`".pdf"
  44. check=`cksum "$directory""$file" | awk '{print $1}'`
  45. # if pdf version hasn't ever been created, or it changed, then
  46. # make conversion and store the checksum.
  47. if [[ ( ! -f "$zadir$pdffile" ) || ( ! "$check" == `cat "$zadir"cksum/"$file".check` ) ]]; then
  48. # if it's a mobi file, then convert it to epub (the command depends on calibre)
  49. if [[ "$file" =~ ^.*\.mobi$ ]]; then
  50. ebook-converter "$directory""$file" "$directory"`echo "$file" | sed 's/mobi$/epub/'`
  51. else
  52. libreoffice --convert-to pdf "$directory""$file" --headless --outdir "$zadir"
  53. fi
  54. echo "$check" > "$zadir"cksum/"$file".check
  55. fi
  56. $reader "$zadir$pdffile"
  57. fi