---
tyesterday (1969B)
---
1 #!/usr/local/plan9/bin/rc
2
3 . 9.rc
4
5 smon='s/Jan/01/
6 s/Feb/02/
7 s/Mar/03/
8 s/Apr/04/
9 s/May/05/
10 s/Jun/06/
11 s/Jul/07/
12 s/Aug/08/
13 s/Sep/09/
14 s/Oct/10/
15 s/Nov/11/
16 s/Dec/12/'
17
18
19 fn usage {
20 echo 'usage: yesterday [-cd] [-[[yy]yy]mm]dd] [-n daysago] file ...' >[1=2]
21 exit 1
22 }
23
24 fn Xcp {
25 echo cp $1 $2
26 cp $1 $2
27 }
28
29 fn Xcarefulcp {
30 if(! cmp -s $1 $2) Xcp $1 $2
31 }
32
33 fn Xdiff {
34 echo diff -c $1 $2
35 diff -c $1 $2
36 }
37
38 fn Xecho {
39 echo $1
40 }
41
42 year=`{date|sed 's/.* //'}
43 copy=Xecho
44 last=()
45 while(! ~ $#* 0 && ~ $1 -* && ! ~ $1 --){
46 switch($1){
47 case -c
48 copy=Xcp
49 shift
50 case -d
51 copy=Xdiff
52 shift
53 case -C
54 copy=Xcarefulcp
55 shift
56 case -n*
57 if(~ $1 -n){
58 if(~ $#* 1)
59 usage
60 shift
61 days=$1
62 }
63 if not
64 days=`{echo $1 | sed 's/^-.//'}
65 last=`{date `{hoc -e `{date -n} ^ '-'$days'*60*60*24'} | \
66 sed -e 's%... (...) (..) ..:..:.. ... (....)%\3/\1\2%' -e 'y/ /0/' -e $smon}
67 shift
68 case -[0-9]
69 mon=`{date|9 sed 's/^....(...).*/\1/' -e $smon}
70 last=$year/$mon ^`{echo $1|sed 's/^-/0/'}
71 shift
72 case -[0-9][0-9]
73 mon=`{date|9 sed 's/^....(...).*/\1/' -e $smon}
74 last=$year/$mon ^`{echo $1|9 sed 's/^-//'}
75 shift
76 case -[0-9][0-9][0-9][0-9]
77 last=$year/ ^ `{echo $1|9 sed 's/^-//'}
78 shift
79 case -[0-9][0-9][0-9][0-9][0-9][0-9]
80 last=`{echo $year|9 sed 's/..$//'} ^ `{echo $1|9 sed 's/^-(..)/\1\//'}
81 shift
82 case -[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
83 last=`{echo $1|9 sed 's/^-(....)/\1\//'}
84 shift
85 case *
86 usage
87 }
88 }
89 if(! ~ $#* 0 && ~ $1 --)
90 shift
91
92 if(~ $#* 0)
93 usage
94
95 dir=`{pwd}
96 if(! ~ $status ''){
97 echo 'yesterday: can''t find directory' >[1=2]
98 exit 'pwd failed'
99 }
100
101 h=`{hostname}
102 switch($h){
103 case amsterdam
104 xdump=/dump/am
105 case *
106 if(! test -d /dump/$h){
107 echo 'no dumps on '^`{hostname} >[1=2]
108 exit 1
109 }
110 xdump=/dump/$h
111 }
112
113 for(i){
114 xpath=$i
115 if(! ~ $xpath /*)
116 xpath=`{9 cleanname -d `{pwd} $i}
117 dumppath=$xpath
118 if(~ $#last 0)
119 xlast=`{9 ls -t $xdump/$year|sed 1q}
120 if not
121 xlast=$xdump/$last
122 $copy $xlast^$dumppath $xpath
123 } |