#!/bin/bash

function checktools() {
    if [ -x "descramble" ]; then
        descramble="./descramble"
        scramble="./scramble"
        mkboot="./mkboot"
    elif [ -x "tools/descramble" ]; then
        descramble="tools/descramble"
        scramble="tools/scramble"
        mkboot="tools/mkboot"
    else
        echo "Couldn't find the needed tools. Exiting"
        exit
    fi
}

function getfilename() {
    if [ ! -z $1 ]; then
        orig=$1
    else
        echo -n "Enter filename: "
        read orig;
    fi
    if [ ! -w $orig ]; then
        echo "File $orig doesn't exist or is not writeable"
        exit
    fi
}

function checkorigmd5() {
    # MD5 sums for unpatched firmwares
    # ihp_100 sums
    unpatched[0]="86103cb05658970b43ce1f40f93d53a8" # 1.63eu
    unpatched[1]="70127fa9ee69afab7645297fbb61636d" # 1.63k
    unpatched[2]="7a504a450c76a0dda757fdb6b5531b22" # 1.63us
    unpatched[3]="478dc657b97e77d1b4944ef26c3dcb8e" # 1.65eu
    unpatched[4]="97ba82fb8099bb23ca0c78fc119f8cce" # 1.65k
    unpatched[5]="d3725865e0948cd5f604b00db2ec89aa" # 1.65us
    unpatched[6]="fc59f742fe383da3e4d17a660c46afb9" # 1.66eu
    unpatched[7]="22d214401994c276ec6878288caa0dba" # 1.66k
    unpatched[8]="0ae247f567aeafcafecf823a50cbf33e" # 1.66us
    unpatched[9]="8bfc8ae5805c270597232efeafade5fd" # 1.66jp
    # ihp_120 sums
    unpatched[10]="14488347a171480c63c94bc7b885225d" # 1.63eu
    unpatched[11]="3401fe8845e569156abfaddf05ca7771" # 1.63k
    unpatched[12]="d9078209105c186cee5246055fdb99c9" # 1.63us
    unpatched[13]="c9e71aac4a498f1e2f0e684c2d554ea1" # 1.65eu
    unpatched[14]="360c0c565266f84e9bca610c596f3207" # 1.65k
    unpatched[15]="b9e516d4b8a0265605f46f254897bfb0" # 1.65us
    unpatched[16]="a094999b41781f4322a57b9be7fa4534" # 1.66eu
    unpatched[17]="79daba973bb31b60b4b87c2ef497587d" # 1.66k
    unpatched[18]="7fa020a3104c76fbbfcb8313e287dbe2" # 1.66us
    unpatched[19]="271d151eb6586929da24b3458ea965fe" # 1.66jp
    # ihp_300 sums
    unpatched[20]="0b2083d37f24899b82d21a14d2b38060" # 1.28eu
    unpatched[21]="c6f854ae3d8f48e8982819a4b5302fb0" # 1.28k
    unpatched[22]="7fcd7bca6b98a34134e2518a616f4e85" # 1.28jp
    unpatched[23]="27d90fc316709c096979ab24c914ee31" # 1.29eu
    unpatched[24]="44416d97737fc47cd417a64d44064768" # 1.29k
    unpatched[25]="1ac242c645572a0a5de99ae2b23453b8" # 1.29jp
    unpatched[26]="8d2d775b018f0532235d38f5317ae10c" # 1.30eu

    # Patched sums
    # ihp_100 sums
    patched[0]="4938420d83aa6da3764d33e5a008c8d4" # 1.63eu
    patched[1]="044b737ddb74436353e290d3fbcc3333" # 1.63k
    patched[2]="e51883ed89d49f5677b0a976aef7b154" # 1.63us
    patched[3]="528917d9e5b34a3e5bcac8fe1f4bd7a9" # 1.65eu
    patched[4]="ef24f69a679e5eba2216045cb24d8b15" # 1.65k
    patched[5]="76d83812b9e1856f768ba913eeba44e1" # 1.65us
    patched[6]="2aa9d16d3e166a7575fc652db8588daa" # 1.66eu
    patched[7]="a87dbe604ea98d035450a34b6e184a08" # 1.66k
    patched[8]="f7668e8949f1a78e8ecd685db78b6824" # 1.66us
    patched[9]="72613b75c5196f63e99e41b7a88d7241" # 1.66jp
    # ihp_120 sums
    patched[10]="15f09130a1aa02c25f820e8cc68259a4" # 1.63eu
    patched[11]="55e4cb20e36f4da06ad80b31aaaa3054" # 1.63k
    patched[12]="dbdd012a3d821d26f907879cca71e9f4" # 1.63us
    patched[13]="43454df30c176e55d0df7e3c48e67785" # 1.65eu
    patched[14]="d8cb7d9f586186bf9780ee761fc8a677" # 1.65k
    patched[15]="99c5666e990dc782b9daefcb1a087ec0" # 1.65us
    patched[16]="e26d2574f39cee0adcdd54fb9f316293" # 1.66eu
    patched[17]="e76121ba9efe72ecfbb6392eaefe6d96" # 1.66k
    patched[18]="12194678a2fdd0814d2e0bb57c8e8e6e" # 1.66us
    patched[19]="51716393ea0605d225e70a6be29dfdbd" # 1.66jp
    # ihp_300 sums
    patched[20]="9cd7e291a66f55335c619d63f3a7634b" # 1.28eu
    patched[21]="90967247ba3f1bcb257432a4d78553bf" # 1.28k
    patched[22]="cfd22c20e473727148f11f83d0028fb3" # 1.28jp
    patched[23]="5982302507d57c2a96e9480c242b5de0" # 1.29eu
    patched[24]="1303e22ef5b1af866aa4def03d6c5f5a" # 1.29k
    patched[25]="ce433c404ff7531f5852ce3cb61143ee" # 1.29jp
    patched[26]="7fb49041294b3e6aea3894218d6d64cf" # 1.30eu

    md5=$(md5sum $orig |cut -f1 -d\ )
    targetmd5=""
    i=0
    model=-1
    while [ $i -lt ${#unpatched[@]} ]; do
        if [ ${unpatched[$i]} == $md5 ]; then
#            echo "Found a match: $md5  = ${unpatched[$i]}";
            targetmd5=${patched[$i]}
            model=$i
            i=${#unpatched[@]}
#        else echo "No match:      $md5 != ${unpatched[$i]}"
        fi
        let "i = $i + 1"
    done
    if [ $model -gt -1 ] && [ $model -lt 10 ]; then
        bootloader="bootloader-h100.bin"
        mkbootopts=""
    elif [ $model -gt 9 ] && [ $model -lt 20 ]; then
        bootloader="bootloader-h120.bin"
        mkbootopts=""
    elif [ $model -gt 19 ] && [ $model -lt 27 ]; then
        bootloader="bootloader-h300.bin"
        mkbootopts="-h300"
    else
        i=0
        while [ $i -lt ${#patched[@]} ]; do
            if [ ${patched[$i]} == $md5 ]; then
                echo "File $orig is already patched"
                exit
            fi
            let "i = $i + 1"
        done
        echo "File $orig matches no known original firmware"
        exit
    fi
}

function run_descramble() {
    echo "*** Running descramble ***"
    $($descramble -iriver "$orig" "${orig}.descramble")
    echo "-----------------------------------------"
}

function run_mkboot() {
    if [ ! -r $bootloader ]; then
        echo "Please place the file $bootloader in the current directory"
        cleanup
        exit
    else
        echo "*** Running mkboot ***"
        $mkboot $mkbootopts "${orig}.descramble" "$bootloader" "${orig}.mkboot"
        echo "-----------------------------------------"
    fi
}

function run_scramble() {
    echo "*** Running scramble ***"
    $($scramble -iriver "${orig}.mkboot" "${orig}.final")
    echo "-----------------------------------------"
}

function checkresultmd5() {
    md5=$(md5sum "${orig}.final" | cut -f1 -d\ )
    if [ $md5 == $targetmd5 ]; then
        echo "Resulting MD5 is correct ($targetmd5)";
        mv -f "${orig}.final" "$orig"
    else
        echo "Resulting MD5 is wrong, leaving firmware untouched";
    fi
}

function cleanup() {
    rm -f "${orig}.mkboot" "${orig}.final" "${orig}.descramble"
}

checktools
getfilename $1
checkorigmd5
run_descramble
run_mkboot
run_scramble
checkresultmd5
cleanup
