Advertisement
Guest User

joplin_desk_ins.bash

a guest
Jan 16th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.30 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # https://discourse.joplin.cozic.net/
  4.  
  5. # "I didn't get rid of the Hydra by smiling at them."
  6. #                                    -- Kara Danvers
  7.  
  8. # OS:
  9. # Tested ver. 1.0.120 on Ubuntu 16
  10. # Might also work on other Linux distributions
  11.  
  12. # Feature:
  13. # 1. Install Joplin desktop by one command,
  14. #        basically wrapping the official install script
  15. # 2. Kill the running Joplin desktop before installation.
  16. # 3. Backup the Joplin desktop db to the default directory.
  17. # 4. Create links to the Joplin desktop with version info
  18. #        (unavailable for versions perhaps before 1.0.110)
  19.  
  20. # Usage:
  21. # 1. Manually run the command as non-root
  22. #        ./joplink_desk_ins.bash
  23. #    A temporary file of "joplin_install_and_update.bash"
  24. #        will be created in the /tmp dir.
  25. #    To change this path, modify the value of DPATH_TMP.
  26. # 2. By default, the backup of note data will be copied
  27. #        to the predefined path of ~/joplin_db_bak
  28. #    To change this path,
  29. #        modify the value of DPATH_BAK.
  30. #    To disable the note backup,
  31. #        comment out the line of DPATH_BAK=
  32. # 3. To diable making links,
  33. #        comment out the line of DPATH_JOPLIN_DESK_APP=
  34. # 4. Write your own functions and put them in the main function
  35. #        when you need to extend the script
  36. #    Or you can modify the script at your own will
  37. # 5. To run the script by cron job as non-root user (required),
  38. #        try something like this:
  39. #        2 12 * * * sudo -u username "/path/to/joplin_desk_ins.bash"
  40. #        then to update the joplin automatically
  41.  
  42. # Caution:
  43. # The script might be buggy
  44. #     so backup your data first
  45. #     and use the script at your own risks
  46.  
  47.  
  48. SET_DEFAULT_VALUES(){
  49.     ## Paths for original data file
  50.     JOPLIN_DESK_CONF_DIRNAME="joplin-desktop"
  51.     DPATH_CONF=~/.config/"$JOPLIN_DESK_CONF_DIRNAME"
  52.     FPATH_DATA="$DPATH_CONF"/database.sqlite
  53.  
  54.     ## Paths for backup files
  55.     # Set the path for the backup dir you prefer
  56.     #     with proper permissions
  57.     # Comment out DPATH_BAK= to disable backup of data
  58.     DPATH_BAK=~/joplin_db_bak
  59.  
  60.     ## Path for temporary files
  61.     DPATH_TMP=/tmp
  62.  
  63.     ## Paths for link files
  64.     # Comment out DPATH_JOPLIN_DESK_APP= to disable making links
  65.     DPATH_JOPLIN_DESK_APP=~/.joplin
  66.     JOPLIN_APPNAME="Joplin.AppImage"
  67.  
  68.     # BINPATH_HOME is used to hold bin files or joplin link
  69.     #     with proper permissions of the $HOME user
  70.     #     and should be in the PATH of ~/profile
  71.     # Leave it commented out if it's not needed
  72.  
  73.     # BINPATH_HOME="~/bin"
  74. }
  75.  
  76. get_datetime(){
  77.     datef=`date '+%Y%m%dT%H%M%S'`
  78.     echo "$datef"
  79. }
  80.  
  81. is_unused_path(){
  82.     fpath="$1"
  83.     [ "y" == "y$fpath" ] && return 1
  84.     [ -e "$fpath" ] && return 64
  85.     [ -h "$fpath" ] && return 65
  86.     return 0
  87. }
  88.  
  89. mkdir_chk(){
  90.     dpath="$1"
  91.     is_unused_path "$dpath"
  92.     retval=$?
  93.     [ 1 -le $retval ] && return 1
  94.     mkdir "$dpath"
  95. }
  96.  
  97. init(){
  98.     SET_DEFAULT_VALUES
  99.     mkdir_chk "$DPATH_BAK"
  100.     mkdir_chk "$BINPATH_HOME"
  101. }
  102.  
  103. backup_sqlite(){
  104.     [ ! -d "$DPATH_BAK" ] && return 1
  105.     fpath_data_bak="$DPATH_BAK"/database.sqlite.$(get_datetime)
  106.     cp "$FPATH_DATA" "$fpath_data_bak"
  107. }
  108.  
  109. kill_app(){
  110.     # Don't kill the ppid of Joplin.AppImage
  111.     # It would crash the Gnome Desktop
  112.     pid_img=$(pidof Joplin.AppImage)
  113.     pid_jopl=$(pidof joplin)
  114.    
  115.     if [ -n "$pid_img" -o -n "$pid_jopl" ]
  116.         then
  117.         echo "Joplin is running. Killing it now.."
  118.         [ -n "$pid_img" ] && kill $pid_img
  119.         [ -n "$pid_jopl" ] && kill $pid_jopl
  120.     fi
  121. }
  122.  
  123. install(){
  124.     if [ ! -d "$DPATH_TMP" ]
  125.         then
  126.         echo "ERR:: DPATH_TMP: Invalid Path: $DPATH_TMP"
  127.         echo "ERR:: Installation is aborted"
  128.         return 1
  129.     fi
  130.    
  131.     fpath="$DPATH_TMP"/joplin_install_and_update.bash
  132.     # On Linux, if it works with your distribution
  133.     #     (it has been tested on Ubuntu, Fedora, Gnome and Mint),
  134.     #     the recommended way is to use this script
  135.     #     as it will handle the desktop icon too:
  136.     url="https://raw.githubusercontent.com/laurent22/joplin/master/Joplin_install_and_update.sh"
  137.     wget -O "$fpath" "$url"
  138.     chmod u+x "$fpath"
  139.     bash "$fpath"
  140. }
  141.  
  142. rm_chk(){
  143.     fpath="$1"
  144.     is_unused_path "$fpath"
  145.     retval=$?
  146.     [ 1 -ge $retval ] && return 1
  147.     rm "$fpath"
  148. }
  149.  
  150. mklink_binhome(){
  151.     fpath_joplin_app_link="$1"
  152.  
  153.     [ ! -d "$BINPATH_HOME" ] && return 1
  154.  
  155.     fpath_binhome_link="$BINPATH_HOME"/joplin-desk
  156.     rm_chk "$fpath_binhome_link"
  157.     ln -s "$fpath_joplin_app_link" "$fpath_binhome_link"
  158. }
  159.  
  160. mklink(){
  161.     [ ! -d "$DPATH_JOPLIN_DESK_APP" ] && return 1
  162.  
  163.     fpath_ver="$DPATH_JOPLIN_DESK_APP"/VERSION
  164.     if [ ! -f "$fpath_ver" ]
  165.         then
  166.         echo "INFO:: fpath_ver: Invalid Path: $fpath_ver"
  167.         echo "INFO:: The making of links is disabled."
  168.         return 1
  169.     fi
  170.  
  171.     # Make a link with version number under the joplin home path    
  172.     ver=`cat "$fpath_ver"`
  173.     fpath_joplin_app="$DPATH_JOPLIN_DESK_APP"/"$JOPLIN_APPNAME"
  174.     fpath_joplin_app_link="$DPATH_JOPLIN_DESK_APP"/"$JOPLIN_APPNAME"-desk-$ver
  175.  
  176.     rm_chk "$fpath_joplin_app_link"
  177.     ln -s "$fpath_joplin_app" "$fpath_joplin_app_link"
  178.  
  179.     mklink_binhome "$fpath_joplin_app_link"
  180. }
  181.  
  182. main(){
  183.     init
  184.     backup_sqlite
  185.     kill_app
  186.     install
  187.     mklink
  188. }
  189.  
  190. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement