#!/bin/zsh

# No copyright is claimed.  This code is in the public domain; do with it
# what you wish.  Written by Joost van Baal-Ilić in 2015, 2016.

# download publicfile documentation

# FIXME: get this script to work with dash / /bin/sh; do not rely upon zsh

set -e
root=false
test $(id -u) -eq 0 && root=true

test -d /var/lib/publicfile/doc/publicfile || \
  mkdir -p /var/lib/publicfile/doc/publicfile || \
  $root || \
  { cat <<EOT ; exit 1
please run:
 sudo mkdir -p /var/lib/publicfile/doc/publicfile
 sudo chown $USER /var/lib/publicfile/doc/publicfile
and rerun this script
EOT
  }

test -d /usr/share/doc/publicfile || \
  mkdir -p /usr/share/doc/publicfile

test -L /usr/share/doc/publicfile/html || \
  ln -s /var/lib/publicfile/doc /usr/share/doc/publicfile/html || \
  $root || \
  { cat <<EOT ; exit 1
please run:
 sudo ln -s /var/lib/publicfile/doc /usr/share/doc/publicfile/html
and rerun this script
EOT
  }

$root && echo >2 "please run this script as a non-root user" && exit 1

cd /var/lib/publicfile/doc

# fetch publicfile.html and files with some license information
for f in publicfile.html mirrors.html distributors.html compatibility.html \
 softwarelaw.html rights.html
do
  test -f $f || wcurl https://cr.yp.to/$f
done

# FIXME support this:
# please either run this script as a non-root user or call it as
# GPD_FORCE_ROOT=true get-publicfile-docs

cd publicfile
for f in configure.html filetypes.html ftpd.html httpd.html install.html \
 log.html performance.html userguide.html
do
    test -f $f || wcurl https://cr.yp.to/publicfile/$f
done
