-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcreator.sh
113 lines (96 loc) · 2.74 KB
/
webcreator.sh
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
#!/bin/bash
echo Welcome to Web Creator
set -o nounset
#set -o errexit
#echo arg1: $1 arg2: $2 arg3: $3 arg4: $4
#echo $#
if [[ $# -ne 4 ]]; then
echo "error: Usage ./webcreator.sh root_directory text_file w p" >&2; exit 1;
fi
root_directory="$1"
text_file="$2"
w="$3"
p="$4"
#check if directory and file exist
if [ ! -d "$root_directory" ]; then echo "error: Directory $root_directory not found." >&2; exit 1; fi
if [ ! -r "$text_file" ]; then echo "error: File $text_file not found." >&2; exit 1; fi
#check if arguements w, p are numbers
re='^[0-9]+$'
if ! [[ $w =~ $re ]] ; then echo "error: Value $w not a number." >&2; exit 1; fi
if ! [[ $p =~ $re ]] ; then echo "error: Value $p not a number." >&2; exit 1; fi
#check number of lines of text_file
numOfLines=$(wc -l < "$text_file")
limit=10000
#echo numOfLines: $numOfLines
if [ "$numOfLines" -lt "$limit" ]; then echo "error: Not enough lines." >&2; exit 1; fi
#check if there are files in root directory
if [ "$(ls -A $root_directory)" ]; then
echo "# Warning: directory is full, purging ..."
rm -rf $root_directory/*
fi
declare -A sites
declare -A haslink
RANDOM=1
for (( i = 0; i < $w; i++ )); do
mkdir $root_directory/site$i
echo "# Creating web site $i ..."
for (( j = 0; j < $p; j++ )); do
while : ; do
sites[$i,$j]=/site$i/page${i}_$RANDOM.html
[[ -r $root_directory${sites[$i,$j]} ]] || break
done
touch $root_directory${sites[$i,$j]}
done
done
for key in "${!sites[@]}"
do
k=$(($RANDOM % ($numOfLines - 2000-2)+2))
m=$(($RANDOM % (1000-1)+1001))
f=$((($p / 2) + 1))
q=$((($w / 2) + 1))
linkAfterLines=$(($m/($f+$q)))
echo "# Creating page ${sites[$key]} with $m lines starting at line $k ..."
cat > $root_directory${sites[$key]} << ASDF
<!DOCTYPE html>
<html>
<body>
ASDF
i=$(echo $key | cut -d ',' -f 1)
lines=$(head -$((k+m-1)) $text_file | tail -$m)
count=1
IFS=$'\n' # make newlines the only separator
for line in $lines
do
echo $line >> $root_directory${sites[$key]}
if [[ $count -eq $linkAfterLines ]]; then
if [[ $f -ne 0 ]] ; then
site=${sites[$i,$(($RANDOM%$p))]}
haslink[$site]=1
echo "<a href=\"$site\">Internal link</a>" >> $root_directory${sites[$key]}
echo "# Adding link to $site"
((f--))
else
while : ; do
j=$(($RANDOM%$w))
[[ $i -eq $j ]] || break
done
site=${sites[$j,$(($RANDOM%$p))]}
haslink[$site]=1
echo "<a href=\"$site\">External link</a>" >> $root_directory${sites[$key]}
echo "# Adding link to $site"
fi
count=0
fi
((count++))
done
cat >> $root_directory${sites[$key]} << ASDF
</body>
</html>
ASDF
done
if [[ ${#haslink[@]} -eq $(($w*$p)) ]]; then
echo "# All pages have at least one incoming link"
else
echo "# Not all pages have at least one incoming link"
fi
echo "# Done."