#!/bin/bash

# version lun. avril  4 08:29:42 CEST 2016
# "softer" version
# - wget option centralised, adding delays everywhere
# - parallel work disabled, serialised download is slow
#    but time is not an issue after all.

MYHACKERNUMBER=4012

WGET="wget -w 01 --random-wait"

fetchproject() {
  mkdir $1
  pushd $1

    # Get the main page:
    $WGET -O main.html "https://hackaday.io/project/$1"

    grep '<div class="section section-instructions">' main.html &&
      $WGET -O instructions.html "https://hackaday.io/project/$1/instructions/" #&

    grep '">See all components</a>' main.html &&
      $WGET -O components.html "https://hackaday.io/project/$1/components" #&

    # Get the images from the gallery
    $WGET -O gallery.html "https://hackaday.io/project/$PRJNR/gallery"
    grep 'very-small-button">View Full Size</a>' gallery.html |\
    sed -e 's/.*href="//' \
        -e 's/".*//' |\
    tee images.url
    [[ "$( <  images.url )" ]] && ( \
      mkdir images
      pushd images
        $WGET -i ../images.url
      popd
    ) #&

    grep '/files" class="'  main.html && ( \
      echo "getting files for $PRJNR"
      $WGET -O files.html "https://hackaday.io/project/$PRJNR/files"

      grep '<h3><a download target="_blank" href="https://cdn.hackaday.io/files' files.html | \
      sed -e 's/.*href="//' \
          -e 's/".*//' | \
      tee files.url

      [[ "$( < files.url )" ]] && ( \
        mkdir files
        pushd files
         $WGET -i ../files.url
         popd
      )

    ) #&

    LOGSRC=main.html
    detail=$(grep 'show">See all details</a' main.html|sed 's/.*href="/https:\/\/hackaday.io/; s/".*//')
    if [[ "$detail" ]]; then
      LOGSRC=detail.html
      # Get the general description of the project
      echo "getting $detail"
      $WGET -O "$LOGSRC" "$detail"
    fi

    # list the logs:
    grep 'https://hackaday.io/project/.*/log/' $LOGSRC|\
    sed -e 's/.*<strong>Logs:<\/strong>//' \
        -e 's/<br>/\n/g' \
        -e 's/<p>/\n/g'|\
    grep '^[0-9]*[.] <a ' > log_index.txt

    sed 's/.*href="//' log_index.txt |\
    sed 's/".*//' > logs.url

    if [[ "$( <  logs.url )" ]]; then
      mkdir logs
      pushd logs
        $WGET -i ../logs.url #&
      popd
      echo end downloads.
    else
      rm logs.url log_index.txt
    fi

  popd
}


######### Start here #########

DATECODE=$(date '+%Y%m%d')
mkdir $DATECODE
pushd $DATECODE

  $WGET -O profile.html https://hackaday.io/hacker/$MYHACKERNUMBER

  BASE_PROJECT_PAGE="https://hackaday.io/projects/hacker/$MYHACKERNUMBER"
  PROJECT_PAGE="/page/1"
  rm -f projects.names backup.log

  while  [[ "$PROJECT_PAGE" ]] ; do
    # List all the projects:
    $WGET -O projects.html $BASE_PROJECT_PAGE$PROJECT_PAGE
    # stop before the contributions:
    sed '/ contributes to<\/h1>/ q' projects.html |\
    grep 'class="item-link">' |\
    sed -e 's/.*href="\/project\///' -e 's/".*//' |\
    tee -a projects.names

    PROJECT_PAGE=$(grep "<a href=\"/projects/hacker/$MYHACKERNUMBER/page/.*>Next</a>" projects.html |\
    sed -e "s/.*href=\"\/projects\/hacker\/$MYHACKERNUMBER//" |\
    sed -e "s/\".*//")
  done;

  ProjectList=$( sort projects.names )
  if [[ "$ProjectList" ]]; then (
    for PRJNR in $ProjectList
    do
      fetchproject $PRJNR
#      ( fetchproject $PRJNR ) &
#     sleep 20
    done
  ) 2>&1 |tee backup.log
  else
    echo "No project found."
  fi
popd
