#!/bin/bash

# version mer. mai 18 08:56:27 CEST 2016 : trying to save the "pages"
# version dim. sept. 25 16:56:41 CEST 2016 : new layout, fixing broken pattern matching, cleaning up the HTML
# version dim. janv. 22 05:37:55 CET 2017 : saving the "contributed to" projects
#    Note: only the first 18 (?) contributions are saved (yet).
#    Note: "grep -A 1000000 " is a quick and lousy hack, please suggest a better method.

MYHACKERNUMBER=82584 # Yann Guidon Put your own number there !

SANDBOX=$PWD/sandbox
DESTNAME=
GOTNAME=

#WGET="wget " # the "fast" version
WGET="wget -w 1 --random-wait"

WGET_HTML() {
  # a lot of hacks just to get the right file name !
  rm -rf $SANDBOX
  mkdir $SANDBOX
  pushd $SANDBOX
    $WGET $1
    GOTNAME=$(ls .)
  popd
  if [[ "$2" ]]; then
    DESTNAME=$2
  else
    DESTNAME=$GOTNAME
  fi
# remove the wrench
# remove signup
# remove some header
# remove <span class="delete-confirm hide"><span class="delete-q">Are you sure?</span> <a href="#" class="delete-yes">yes</a> | <a href="#" class="delete-no">no</a></span>
# remove everything after '<div id="footer" class="footer">' until '</body>'
    sed -e '/^################............................................................################$/d' $SANDBOX/$GOTNAME \
        -e '/   <div class="page-basic-info-static guest-message-signup">/,/<div class="header-photo" >/d'                       \
        -e '/<section class="section-recommendations">$/q'                                                                      \
        -e '/<span class="delete-confirm hide"><span class="delete-q">Are you sure/d'                                            \
        -e '/^<div class="header" id="header">$/,/class="alert-close j-top-message-close"><i class="icon-close">/d'              \
        -e '/^<div id="footer" class="footer">$/,/^<\/body>$/d'  > $DESTNAME &&
  echo "written to $PWD/$DESTNAME"
  echo
}

fetchproject() {
  mkdir $1
  pushd $1

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

  # Instructions (OK)
  grep '/instructions" class="' main.html|grep '">View all</a>' &&
    WGET_HTML "https://hackaday.io/project/$1/instructions/" instructions.html

  #  Components (OK)
  grep '/components" class="' main.html|grep '">View all</a>' &&
    WGET_HTML "https://hackaday.io/project/$1/components" components.html

  # Get the images from the gallery (OK)
  $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
  )

  # Files (OK)
  grep '/files" class="'  main.html && ( \
    echo "getting files for $PRJNR"
    WGET_HTML "https://hackaday.io/project/$PRJNR/files" files.html

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

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

  #  Project details (OK)
  LOGSRC=main.html
  detail=$(grep ' show">View all details</a></span>' 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_HTML "$detail" "$LOGSRC"
  fi

  # list the logs (OK)
  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 -e 's/.*href="//' \
      -e 's/".*//' log_index.txt |tee logs.url

  if [[ "$( <  logs.url )" ]]; then
    mkdir logs
    pushd logs
      for LGNR in $( sort ../logs.url )
      do
        WGET_HTML $LGNR
      done
    popd
    echo end downloads.
  else
    rm logs.url log_index.txt
  fi

  popd
  echo
  echo end of $1
  echo
}

SaveContributions() {
  mkdir contributions
  pushd contributions

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

####### Note: without any available test case, I de-activated the loop.
###    while  [[ "$CONTRIB_PAGE" ]] ; do
      # List all the projects:
      WGET_HTML $BASE_CONTRIB_PAGE$CONTRIB_PAGE contributions.html

      # start at the contributions:
            ########!!!!!! (must find a better command)
      grep -A 100000000 '<div class="headline" id="projectsIContributeTo">' contributions.html |\
      grep 'class="item-link">' |\
      sed -e 's/.*href="\/project\///' -e 's/".*//' |\
      tee -a contributions.names

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

    ContributionList=$( sort contributions.names )
    if [[ "$ContributionList" ]]; then
      (
      for PRJNR in $ContributionList
        do
          fetchproject $PRJNR
        done
      ) 2>&1 |tee backup.log
      grep -C 3 'Not Found' backup.log |  echo 'Contributions: ok !'
    else
      echo "No contribution found."
    fi
    rm -f contributions.html
  popd
  echo
}


SaveProjects() {
  mkdir projects
  pushd projects

    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_HTML $BASE_PROJECT_PAGE$PROJECT_PAGE projects.html
      # 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
        done
      ) 2>&1 |tee backup.log
      grep -C 3 'Not Found' backup.log  | echo 'ok !'
    else
      echo "No project found."
    fi
    rm -f projects.html
  popd
  echo
}


SavePages() {
  PAGE_BASE="https://hackaday.io/pages/$MYHACKERNUMBER"
  # List all the pages:
  WGET_HTML $PAGE_BASE pages.html
  # remove everything before '                <ul class="buildlogs-list">'
  sed -e '/^<div class="header" id="header">$/,/<div class="section section-buildlogs last">/d' pages.html > pages.html.tmp
  mv pages.html.tmp pages.html

  # no cap at 10 pages ?...

  grep '<a class="gray-link" href="/page/' pages.html > pages_list
  if [[ -s pages_list ]]; then
    mkdir pages
    pushd pages
      echo
      echo "I found these pages:"
      sed -e 's/.*href=\"/https:\/\/hackaday.io/' ../pages_list \
          -e 's/[#].*//' |\
      tee pages.url
      echo

      for PGNR in $( sort pages.url )
      do
        WGET_HTML $PGNR
      done
    popd
  else
    echo "No page found."
  fi
}

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

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

  WGET_HTML https://hackaday.io/hacker/$MYHACKERNUMBER
  mv $DESTNAME profile-$DESTNAME.html

  SavePages
  SaveProjects
  SaveContributions

popd
}|tee  log 2>&1
rm -rf $SANDBOX
