aboutsummaryrefslogtreecommitdiff
path: root/bin/ak-profile
blob: c7d55c00915bfe062228a7a29c32962d43647751 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env bash
##
## Indexes, shows, imports, adds, sets and gets values from and to the
## zchain and files.
##
## Usage:
##
##     -h, --help                      Prints this help message
##     -l, --local-index               Show current status
##     --show <data IPFS CIDv0>        Show profile entry from specified DATA
##                                     IPFS CIDv0
##     -s, --set <property> <value>    Sets a profile value
##     -g, --get <property>            Gets a profile value from the on-disk
##                                     file.
##
## Advanced (use with caution may produce duplicate entries):
##
##     -a, --add <file>                Creates a data file from the profile file
##                                     you point (file should already be in
##                                     ZPROFILEDIR).
##     -i, --import <folder>           Import a folder to zchain #TODO
##
fullprogrampath="$(realpath $0)"
PROGRAM=$(basename $0)
descriptionString="Profile module"
ZPROFILEDIR="$AK_WORKDIR/profile"

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

# Whatever the command is, we check if $ZPROFILEDIR is there.
# If NOT we create it and we change dir there.
if [ ! -d $ZPROFILEDIR ]
then
    mkdir $ZPROFILEDIR
    _ak_log_info "zprofiledir created"
else
    _ak_log_info "zprofiledir found"
fi

cd $ZPROFILEDIR

# This is working with DATA blocks. DATA that matched profile/add ACTION
#
# The profile settings/configuration is part of the blockchain produced.
# Hence, we need a specific DATA block that actually has the announcement of a
# {"key":"value"} pair.
_ak_modules_profile_show(){
    if [ ! -z $1 ]
    then
        _ak_log_info "Working with $1"
        _ak_ipfs_cat $(echo $(_ak_ipfs_cat $1) | jq '.ipfs' -r)
    else
        _ak_log_error "No DATA provided"
        exit 1
    fi
}

# This should retrieve a specific value from our profile otherwise it dumps the
# whole profile values.
_ak_modules_profile_propget(){
    if [ ! -z $1 ]
    then
        if [ ! -f $ZPROFILEDIR/$1 ]
        then
            _ak_log_error "property not found $1"
        else
            cat $ZPROFILEDIR/$1
        fi
    else
        _ak_log_error "No particular property... indexing"
        _ak_modules_profile_index
    fi
}

_ak_modules_profile_propwrite(){
    cat > $ZPROPERTY_FILE << EOF
{
    "$ZPROPERTY_KEY":"${ZPROPERTY_VALUE}"
}
EOF
    if [ $? == 0 ]
    then
        _ak_log_info "Added successfully... proceeding"
        IPFS_FILE=$(_ak_ipfs_add $ZPROPERTY_FILE)
        _ak_log_info "Prop writting,,, $IPFS_FILE"
        _ak_modules_profile_add $ZPROPERTY_KEY
        cd $ZPROFILEDIR
    else
        _ak_log_error "Couldn't write to file $ZPROFILEDIR/$TO_FILE"
        exit 1
    fi
}

_ak_modules_profile_propset(){
    if [ ! -z $1 ]
    then
        ZPROPERTY_FILE="$ZPROFILEDIR/$1"
        ZPROPERTY_KEY="$1"
        if [ ! -f $ZPROPERTY_FILE ]
        then
            _ak_log_warning "No such property: $ZPROPERTY_KEY ... creating"
            ZPROPERTY_VALUE="$2"
            if [ ! -z "$ZPROPERTY_VALUE" ]
            then
                touch $ZPROPERTY_FILE
                _ak_log_debug "$ZPROPERTY_KEY = $ZPROPERTY_VALUE in file $ZPROPERTY_FILE"
                _ak_modules_profile_propwrite #"$ZPROPERTY_FILE" "${ZPROPERTY_VALUE}"
            else
                _ak_log_error "No value for $1"
            fi
        else
            _ak_log_info "found $ZPROPERTY_FILE"
            _ak_log_debug "$ZPROPERTY_KEY = $ZPROPERTY_VALUE in file $ZPROPERTY_FILE"
            ZPROPERTY_VALUE="$2"
            read -p "Overwrite $1 with ${ZPROPERTY_VALUE} ? " yn
            case $yn in
                [Yy]* ) _ak_modules_profile_propwrite;;# "$ZPROPERTY_" "${ZPROPERTY_VALUE}";;
                [Nn]* ) exit 130;;
                * ) echo "Answer please";;
            esac
            _ak_log_debug $IPFS_FILE
            _ak_modules_profile_show $IPFS_FILE
        fi
    else
        _ak_log_error "conditions unmet"
        exit 244
    fi
}
_ak_modules_profile_index(){
    FILES="$(ls -1 $ZPROFILEDIR)"
    i=0
    echo -n "{"
    for FILE in $FILES
    do
        if [ $FILE != "README" ]; then
            if [ $i != "0" ]; then
                echo -n ",";
            fi
            PROP=$(echo $(cat $ZPROFILEDIR/$FILE | ak-json2bash) | cut -d '=' -f 1 | awk '{print $0}')
            VAL=$(echo $(cat $ZPROFILEDIR/$FILE | ak-json2bash) | cut -d '=' -f 2 | awk '{print $1}')
            echo -n '"'$PROP'":"'$VAL'"';
            let i+=1
        fi
    done
    echo "}"
}

_ak_modules_profile_import(){
    if [ ! -z $1 ]
    then
        if [ ! -d $1 ]
        then
            _ak_log_error "Profile folder check: Folder $1 does not exist. Stopping..."
            exit 4
        else
            _ak_log_info "Profile folder check: Folder $1 exists."
            fl="$(ls -1 $1)"
            for f in $fl
            do
                _ak_modules_profile_add $1/$f
            done
        fi
    else
        _ak_log_error "No value"
        exit 6
    fi
    exit 224
}

# Adds a file as a profile/add ACTION on the zchain.
_ak_modules_profile_add(){
    TEMPASSIN="$(_ak_make_temp_directory)"
    cd $TEMPASSIN
    if [ -f $ZPROFILEDIR/$1 ]; then
        FILE="$ZPROFILEDIR/$1"
        _ak_log_info "Adding from " $FILE
        FILE_IPFS_HASH=$(_ak_ipfs_add $FILE)
        FILE_SIGN_FILE=$(pwd)/$1".asc"
        _ak_gpg_sign_detached $FILE_SIGN_FILE $FILE
        FILE_SIGNATURE=$(_ak_ipfs_add $FILE_SIGN_FILE)
        cat > data <<EOF
{
   "datetime":"$(date -u +%s)",
   "ipfs":"$FILE_IPFS_HASH",
   "detach":"$FILE_SIGNATURE"
}
EOF
    else
        _ak_log_error "File $FILE doesn't exist"
        exit 2
    fi

    _ak_zblock_pack "profile/add" $(pwd)/data
    if [ $? -ne 0 ]
    then
        _ak_log_error "Error while packing"
        exit 1
    fi
    _ak_log_info "Profile zblock added successfully"
    cd $ZPROFILEDIR
    rm -rf $TEMPASSIN
}


if [ ! -z $1 ]; then
    case $1 in
        -h | --help) _ak_usage; exit;;
        -l | --local-index) _ak_modules_profile_index; exit;;
        --show) _ak_modules_profile_show $2; exit;;
        -i | --import) _ak_modules_profile_import $2; exit;;
        -a | --add) _ak_modules_profile_add $2; exit;;
        -s | --set) _ak_modules_profile_propset $2 "$3"; exit;;
        -g | --get) _ak_modules_profile_propget $2; exit;;
        * ) _ak_usage;;
    esac
else _ak_usage
fi