-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·76 lines (65 loc) · 1.91 KB
/
sync.sh
File metadata and controls
executable file
·76 lines (65 loc) · 1.91 KB
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
#!/bin/bash
#### we have required params...
if (( $# != 3 )) && (( $# != 4 )); then
echo "Simple Gemini Webring Sync"
echo "=========================="
echo ""
echo "Parameters:"
echo "1.) URL or path / filename to the source list"
echo "2.) Target path / filename on your capsule"
echo "3.) Line of your own capsule URL"
echo "4.) Alternate headline (replaces the first line of the gmi / optional)"
echo ""
echo "Example:"
echo "sync.sh https://raw.githubusercontent.com/someuser/somerepo/main/somefile.gmi /var/gemini/default/linklist.gmi 5 \"some links for you\""
exit
fi
#### some variable names for better reading
sourceRepo=$1
targetFileName=$2
ownCapsuleLine=$3
linkListTitle=$4
#### configuration
tempDir=/tmp
#### temporary directory:
if ! [[ -d "$tempDir/sgw-sync" ]]; then
mkdir $tempDir/sgw-sync
fi
# double check... the temp directory must exists!
if ! [[ -d "$tempDir/sgw-sync" ]]; then
echo "ERROR: can't create temporary directory $tempDir/sgw-sync."
exit
fi
cd $tempDir/sgw-sync
#### download / copy source gmi
# if we found a https url, we download this file via curl.
# if it is a local file, we just copy it.
httpsCheck=${sourceRepo/https\:\/\/*/}
if [ "${#httpsCheck}" -eq "${#sourceRepo}" ]; then
if [[ -f "$sourceRepo" ]]; then
cat $sourceRepo >> tmp.gmi
else
echo "ERROR: Source file $sourceRepo not found."
exit
fi
else
curl -f -s $sourceRepo -o tmp.gmi
if ! [[ -f "tmp.gmi" ]]; then
echo "ERROR: Could not download $sourceRepo"
exit
fi
fi
#### cut out line $ownCapsuleLine from tmp.gmi and replace the headline if set
if (( $# == 4 )); then
echo "# $linkListTitle" >> tmp_cutted.gmi
sed "1d" tmp.gmi >> tmp_cutted.gmi
sed -i "${ownCapsuleLine}d" tmp_cutted.gmi
else
sed "${ownCapsuleLine}d" tmp.gmi >> tmp_cutted.gmi
fi
#### move new link list to $targetFileName
mv tmp_cutted.gmi $targetFileName 2> /dev/null
#### cleanup
rm tmp.gmi 2> /dev/null
cd ..
rm -R sgw-sync 2> /dev/null