summary refs log tree commit diff
path: root/utils/message-tcoin-converter.awk
blob: 81ea0e427ec7bd2e8569052d383021603d4112bc (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
#!/usr/bin/awk -f

#mst.awk -> message_sum_tcoin.awk

BEGIN {
  tsv=1
  summary=0
  sum=0
  sumplus=0
  summinus=0
  self_user=""
  self_user_found=0
  prev_self_txn=0
  FS=""
}

(NR == 1) {
  #Determining onlyo (only other-user) from first line (see /home/login/mst)
  if($0 ~ /^[a-zA-Z0-9_.\-]+/) {
    if(substr($0,NF-12) == "_messages.txt") {
      onlyo=substr($0,1,NF-13)
    }
    else {
      onlyo=$0
    }
  }
}

(NR != 1) {
  term=0
  other_user=""
  other_user_found=0
  if ($0 == "" || $1==" ") {
    if($1==" " && !prev_self_txn) {
      i=1;
      while($i != ":") { ++i }
      message_user = substr($0, 5, i-10); #position (i-5) - position (5) + 1 = length of message_user, where position (i-5) is the position of the last character of message_user
      old_ofs=OFS;OFS="";print "\n", message_user, "\t", substr($0,i+2);OFS=old_ofs;
    }
    next
  }

  for (a=1;a!=NF;a++) {
    # other-user determination
    if ($a == ":" && a == 25) {
      for (b=a+1;;b++) {
        if ($b ~ /^[a-zA-Z0-9_\-]/) {
          for(c=b;;c++) {
            if ($c ~ /^[^a-zA-Z0-9_\-]?$/) {
              break
            }
            else {
              other_user = ((other_user) ($c))
            }
          }
          break
        }
      }
      other_user_found=1
    }
    # self-username determination
    else if ($a == "<" && !self_user_found) {
      for (b=a+1;;b++) {
        if ($b == " ") {
          for(c=b+1;;c++) {
            if($c ~ /^[^a-zA-Z0-9_\-]?$/) {
              break
            }
            else {
              self_user = ((self_user) ($c))
            }
          }
          break
        }
      }
      if (!tsv) { print "S:", self_user }
      self_user_found = 1
      break
    }
    else if ($a == ">" && !self_user_found) {
      for (b=a+2;;b++) {
        if($b ~ /^[^a-zA-Z0-9_\-]?$/) {
          break
        }
        else {
          self_user = ((self_user) ($b))
        }
      }
      if (!tsv) { print "S:", self_user }
      self_user_found = 1
      break
    }
  }

  if (onlyo && (onlyo != other_user)) {
    #print other_user, "SKIPPED!"
    next
  }

  if (self_user == other_user) {
    prev_self_txn = 1
    #print "EQUAL USER!"
    next
  }
  else {
    prev_self_txn = 0
  }

  for (i=1;i!=NF;i++) {
    if (i <= 26 && !summary) {
      if(tsv && i == 25) {
        old_ors=ORS;ORS="";print "\t";ORS=old_ors
      }
      else if(tsv && i == 26) {
        #do nothing, don't print the "space"
      }
      else {
        old_ors=ORS;ORS="";print $i;ORS=old_ors
      }
    }
    else if ($i == "<") {
      for(j=i+4;;j++) {
        if ($j == ".") {
          l=10
          for(k=j+1;;k++) {
            if ($k ~ /^[^0-9]/) {
              break
            }
            term = term + $k/l
            l=l*10
          }
          break
        }
        else if ($j ~ /^[^0-9]/) {
          break
        }
        else {
          term = term*10 + $j
        }
      }
      sum = sum - term;
      summinus = summinus + term;
      olistminus[other_user] += term;
      olist[other_user] -= term;
      if(!summary) { if(tsv){ old_ofs=OFS;OFS="\t"; print other_user, self_user, -1*term; OFS=old_ofs } else { print -1*term, "O:", other_user } }
      next
    }
    else if($i == ">") {
      multiplier=1
      for(j=i-5;;j--) {
        if ($j == ".") {
          multiplier=1
          l=1
          for(k=j-1;;k--) {
            if ($k ~ /^[^0-9]/) {
              break
            }
            term = term + ($k)*l;
            l=l*10
          }
          break
        }
        else if ($j ~ /^[^0-9]/) {
          break
        }
        else {
          multiplier = multiplier*10
          term = (term + $j)/10
        }
      }
      term = term * multiplier
      sum = sum + term;
      sumplus = sumplus + term;
      olistplus[other_user] += term;
      olist[other_user] += term;
      if (!summary) {  if(tsv){ old_ofs=OFS;OFS="\t"; print other_user, self_user, term; OFS=old_ofs } else{ print 1*term, "O:", other_user } }
      next
    }
  }
}

END {
  if(!tsv) {
    print "--USERLIST-START--"
    for (key in olist) {
      print key, 1*olist[key], "Cr:", 1*olistplus[key], "Dr:", -1*olistminus[key]
    }
    print "---USERLIST-END---"
    print "GRAND TOTAL:", 1*sum
    print "CREDIT:", 1*sumplus
    print "DEBIT:", -1*summinus
  }
}