#!/bin/bash
# script d'automatisant le dump de gdrom via httpd-ack
# nécessite sous Ubuntu 7.10 le packet libarchive-zip-perl (crc32)
#
# utilisation : ./dumpcast.sh 'Mon Dossier' 'Ring Code'
#

IP="192.168.1.210"
DIR=$1
RINGCODE=$2
SOUND="/usr/share/sounds/Kopete_User_is_Online.ogg"

if [ "$DIR" == "" ]
then
  echo "You have to specify a directory"
  exit 1
fi

echo -n "Téléchargement des fichiers en cours ... "
# dl'em all
wget -r -nH --progress=bar -P "$DIR" http://"$IP"/ #2>&1 | tee "$DIR".log
ERR=$?
ERR=0

# si pas de dl (adresse injoignable) alors $? <> 0
if [ $ERR -eq 0 ]
then
  # replace le log wget dans le dossier de travail
  mv "$DIR".log "$DIR"/"$DIR".log

  echo -e -n "terminé.\nCorrection des noms de fichiers ... "

  cd "$DIR"

  # corrige les noms des pistes (enlève le surplus après l'extension)
  for fic in $(ls track*)
  do mv $fic $(echo $fic | cut -d"?" -f 1)
  done

  echo -e -n "terminé.\nGénération du fichier cue pour l'extraction des données ... "
 
  # generation du .cue pour l'extraction & la verification des donnees
  i=1
  echo -n > extract_high_density.cue
  cat index.html | grep "_session02_" | cut -d"\"" -f 2 | cut -d"\"" -f 1 | while read FIC
  do
    NUM=$(echo $FIC | cut -d"k" -f 2 | cut -d"." -f 1)
    EXT=$(echo $FIC | cut -d"." -f 2 | cut -d"?" -f 1)
    GAP=$(echo $FIC | cut -d"_" -f 7 | cut -d"p" -f 2)
    SES=$(echo $FIC | cut -d"_" -f 2 | cut -d"n" -f 2)
    FIC=$(echo $FIC | cut -d"?" -f 1)
    if [ $SES -eq "02" ] # session 02 ??
    then
      echo "FILE \"$FIC\" BINARY" >> extract_high_density.cue
      if [ $EXT = "bin" ] # BINARY OR AUDIO ??
      then
        echo "TRACK $NUM MODE1/2352" >> extract_high_density.cue
      else
        echo "TRACK $NUM AUDIO" >> extract_high_density.cue
      fi
      if [ $i -eq 1 ] # first track of session 02 ??
      then echo "PREGAP 10:00:00" >> extract_high_density.cue
      fi
      echo "INDEX 01 00:00:00" >> extract_high_density.cue
      if [ $GAP -eq 150 ] # gap 150 ??
      then echo "POSTGAP 00:02:00" >> extract_high_density.cue
      fi
      echo >> extract_high_density.cue
    fi
    i=$(expr $i + 1)
  done

  echo -e -n "terminé.\nGénération du fichier de post (avec hash) pour le forum dumpcast ... "

  # generation du post pour http://dumpcast.thekickback.com/forum/index.php
  echo "[b]Title : [/b]"$(cat index.html | grep "Title" | cut -d\> -f 5 | cut -d\< -f 1) > post.txt
  echo "[b]Media ID : [/b]"$(cat index.html | grep "Media ID" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Media Config : [/b]"$(cat index.html | grep "Media Config" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Regions : [/b]"$(cat index.html | grep "Regions" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Peripheral String : [/b]"$(cat index.html | grep "Peripheral String" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Product Number : [/b]"$(cat index.html | grep "Product Number" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Version : [/b]"$(cat index.html | grep "Version" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Release Date : [/b]"$(cat index.html | grep "Release Date" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Manufacturer ID : [/b]"$(cat index.html | grep "Manufacturer ID" | cut -d\> -f 5 | cut -d\< -f 1) >> post.txt
  echo "[b]Ring Code : [/b]"$RINGCODE >> post.txt
  echo >> post.txt
  echo "[b]Dump Tools : [/b]Dreamcast PAL HKT-3030 (1) with Broadband HIT-0400 | dcload-ip-1.0.3 & httpd-ack-20071123 | Slackware64 13.1 with wget & modified script by executter" >> post.txt
  echo >> post.txt
  echo "[b]Emu compatibility : [/b]*TO EDIT**works with nullDC v1.0.0 beta 1.6*" >> post.txt
  echo >> post.txt
  echo >> post.txt
  echo -n "[quote=\"GDI Structure\"][color=green]" >> post.txt
  cat disc.gdi | tr -d "\r" >> post.txt
  echo "[/color][/quote]" >> post.txt
  echo >> post.txt
  echo -n "[quote=\"Files Infos\"]" >> post.txt
 
  i=1
  cat disc.gdi | while read ligne
  do
    if [ $i -eq 1 ]
    then filename="disc.gdi"
    else filename=$(echo $ligne | cut -d" " -f 5)
    fi
    if [ -f "$filename" ]
    then
      size=$(ls -la $filename | awk '{print $5}')
      crc32=$(crc32 $filename)
      md5=$(md5sum $filename | awk '{print $1}')
      sha1=$(sha1sum $filename | awk '{print $1}')
      echo $filename" [color=red]size "$size"[/color] [color=green]crc "$crc32"[/color] [color=blue]md5 "$md5"[/color] [color=red]sha1 "$sha1"[/color]" >> post.txt
    fi
    i=$(expr $i + 1)
  done
 
  echo "[/quote]" >> post.txt

  # supprime les fichiers inutiles
  # rm dc_bios.bin dc_flash.bin syscalls.bin
 
  # signal de fin !
  echo -e "terminé !\nAu suivant ;)"
  play -q "$SOUND"
else
  echo -e "échoué !\nErreur : La dreamcast est injoignable !"
fi
