#!/bin/bash

#
#  JShelter is a browser extension which increases level
#  of security, anonymity and privacy of the user while browsing the
#  internet.
#
#  Copyright (C) 2021  Marek Salon
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

#
# This script converts CSV to JSON configuration for FPD module. 
# Useful for creation of simple heuristics based on automated crawls.
# CSV needs to contain first 4 columns (other columns are ignored): 
#	Endpoint/Resource (name of the wrapped resource)
#	Operation/Type (type of the resource: get, set, call)
#	Weight (weight of the resource in given group)
#	Argument (optional value of argument that should be logged with the resource)
#

USAGE="USAGE: .\csv_to_wrappers input_csv [group_name] [output_name]

input_csv	Path to input CSV file containing required fields to create wrappers definition.
group_name	String containing optional name for group that covers created wrappers.
output_name	String containing optional name for JSON result file (without file extension)."

[ "$#" -lt 1 ] || [ "$#" -gt 3 ] && { echo "$USAGE"; exit 1; }

INPUT="$1"
[ ! -f $INPUT ] && { echo "ERROR: $INPUT file not found."; exit 1; }

GROUP_NAME="AutoGeneratedGroup"
[ ! -z "$2" ] && GROUP_NAME="$2"

OUTPUT="output_wrappers.json"
[ ! -z "$3" ] && OUTPUT="$3.json"

cat << EOF > $OUTPUT
[
EOF

OLDIFS=$IFS
IFS=','
echo -ne "CONVERTING"
x=0
while read RESOURCE OPERATION WEIGHT ARGUMENT REST
do
	((x=x+1))
	if [ $x -eq 10 ]; then
		x=0
		echo -ne "."
	fi
	
	if [ $(("$WEIGHT")) -gt 0 -a ! "$WEIGHT" = "WEIGHT" ]
	then
		if [ "$OPERATION" = "call" ]
		then
cat << EOF >> $OUTPUT
	{
			"resource":"${RESOURCE}",
			"type":"function",
			"groups":[
				{
					"group":"${GROUP_NAME}",
EOF
			if [ ! -z "$ARGUMENT" ]
			then
cat << EOF >> $OUTPUT			
					"arguments":${ARGUMENT},
EOF
			fi
cat << EOF >> $OUTPUT
					"criteria":[
						{
							"value":1,
							"weight":${WEIGHT}
						}
					]
				}
			]
	},
EOF
		else
cat << EOF >> $OUTPUT
	{
			"resource":"${RESOURCE}",
			"type":"property",
			"groups":[
				{
					"group":"${GROUP_NAME}",
					"property":"${OPERATION}",
EOF
			if [ ! -z "$ARGUMENT" ]
			then
cat << EOF >> $OUTPUT			
					"arguments":${ARGUMENT},
EOF
			fi
cat << EOF >> $OUTPUT
					"criteria":[
						{
							"value":1,
							"weight":${WEIGHT}
						}
					]
				}
			]
	},
EOF
		fi
	fi
done < $INPUT
echo "DONE!"
echo "Result saved to: $OUTPUT"
IFS=$OLDIFS

sed -i '$ s/.$//' $OUTPUT

cat << EOF >> $OUTPUT
]
EOF
