#!/bin/bash
# ak-enter
#
# Using this tool, we can seek a whole zchain, if available from
# an IPFS CID or an IPNS link.
#
# Default (no arguments) will retrieve the local ZCHAIN starting
# from the IPFS CID stored in the file that is tracked by the
# $AK_ZLATEST environment variable.
#
# ak-enter [-n IPNS_LINK]
# ak-enter [IPFS CID]
# ak-enter -N
# ak-enter -h
# ak-enter
## ak-enter [-N | --no-verify] [-l | --limit <number>] [zblock]
## ak-enter [-N | --no-verify] [-l | --limit <number>] -n <zchain>
## Usage:
##     --help, -h                             Print this help and exit
##     --chain <ipns-link>, -n <ipns-link>    Crawl specified chain
##     --no-verify, -N                        Don't verify signatures
##     <ipfs-link>                            Specify IPFS CID for entrance
##
## Note that combined flags don't work for now
## Running with no flags crawls your chain based on AK_ZLATEST environment
## variable
#
# Returns a JSON array representing the chain retrieved.
# Logs messages to $LOGSFILE.

fullprogrampath="$(realpath $0)"
PROGRAM="$(basename $0)"
descriptionString="Crawl an arching kaos chain"
source $AK_LIBDIR/_ak_log
source $AK_LIBDIR/_ak_script
source $AK_LIBDIR/_ak_ipfs
source $AK_LIBDIR/_ak_gpg
source $AK_LIBDIR/_ak_zblock

# Start of tests
#entrance="QmW5WVXCJfhb4peHG6cbEdamC24vZzMX2Vz386vpENh38U"
#entrance="QmNjQq7GkuXGF8kFT1z2Mv3i4JhY7sBXVUmHDiR1zkQjoE"
#entrance="QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
# End of tests
entrance="$(cat $AK_ZLATEST)"

verify=1
limit=0
fromIpns=0

while [ "$#" ]; do
    case "$1" in
        -h | --help)
            _ak_usage
            ;;
        -l | --limit)
            limit=$2
            shift 2
            ;;
        -N | --no-verify)
            verify=0
            shift
            ;;
        -n | --chain | --ipns)
            fromIpns=1
            ipns=$1
            shift
            ol=$1
            entrance="$(_ak_ipns_resolve $1)"
            if [ $? -ne 0 ]
            then
                _ak_log_error "Could not resolve IPNS name"
                exit 1
            fi
            shift
            ;;
        *)
            test="$1"
            if [ ! -z "$test" ] && [ $fromIpns -eq 0 ]
            then
                _ak_ipfs_cid_v0_check "$test"
                entrance="$test"
            elif [ -z "$entrance" ] && [ $fromIpns -eq 1 ]
            then
                entrance="$(cat $AK_ZLATEST)"
            fi
            break
    esac
done

# We assign the IPFS CIDv0 of an empty file as this is used
# as our GENESIS block, hence the "seed" that the tree grows
# from.
seed="$(cat $AK_ZGENESIS)"

# We assume that we found the entrance inside a block, hence
# ZBLOCK is labeled as previous
zblock="$entrance"

# Enter temp folder
TEMPASSIN="$(_ak_make_temp_directory)"
cd $TEMPASSIN
counter=0

# The loop
# We break the loop from inside the loop
while true
do
    if [ $counter -eq 0 ]
    then
        echo -n '['
    fi
    counter=$(($counter + 1))
    _ak_zblock_show "$zblock"
    if [ $limit -ne 0 ] && [ $limit -eq $counter ]
    then
        echo -n ']'
        exit 0
    else
        echo -n ','
    fi
done