1fn webring_init {
2 switch ($req_path) {
3 case /prev/*
4 target=`{echo $req_path | sed 's!^/prev/!!'}
5 webring_navigate -1 $target
6 exit
7 case /next/*
8 target=`{echo $req_path | sed 's!^/next/!!'}
9 webring_navigate 1 $target
10 exit
11 case /random
12 webring_random
13 exit
14 }
15}
16
17fn webring_navigate {
18 step = $1
19 pat = $2
20
21 list_file = $sitedir/list.txt
22
23 num=`{awk -v pat=$pat '$0 ~ pat {print NR; exit}' $list_file}
24
25 if (~ $#num 0) {
26 echo 'Pattern not found in the webring list.'
27 exit
28 }
29
30 lines=`{wc -l < $list_file}
31
32 thing=`{awk -v n=$num -v total=$lines -v step=$step '
33 BEGIN {
34 n = n + step
35 if (n < 1)
36 n = total
37 else if (n > total)
38 n = 1
39 }
40 NR == n {
41 print
42 exit
43 }' $list_file}
44
45 http_redirect $thing '302 Found'
46}
47
48fn webring_random {
49 list_file = $sitedir/list.txt
50
51 lines=`{wc -l < $list_file}
52
53 thing=`{awk -v total=$lines '
54 BEGIN {
55 srand()
56 target = int(rand() * total) + 1
57 }
58 NR == target {
59 print
60 exit
61 }' $list_file}
62
63 if (~ $#thing 0) {
64 echo 'Failed to pick a random site.'
65 exit
66 }
67
68 http_redirect $thing '302 Found'
69}