#!/bin/bash ## ## Indexes, shows, imports, adds, sets and gets values from and to the ## zchain and files. ## ## Usage: ## ## -h, --help Prints this help message ## ## index Show current status ## ## show Show profile entry from specified DATA IPFS CIDv0 ## ## set Sets a profile value ## ## get Gets a profile value from the on-disk file. ## ## Advanced (use with caution may produce duplicate entries): ## ## add Creates a data file from the profile file you point ## to (file should already be in ZPROFILEDIR. ## ## import Import a folder to zchain #TODO ## fullprogrampath="$(realpath $0)" PROGRAM=$(basename $0) descriptionString="Profile module" ZPROFILEDIR="$AK_WORKDIR/profile" TEMP="/tmp/aktmp" # Outputs to log file in the classic format :) 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 cd $ZPROFILEDIR logit "INFO" "zprofiledir created" else logit "INFO" "zprofiledir found" fi # 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 logit "INFO" "Working with $1" _ak_ipfs_cat $(echo $(_ak_ipfs_cat $1) | jq '.ipfs' -r) else echo "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 echo "property not found" else cat $ZPROFILEDIR/$1 fi else echo "No particular property... indexing" _ak_modules_profile_index fi } propwrite(){ cat > $ZPROPERTY_FILE << EOF { "$ZPROPERTY_KEY":"${ZPROPERTY_VALUE}" } EOF if [ $? == 0 ] then echo "Added successfully... proceeding" IPFS_FILE=$(_ak_ipfs_add $ZPROPERTY_FILE) echo "Prop writting,,, $IPFS_FILE" _ak_modules_profile_add $ZPROPERTY_KEY cd $ZPROFILEDIR else echo "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 echo "No such property: $ZPROPERTY_KEY ... creating" ZPROPERTY_VALUE="$2" if [ ! -z "$ZPROPERTY_VALUE" ] then touch $ZPROPERTY_FILE echo "$ZPROPERTY_KEY = $ZPROPERTY_VALUE in file $ZPROPERTY_FILE" propwrite #"$ZPROPERTY_FILE" "${ZPROPERTY_VALUE}" else echo "No value for $1" fi else echo found $ZPROPERTY_FILE echo "$ZPROPERTY_KEY = $ZPROPERTY_VALUE in file $ZPROPERTY_FILE" ZPROPERTY_VALUE="$2" read -p "Overwrite $1 with ${ZPROPERTY_VALUE} ? " yn case $yn in [Yy]* ) propwrite;;# "$ZPROPERTY_" "${ZPROPERTY_VALUE}";; [Nn]* ) exit 130;; * ) echo "Answer please";; esac echo $IPFS_FILE _ak_modules_profile_show $IPFS_FILE fi else echo "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 echo "Profile folder check: Folder $1 does not exist. Stopping..." exit 4 else echo "Profile folder check: Folder $1 exists." fl="$(ls -1 $1)" for f in $fl do _ak_modules_profile_add $1/$f done fi else echo "No value" exit 6 fi exit 224 } # Adds a file as a profile/add ACTION on the zchain. _ak_modules_profile_add(){ TEMP="$(ak-tempassin)" cd $TEMP if [ -f $ZPROFILEDIR/$1 ]; then FILE="$ZPROFILEDIR/$1" echo "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 <