aboutsummaryrefslogtreecommitdiff
path: root/bin/yesterday
blob: 04ef31c75dd91ca863d09467a0778ac673cde54d (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/local/plan9/bin/rc

. 9.rc

smon='s/Jan/01/
	s/Feb/02/
	s/Mar/03/
	s/Apr/04/
	s/May/05/
	s/Jun/06/
	s/Jul/07/
	s/Aug/08/
	s/Sep/09/
	s/Oct/10/
	s/Nov/11/
	s/Dec/12/'


fn usage {
	echo 'usage: yesterday [-cd] [-[[yy]yy]mm]dd] [-n daysago] file ...' >[1=2]
	exit 1
}

fn Xcp {
	echo cp $1 $2
	cp $1 $2
}

fn Xcarefulcp {
	if(! cmp -s $1 $2) Xcp $1 $2
}

fn Xdiff {
	echo diff -c $1 $2
	diff -c $1 $2
}

fn Xecho {
	echo $1
}

year=`{date|sed 's/.* //'}
copy=Xecho
last=()
while(! ~ $#* 0 && ~ $1 -* && ! ~ $1 --){
	switch($1){
	case -c
		copy=Xcp
		shift
	case -d
		copy=Xdiff
		shift
	case -C
		copy=Xcarefulcp
		shift
	case -n*
		if(~ $1 -n){
			if(~ $#* 1)
				usage
			shift
			days=$1
		}
		if not
			days=`{echo $1 | sed 's/^-.//'}
		last=`{date `{hoc -e `{date -n} ^ '-'$days'*60*60*24'} | \
				sed -e 's%... (...) (..) ..:..:.. ... (....)%\3/\1\2%' -e 'y/ /0/' -e $smon}
		shift
	case -[0-9]
		mon=`{date|9 sed 's/^....(...).*/\1/' -e $smon}
		last=$year/$mon ^`{echo $1|sed 's/^-/0/'}
		shift
	case -[0-9][0-9]
		mon=`{date|9 sed 's/^....(...).*/\1/' -e $smon}
		last=$year/$mon ^`{echo $1|9 sed 's/^-//'}
		shift
	case -[0-9][0-9][0-9][0-9]
		last=$year/ ^ `{echo $1|9 sed 's/^-//'}
		shift
	case -[0-9][0-9][0-9][0-9][0-9][0-9]
		last=`{echo $year|9 sed 's/..$//'} ^ `{echo $1|9 sed 's/^-(..)/\1\//'}
		shift
	case -[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
		last=`{echo $1|9 sed 's/^-(....)/\1\//'}
		shift
	case *
		usage
	}
}
if(! ~ $#* 0 && ~ $1 --)
	shift

if(~ $#* 0)
	usage

dir=`{pwd}
if(! ~ $status ''){
	echo 'yesterday: can''t find directory' >[1=2]
	exit 'pwd failed'
}

h=`{hostname}
switch($h){
case amsterdam
	xdump=/dump/am
case *
	if(! test -d /dump/$h){
		echo 'no dumps on '^`{hostname} >[1=2]
		exit 1
	}
	xdump=/dump/$h
}

for(i){
	xpath=$i
	if(! ~ $xpath /*)
		xpath=`{9 cleanname -d `{pwd} $i}
	dumppath=$xpath
	if(~ $#last 0)
		xlast=`{9 ls -t $xdump/$year|sed 1q}
	if not
		xlast=$xdump/$last
	$copy $xlast^$dumppath $xpath
}