aboutsummaryrefslogtreecommitdiff
path: root/bin/ak-sm-merkle-tree-to-file
blob: 60e7f71f578d2bceb6a10bd0904914e12dd0859b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# set -xe
if [ -z $1 ]
then
    echo "Please provide a SHA512 hash"
    exit 1
fi
if [ -z $2 ]
then
    echo "Please an output filename"
    exit 2
fi
echo $1 | grep ".\{128\}"
if [ $? -ne 0 ]
then
    echo "Look, I asked for a SHA512 hash, please try again"
    exit 1
fi
treeRootHash="$1"
outputFilename="$2"

fmrk="$AK_WORKDIR/fmrk"
ftr="$AK_WORKDIR/ftr"

pwd>.pwd

dirWeWere=$(cat .pwd)

# Enter temp folder
TEMPASSIN="$(ak-tempassin)"
cd $TEMPASSIN

findDepth(){
    currentNode="$1"
    if [ -f $fmrk/$currentNode ] && [ "$(du -b $fmrk/$currentNode | awk '{print $1}')" == "258" ]
    then
        fileHead="$(head -n 1 $fmrk/$currentNode)"
        counter="$(expr $counter + 1)"
        findDepth "$fileHead"
    elif [ ! -f $fmrk/$currentNode ]
    then
        printf "%s" "$counter" > depth
    else
        exit 111
    fi
}

currentNode="$treeRootHash"
counter=0

printf "%s" "$currentNode" > workspace.0
findDepth "$currentNode"
depth="$(expr `cat depth` + 1)"
counter="0"
printf "%s" "$depth"
if [ -f output ]
then
    rm output
fi
touch output

while [ "$counter" != "$depth" ]
do
    printf "Entering loop... %s %s\n" "$counter" "$depth"
    while IFS="" read -r p || [ -n "$p" ]
    do
        nextLevel="$(expr $counter + 1)"
        if [ "$p" == "" ]
        then
            echo hi
        else
            if [ -f $fmrk/$p ]
            then
                if [ "$(head -n 1 $fmrk/$p)" == "$(tail -n 1 $fmrk/$p)" ]
                then
                    head -n 1 $fmrk/$p >> workspace.$nextLevel
                else
                    cat $fmrk/$p >> workspace.$nextLevel
                fi
            elif [ -f $ftr/$p ]
            then
                cat $ftr/$p >> output
            fi
        fi
    done < workspace.$counter
    counter="$(expr $counter + 1)"
done

base64 -d output > $dirWeWere/$outputFilename
rm $dirWeWere/.pwd
printf "Recreation of $treeRootHash as $outputFilename succeeded!"