#! /bin/bash
# mirror.sh -d depth -i index.html -k USK@... -m <url> <short>
function help {
  printf "%s" "
Usage: mirror.sh [-d depth] [-i index.html] [-k USK@...] [-m] <url> <shortname>
Retrieves the website from <url> and inserts it into freenet.

The website contents are downloaded to a directory given by <shortname> and it
is uploaded to freenet with a key like USK@.../shortname/0.

With no options it retrieves the given page and the resources that page needs only.
It does not recurse into pages linked from that page. A random USK key is used.

A file 'reinsert_<shortname>.sh' is created in the current directory with the
commands to reinsert the site contents without downloading the original URL.

The options available are:

  -c cutdir
    Ignore 'cutdir' directory components when storing the download site.

  -d depth
    Specifiy maximum link recursion depth when downloading URL.

  -i index.html
    The first HTML file that is opened with the freenet key is requested.

  -g
    Generate a PNG image of the webpage at the given URL instead of retreving
    the HTML. This requires 'phantomjs' to be installed.

  -h
    This help.

  -k USK@...
    The private key used for inserting the freenet data. Reusing an existing
    key can be used to publish new USK editions.

  -m
    Mirror mode for URL retrieval. This recurses into links when retreving
    the original site.

  -r
    Reinsert the site from the directory specified by 'shortname' without
    redownloading it.

  -t
    Use tor when downloading the site before uploading to Freenet.

Examples:

  mirror.sh http://example.com/one.html example
    Downloads the page into a directory 'example', creating it if needed,
    and inserts into Freenet as USK@randomkey/example/0.

  mirror.sh -k USK@previouskey/example/0 http://example.com/one.html example
    Downloads the page into the 'example' directory and inserts it as a
    new edition under the given key.

  mirror.sh -m -i one.html http://example.com/ example
    Downlaods the complete site and inserts into Freenet so that 'one.html'
    loads first when the key is requested.
"
  exit 1
}

depth="-l 1"
index=""
mirror=""
reinsert="false"
cut=""
tor=""
phantomtor=""
graphic=""
res=0

while getopts ":md:k:i:rthc:g" opt; do
  case $opt in
    h)
      help
      ;;
    c)
      cut="--cur-dirs $OPTARG"
      ;;
    d)
      depth="-l $OPTARG"
      ;;
    g)
      graphic="true"
      ;;
    i)
      index="$OPTARG"
      ;;
    k)
      key="$OPTARG"
      ;;
    m)
      mirror="--mirror"
      ;;
    r)
      reinsert="true"
      ;;
    t)
      tor="torify"
      phantomtor="--proxy=127.0.0.1:9050 --proxy-type=socks5"
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument" >&2
      exit 1
      ;;
  esac
done
shift $((OPTIND-1))

if [ "$#" -ne 2 ]; then
  echo "Incorrect number of arguments"
  help
fi

domain=$(echo $1 | awk -F/ '{print $3}')
url=$1
short=$2
dir=($realpath "$short")

if [ -z "$index" ]; then
  if [ -z "$graphic" ]; then
    index="index.html"
  else
    index="$short.png"
  fi
fi

if [ "$reinsert" == "false" ]; then
  old=$(pwd)
  mkdir -p "$dir"
  cd "$dir"
#  $tor wget -w 1 $cut $depth $mirror --no-parent --timestamping --convert-links --page-requisites --no-host-directories --span-hosts --adjust-extension --no-check-certificate -e robots=off -U 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4' "$url"
#  $tor wget -w 1 $cut $depth $mirror --no-parent --timestamping --convert-links --page-requisites --no-directories --no-host-directories --adjust-extension --no-check-certificate -e robots=off -U 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4' "$url"
  if [ -z "$graphic" ]; then
    if [ -z "$mirror" ]; then
     $tor wget -w 1 $cut $depth $mirror --no-parent --timestamping --convert-links --page-requisites --no-directories --no-host-directories --adjust-extension --no-check-certificate -e robots=off -U 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4' "$url"
    else
      $tor wget -w 1 $cut $depth $mirror --no-parent --timestamping --convert-links --page-requisites --no-host-directories --adjust-extension --no-check-certificate -e robots=off -U 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4' "$url"
    fi
  else
    fn=$(mktemp)
    cat >"$fn" <<EOF
var page = require('webpage').create();
page.open("$url", function() {
  page.viewportSize = {width:1280, height: 1024};
  page.render("$short.png");
  phantom.exit();
});
EOF
    phantomjs $phantomtor "$fn"
    rm "$fn"
  fi
  cd "$old"
fi

put=$(./putdir.sh "$dir" "$short" "$index" "$key")
public=$(echo "$put" | awk '{print $1}')
private=$(echo "$put" | awk '{print $2}')

echo private: $private
echo public: $public
echo "# $url" >reinsert_$short.sh
echo "# $public" >>reinsert_$short.sh
echo ./putdir.sh "$dir" "$short" "$index" "$private" >>reinsert_$short.sh
