summaryrefslogtreecommitdiff
path: root/atom/tstool.sh
blob: 44ff51560f2d1e04d1b5642ee049f5d0f2d98a10 (plain)
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
49
50
#!/bin/bash

#FILES=$(ls *.gmi)
SAVE_EXTENSION='original_time'

show_usage() {
  echo "Usage: $0 OPTION FILE..."
  echo "Save or restore the original timestamps of a collection of files."
  echo
  echo "  save    - save the original timestamps of files"
  echo "  restore - restore the original timestamps of files"
  echo
}

save_timestamps() {
  for file in "$@"
  do
    touch -amr $file $file.$SAVE_EXTENSION
    echo "$file timestamps saved to $file.$SAVE_EXTENSION"
  done
}

restore_timestamps() {
  ls *.$SAVE_EXTENSION > /dev/null 2>&1
  if [[ $? -gt 0 ]]; then
    echo "error: could not find stored timestamps"
    exit 1
  fi
  for file in $(ls *.$SAVE_EXTENSION)
  do
    if [[ ${file::-$((${#SAVE_EXTENSION} + 1))} -nt $file ]]; then
      touch -amr $file ${file::-$((${#SAVE_EXTENSION} + 1))}
      echo "${file::-$((${#SAVE_EXTENSION} + 1))} timestamps restored from $file"
    fi
    rm $file
  done
}

if [[ -z $1 || -z $2 ]]; then
  show_usage
else
  if [[ $1 == "save" ]]; then
    shift
    save_timestamps "$@"
  elif [[ $1 == "restore" ]]; then
    restore_timestamps
  else
    show_usage
  fi
fi