-
Notifications
You must be signed in to change notification settings - Fork 0
/
unisonHomeDirectory
executable file
·48 lines (43 loc) · 1.46 KB
/
unisonHomeDirectory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /bin/bash
#
readonly unisonProfile='roamingRemoteHome'
readonly unisonPrefer='newer'
readonly unisonLog='unisonCronJob.log'
readonly lockFilePath="${HOME}/.unison/unisonCronjob.lock"
readonly testHost='10.6.0.71'
readonly testPort='5001'
batteryLevel="$(cat /sys/class/power_supply/BAT*/capacity)"
acStatus="$(cat /sys/class/power_supply/BAT*/status)"
# Check for lockfile, create if it doesn't exist
echo "Unison CronJob..."
echo "Checking if lock file at ${lockFilePath} exists."
if [ -f "${lockFilePath}" ]; then
echo "Unison Cronjob can't start, lock file exists."
exit 1
else
echo "Lock file does not exist, making one now."
echo $$ > "${lockFilePath}"
# Register a trap to clean up the lockfile
trap 'rm -fv ${lockFilePath}' 0 2 3 15
fi
# Run tests
echo "Testing if host ${testHost} is reachable"
if ! ping -c 3 "${testHost}"; then
echo "Can't ping unison host at ${testHost}, exiting."
exit 1
fi
echo "Testing if port ${testPort} is reachable on host ${testHost}"
if ! nc -z "${testHost}" "${testPort}"; then
echo "Can't reach unison port at ${testPort}, exiting."
exit 1
fi
echo "Testing if laptop is on AC or if battery is low"
if [[ "${acStatus}" == 'Discharging' && "${batteryLevel}" -lt 30 ]]; then
echo "Battery discharging and lower than ${batteryLevel}, exiting."
exit 1
fi
echo "Running unison with profile ${unisonProfile}.prf..."
unison -auto -batch "$@" \
-logfile "./${unisonLog}" \
-prefer "${unisonPrefer}" \
"${unisonProfile}.prf"