#!/bin/bash

# Download Manga Script
# ( For use with the site OneManga.com )
# By TaTooKa

# Note: This Script requires wget and rar.
# It downloads all the JPG files of a Manga chapter from OneManga.com.
#
# Visit www.onemanga.com to browse through Manga's and then run this
# script with the params described in the Usage.
#
# You can find the ID's that this script requires if you look at the image URL.
# The URL format for each JPG page is:
# http://img[IMAGE_SERVER_ID].onemanga.com/mangas/[MANGA_ID]/[CHAPTER_ID]/[PAGE_NUMBER].jpg
# Note that you don't have to zero-pad these ID's, this script already does that.
# (So MANGA_ID 00000144 would be just 144, and so on)
#
# Use with caution. This will only last till they hash the image URL, so don't abuse.
#  -- TaTooKa

USAGE="Usage: ./download_manga.sh [image_server_id] [manga_id] [chapter_id]"

if [ -z "$1" ]
then
    echo $USAGE;
else
    IMAGE_SERVER_ID=$1
    MANGA_ID=`printf "%08d\n" $2`
    CHAPTER_ID=`printf "%08d\n" $3`

    ##########################################

    # Edit this vars to your own needs

    MANGA_SITE=onemanga.com
    MAX_PAGE_PER_CHAPTER=100
    MAX_WGET_ERRORS=10
    DOWNLOAD_DIR=~/manga/$2/$3
    RAR_FILENAME=$2-$3

    ##########################################

    WGET_ERRORS=0

    echo "";
    echo "Script initializing with the following vars:";
    echo "";
    echo "Image Server ID: $IMAGE_SERVER_ID (img$IMAGE_SERVER_ID)";
    echo "Manga ID: $2 ($MANGA_ID)";
    echo "Chapter ID: $3 ($CHAPTER_ID)";
    echo "";
    echo "Creating Download Directory $DOWNLOAD_DIR ...";
    mkdir -p $DOWNLOAD_DIR;
    echo "";

    # Download Loop
    for ((i=0; i<$MAX_PAGE_PER_CHAPTER; i++))
    do
        echo "Downloading page $i....";
        # The page number must be padded ( NN )
        PAGE=`printf "%02d\n" $i`
        wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/$PAGE.jpg -P $DOWNLOAD_DIR;
        if [ $? != 0 ]
        then
            echo "Error while trying to download $PAGE.jpg. The file might not exist.";
            let j=$i+1;    
            DOUBLE_PAGE=`printf "%02d\n" $i`-`printf "%02d\n" $j`
            echo "";
            echo "Maybe it's a double-page. Trying to download $DOUBLE_PAGE.jpg ...";
            wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/$DOUBLE_PAGE.jpg -P $DOWNLOAD_DIR;
            # If the single page and the double-page don't exist, probably we've reached the end.
            # Once this happens a number of times equal to $MAX_WGET_ERRORS, the loop is broken.
            if [ $? != 0 ]
            then
                let WGET_ERRORS++;
                if  [ $WGET_ERRORS -gt $MAX_WGET_ERRORS ]
                then
                    break;
                fi
            fi
        fi
        echo "";
    done
    # Download Covers, Credits and notes
    wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/cover.jpg -P $DOWNLOAD_DIR;
    wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/cover1.jpg -P $DOWNLOAD_DIR;
    wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/cover2.jpg -P $DOWNLOAD_DIR;
    wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/note.jpg -P $DOWNLOAD_DIR;
    wget http://img$IMAGE_SERVER_ID.$MANGA_SITE/mangas/$MANGA_ID/$CHAPTER_ID/credits.jpg -P $DOWNLOAD_DIR;


    # Create CBR
    echo "Creating RAR file...";
    rar a $DOWNLOAD_DIR/$RAR_FILENAME.rar $DOWNLOAD_DIR/*.jpg
    echo "Renaming RAR to CBR format...";
    mv $DOWNLOAD_DIR/$RAR_FILENAME.rar $DOWNLOAD_DIR/$RAR_FILENAME.cbr
    echo "Deleting the remaining JPGs...";
    rm $DOWNLOAD_DIR/*.jpg
    echo "The Manga was saved as $DOWNLOAD_DIR/$RAR_FILENAME.cbr";
fi
exit