common.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env bash
  2. CURRDIR="$(dirname $0)"
  3. DATADIR="$CURRDIR/data"
  4. CSVOUTPUT="$DATADIR/output.csv"
  5. DATADIR_OVPNS="$DATADIR/ovpns"
  6. csv_offset=2
  7. ## Default config values
  8. # How many connections to make available.
  9. ovpn_count=5
  10. # After how many hours the downloaded VPN connection list should be considered
  11. # old/stale and latest file should be (offered to be) downloaded.
  12. stale_threshold_hours=6
  13. # If set to true, update stale files without asking.
  14. auto_update_when_stale=true
  15. # Try to source config.sh if it exists.
  16. # If you want to override default config values try
  17. # cp -i config.sample.sh config.sh
  18. # and edit config.sh.
  19. [ -f "$CURRDIR/config.sh" ] && source "$CURRDIR/config.sh"
  20. # Detects if a file is old enough depending on a threshold of hours.
  21. function is_file_stale() {
  22. local filename="$1"
  23. # Redirection is to suppress output, but later get return code
  24. stat -q > /dev/null 2>&1
  25. if [ "$?" = '1' ]; then # GNU+Linux doesn't have -q option
  26. local changed=$(stat -c "%Y" "$filename")
  27. else # BSD etc.
  28. local changed=$(stat -f "%m" "$filename")
  29. fi
  30. local now=$(date +%s)
  31. # Difference between now and file modified time is more than the threshold
  32. if [ $[ $now - $changed ] -gt $[ $stale_threshold_hours * 60 * 60 ] ]; then
  33. return 1
  34. else
  35. return 0
  36. fi
  37. }