|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# v1.0.0 |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# - Call script to register sync script with launchd. |
| 7 | +# - Call with `--no-logs` to disable logging. |
| 8 | +# - Call with `--uninstall` or `--remove` to unregister from launchd and clean up files. |
| 9 | + |
| 10 | +# Adjust the paths to match your system (do not end the path with /). |
| 11 | +# Path to local (working) projects folder |
| 12 | +local_path="${HOME}/LocalDocs/Projects" |
| 13 | + |
| 14 | +# Path to cloud projects folder (node_modules, etc. are omitted). |
| 15 | +# |
| 16 | +# Note: if you're using iCloud on a system before Sierra, the Documents folder |
| 17 | +# can be found at "${HOME}/Library/Mobile Documents/com~apple~CloudDocs" |
| 18 | +cloud_path="${HOME}/Documents/Projects" |
| 19 | + |
| 20 | +# Comma-separated list of files to ignore. |
| 21 | +# Example: "node_modules,*.log" -> ignore all paths containing `node_modules` and any files ending with `*.log`. |
| 22 | +# For more details see: http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#ignore |
| 23 | +ignore_files="node_modules,bower_components,*.log,.DS_Store" |
| 24 | + |
| 25 | +# If you want, change log destination here (irellevant with --no-logs flag). |
| 26 | +log_file="/var/log/${label}.out.log" |
| 27 | +err_file="/var/log/${label}.err.log" |
| 28 | + |
| 29 | +########################################################################## |
| 30 | +# No need to modify the code below, unless you know what you're doing :D # |
| 31 | +########################################################################## |
| 32 | + |
| 33 | +# Path to script and launchd config. |
| 34 | +label="com.markogresak.projects.CloudSyncIgnore" |
| 35 | +script_path="/usr/local/bin/${label}.sh" |
| 36 | +plist_path="${HOME}/Library/LaunchAgents/${label}.plist" |
| 37 | + |
| 38 | +# If config already exists, unload it before updating it. |
| 39 | +if [ -f $plist_path ]; then |
| 40 | + launchctl unload $plist_path |
| 41 | +fi |
| 42 | + |
| 43 | +if [[ "$1" == "--uninstall" || "$1" == "--remove" ]]; then |
| 44 | + rm -f $script_path $plist_path |
| 45 | + if [ -f $log_file ] || [ -f $err_file ]; then |
| 46 | + echo "The script will attempt to remove log files. This requires sudo access, so the shell will ask you for password." |
| 47 | + sudo rm -f $log_file $err_file |
| 48 | + fi |
| 49 | + echo "Sync script successfully removed. Thanks for giving it a chance. If you have any suggestions for improvement, please let me know by submitting an issue." |
| 50 | + exit |
| 51 | +fi |
| 52 | + |
| 53 | +# Check for unison command and fail if not found. |
| 54 | +if ! command -v unison >/dev/null 2>&1; then |
| 55 | + echo "Command 'unison' not found. Install it (brew install unison) and try this script again." |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +# If `--no-logs` flag is used, use /dev/null as stdout and stderr. |
| 60 | +if [[ "$1" == "--no-logs" ]]; then |
| 61 | + log_file="/dev/null" |
| 62 | + err_file="/dev/null" |
| 63 | +else |
| 64 | + echo "The script will attempt to create log files. This requires sudo access, so the shell will ask you for password." |
| 65 | + |
| 66 | + # Create/clear log files (requires sudo to allow modifying files in /var/log) and fix log file permissions. |
| 67 | + sudo sh -c 'echo "" > $0' "$log_file" |
| 68 | + sudo sh -c 'echo "" > $0' "$err_file" |
| 69 | + sudo chown `whoami` "$log_file" "$err_file" |
| 70 | + echo -e "Log files were successfully created.\n" |
| 71 | +fi |
| 72 | + |
| 73 | +# Create actual files based of .template files. |
| 74 | +sed "s|{{LOCAL_PATH}}|${local_path}|; |
| 75 | + s|{{CLOUD_PATH}}|${cloud_path}|; |
| 76 | + s|{{SCRIPT_PATH}}|${script_path}|; |
| 77 | + s|{{LABEL}}|${label}|; |
| 78 | + s|{{LOG_FILE}}|${log_file}|; |
| 79 | + s|{{ERR_FILE}}|${err_file}|" plist.template > $plist_path |
| 80 | +sed "s|{{UNISON_PATH}}|$(which unison)|; |
| 81 | + s|{{IGNORE_FILES}}|${ignore_files}|; |
| 82 | + s|{{LOCAL_PATH}}|${local_path}|; |
| 83 | + s|{{CLOUD_PATH}}|${cloud_path}|;" script.template > $script_path |
| 84 | + |
| 85 | +# Load launchd config. |
| 86 | +launchctl load $plist_path |
| 87 | + |
| 88 | +echo "Sync script added. It will be triggered any time any of files inside local or iCloud project folder changes." |
| 89 | +echo "I hope this script will help make your life a little easier :)" |
0 commit comments