aboutsummaryrefslogtreecommitdiff
path: root/bin/profile
blob: f4d4eb4f764df6881f16e4b4336dba039a4c687f (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
222
223
224
225
226
227
228
#!/bin/bash
ZPROFILEDIR="$WORKDIR/profile"
TEMP="/tmp/aktmp"
if [ ! -d $ZPROFILEDIR ]; then
	mkdir $ZPROFILEDIR
	cd $ZPROFILEDIR
	git init
	echo "Profile repository" > README
	echo "Qmetc" >> README
	git add README
	git commit -m "Initiated profile repository"
	logthis "zprofiledir created along with git repo"
else
	logthis "zprofiledir found"
fi
tempassin(){
	if [ ! -z $1 ]
	then
		TEMPASSIN="$1"
	else
		TIMESTAMP="$(date -u +%s)"
		TEMPASSIN="/tmp/aktmp_$TIMESTAMP"
	fi
	if [ ! -d $TEMPASSIN ]; then
		mkdir $TEMPASSIN
	fi
	cd $TEMPASSIN
	echo "$TEMPASSASIN"
}
# This is working with DATA blocks. DATA that matched profile/add ACTION
#
show(){
	if [ ! -z $1 ]
	then
		logthis "Working with $1"
		echo '"data":"'$1'",'
		echo '"'$1'":'$(ipfs cat $1)','
		#ipfs cat $1
	else
		echo "No DATA provided"
		exit 1
	fi
}
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"
		index
	fi
}
propwrite(){
	cat > $ZPROPERTY_FILE << EOF
{
    "$ZPROPERTY_KEY":"${ZPROPERTY_VALUE}"
}
EOF
	if [ $? == 0 ]
	then
		echo "Added successfully... proceeding"
#		add $ZPROPERTY_FILE
		IPFS_FILE=$(ipfs add -q $ZPROPERTY_FILE)
		echo "Prop writting,,, $IPFS_FILE"
	#	mv $PROFILE_FILE $ZPROFILEDIR/$TO_FILE
	#	sed -e 's,Qm.*,'"$IPFS_FILE"',g' $ZPROFILEDIR/README
		add $ZPROPERTY_KEY
		echo "Adding to git repo..."
		cd $ZPROFILEDIR
		git add $ZPROPERTY_KEY README
		git commit -m "Added ${ZPROPERTY_KEY} with ${ZPROPERTY_VALUE}"
		git clean --force
	else
		echo "error"
		exit 244
	fi
}
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
			show $IPFS_FILE
		fi
	else
		echo "conditions unmet"
		exit 244
#	tempassin $TEMP
#	pwd
#	export PROFILE_FILE="$(date -u +%s)"
#	vi $PROFILE_FILE
#	echo "Renaming..."
#	TITLE="$(head -n 1 $PROFILE_FILE)"
#	TO_FILE=$PROFILE_FILE-$(echo $TITLE | tr '[:upper:]' '[:lower:]' | sed -e 's/ /\_/g' )
#	add $ZPROFILEDIR/$TO_FILE
#	# rm -rf $TEMP 
	fi
}
index(){
	FILES="$(ls -1 $ZPROFILEDIR)"
	i=0
	echo "{"
	for FILE in $FILES
	do
		if [ $FILE != "README" ]; then
			if [ $i != "0" ]; then
				echo ",";
			fi
			PROP=$(echo $(cat $ZPROFILEDIR/$FILE | json2bash) | cut -d '=' -f 1 | awk '{print $0}')
			VAL=$(echo $(cat $ZPROFILEDIR/$FILE | json2bash) | cut -d '=' -f 2 | awk '{print $1}')
			echo '"'$PROP'":"'$VAL'"';
			let i+=1
		fi
	done
	echo "}"
}
title(){
	echo "ak-profile-cli"
	echo "--------------"
}
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
				add $1/$f
			done
		fi
	else
		echo "No value"
		exit 6
	fi
	exit 224
}
add(){
	tempassin
	if [ -f $ZPROFILEDIR/$1 ]; then
		FILE="$ZPROFILEDIR/$1"
		echo "Adding from " $FILE
		FILE_IPFS_HASH=$(ipfs add -q $FILE)
		FILE_SIGN_FILE=$(pwd)/$1".asc"
		gpg --detach-sign --sign-with $FINGERPRINT --armor --output $FILE_SIGN_FILE $FILE
		FILE_SIGNATURE=$(ipfs add -q $FILE_SIGN_FILE)
		cat > data <<EOF
{
   "datetime":"$(date -u +%s)",
   "ipfs":"$FILE_IPFS_HASH",
   "detach":"$FILE_SIGNATURE"
}
EOF
	else
		echo "File $FILE doesn't exist";
		exit 2
	fi
	pack_z_block "profile/add" data
	if [ $? == 0 ]
	then
		echo "Profile added successfully"
	else
		echo "error??"
		exit 1
	fi
}
usage(){
	title
	echo ""
	echo "Usage:"
	echo "		$0 command [options]"
	echo ""
	echo "All you need to know is that there are two options available:"
	echo "help			Prints this help message"
	echo "index			Show current status"
	echo "show <zblock>		Show profile"
	echo "import <folder>		Import a folder to zchain #TODO"
	echo "add <file>		Creates a data file from the profile file you point to"
	echo "set <property> <value>	Sets a profile value"
	echo "get <property>		Gets a profile"
	echo ""
	exit 0
}
if [ ! -z $1 ]; then
	case $1 in
		help) usage; exit;;
		index) index; exit;;
		show) show $2; exit;;
		import) import $2; exit;;
		add) add $2; exit;;
		set) propset $2 "$3"; exit;;
		get) propget $2; exit;;
		* ) usage;;
	esac
else usage
fi