summaryrefslogtreecommitdiff
path: root/bin/curl
blob: 7199368fa187c7ffc247d3723b07b163b1a763e0 (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
#!/bin/bash

usage() {
  >&2 echo 'usage: '"$0"' -o file url'
  exit
}

if [ "$#" -ne 3 ] || [ ! "x$1" = 'x-o' ]; then
  usage
fi

file="$2"
url=$(
  printf -- '%s' "${3}" \
  | base64 -w0
)

work_dir=$(readlink -e "$0")
work_dir="${work_dir%/*/*}/work"

if [ ! -d "${work_dir}" ]; then
  mkdir "${work_dir}"
  echo 'files' >"${work_dir}"'/.gitignore'
  git -C "${work_dir}" init
  git -C "${work_dir}" add '.gitignore'
  git -C "${work_dir}" commit -m 'initial commit'
fi

if [ -s "${work_dir}/files/${url}" ]; then
  cp "${work_dir}/files/${url}" "${file}"
  if [ -s "${work_dir}/wishlist" ]; then
    grep -vxF "${url}" "${work_dir}/wishlist" \
    | sponge "${work_dir}/wishlist"
    git -C "${work_dir}" add 'wishlist'
    git -C "${work_dir}" commit 'wishlist' -m 'removed old wish'
  fi
  exit
fi

{
  printf -- '%s\n' "${url}"
  [ -s "${work_dir}/wishlist" ] \
  && cat "${work_dir}/wishlist"
} \
| sort -u \
| sponge "${work_dir}/wishlist"

git -C "${work_dir}" add 'wishlist'
git -C "${work_dir}" commit 'wishlist' -m 'new wishes'