#!/bin/bash


function help(){
	cat <<EOF
Usage: $0 [--list | --test] [--skip NUM] [--amount NUM] [--importType TYPE] data.1pif
  test: testMode, do not actually store data
  list: just list elements
  skip: skip NUM entries before actually doing anything, 0 is default
  amount: amount of entries to process, 0 means all
  importType: only import this type, e.g. securenotes.SecureNote

EOF
}
if [ "q$1" == "q" ]; then
	help
	exit 1
fi

onlyList=0
onlyTest=0
skip=0
amount=0
importType=""
while [ "q$1" != "q" ]; do
	echo "Checking $1"
	case "$1" in
		"--list")
			echo "only listing"
			onlyList=1
			shift
			;;
		"--test")
			echo "only testing"
			onlyTest=1
			shift
			;;
		"--skip")
			echo "skipping $2"
			skip=$2
			shift
			shift
			;;
		"--amount")
			echo "limiting to $2"
			amount=$2
			shift
			shift
			;;
		"--importType")
			echo "Only importing $2"
			importType=$2
			shift
			shift
			;;
		"--test")
			echo "testMode"
			shift
			;;
		"--help")
			help
			exit
			;;
		*)
			echo "File to read: $1"
			fileName=$1
			shift
			;;
	esac
		
done

jsonOpts="-json_opt pretty,utf8,allow_bignum"
function getField {
	echo "$1" | json_pp $jsonOpts | grep "\"$2\" :" | tr -d ' ",' | head -n 1 | cut -f 2- -d: 
}

function getLastField {
	echo "$1" | json_pp $jsonOpts | grep "\"$2\" :" | tr -d ' ",' | tail -n 1 | cut -f 2- -d: 

}

trap ctrlc INT

function ctrlc(){
	echo "Exiting..." >&2
	exit 0
}

count=$(grep "**" $fileName | wc -l|tr -d " \t")
echo "Found $count entries" >&2
if [ $skip -gt 0 ]; then
	echo "Skipping to $skip" >&2
fi
c=0
am=0;
while IFS= read -r line; do 
	
	let c=c+1
	if [ $c -lt $skip ]; then
		continue
	fi
	if [ $amount -gt 0 ] && [ $am -ge $amount ]; then
		echo "amount $amount reached" >&2
		exit 0
	fi	
	let am=am+1
	if echo "$line" | egrep '^\*\*' >/dev/null; then 
		echo "id" >/dev/null
	else 	
		type=$(getField "$line" "typeName")
		if [ "q$importType" != "q$type" ] && [ "q$importType" != "q" ]; then
			#echo "Skipping $type"
			continue
		fi
		echo "processing $c/$count" >&2
		#line=$(echo "$line" | tr "\n\t\\" " " | sed -e 's/" "//g' )
		if [ $onlyList == 1 ]; then
			echo -e "$line\n" | json_pp $jsonOpts | while IFS= read l; do
			echo "$l" | sed -e "s/\$problematicPassword/$pp/g"
			done
		else
			#echo -e "$line\n" | json_pp $jsonOpts | grep typeName || ( echo "$line\n"; continue; )
			#type=$(echo -e "$line\n" | json_pp $jsonOpts | grep typeName | tr -d ' ",' | cut -f 2 -d:)
			echo ">>>>>>>>> ====== Type: $type ======= <<<<<<<<<<<<"
			path=""
			content=""
			if [ "$type" == "system.folder.SavedSearch" ]; then
				echo -e "ignoring saved search"
			elif [ "$type" == "passwords.Password" ]; then
				location=$(getField "$line" "location" | tr -d ' ' ) 
				locationKey=$(getField "$line" "locationKey")
				title=$(getLastField "$line" "title")
				password=$(getField "$line" "password"| sed -e "s/\$problematicPassword/$pp/g")
				echo "Location >>> $location <<<"
				path="passwords/$title"
				content="$password\nLocation: $location"
			elif [ "$type" == "webforms.WebForm" ]; then
				url=$(getField "$line" "url")
				title=$(getLastField "$line" "title")
				password=$(echo -e "$line\n" | json_pp $jsonOpts | grep -A4 '"designation" : "password"' | grep '"value"' | cut -f2 -d: | tr -d '" '| sed -e "s/\$problematicPassword/$pp/g")
				username=$(echo -e "$line\n" | json_pp $jsonOpts | grep -A4 '"designation" : "username"' | grep '"value"' | cut -f2 -d: | tr -d '" ')
				path="passwords/$title"
				content="$password\nusername: $username"
			elif [ "$type" == "wallet.financial.CreditCard" ]; then
				bank=$(getField "$line" "bank")
				cardholder=$(getField "$line" "cardholder")
				ccnum=$(getField "$line" "ccnum")
				expirymm=$(getField "$line" "expiry_mm")
				expiryyy=$(getField "$line" "expiry_yy")
				notes=$(getField "$line" "notesPlain")
				cardtype=$(getLastField "$line" "type")
				pin=$(getLastField "$line" "pin")
				cvv=$(getLastField "$line" "cvv")
				phoneNum=$(getField "$line" "phoneLocal" | tr -d ' ')
				title=$(getLastField "$line" "title")
				path="ccards/$title"
				content="$ccnum\nBank: $bank\ncardType: $cardtype\nccNumber: $ccnum\nexpires: $expirymm/$expiryyy\nCVV: $cvv\nPIN: $pin\nnotes: $notes"
				
			elif [ "$type" == "securenotes.SecureNote" ]; then
				title=$(getField "$line" "title")
				note=$(getField "$line" "notesPlain" | sed -e 's/\\n/ >\n  /g')
				content="\nNote: $note"
				path="notes/$title"
			elif [ "$type" == "wallet.financial.BankAccountUS" ]; then
				bankName=$(getField "$line" "bankName")
				accountType=$(getField "$line" "accountType")
				iban=$(getField "$line" "iban")
				owner=$(getField "$line" "owner")
				title=$(getLastField "$line" "title")
				path="bank/$title"
				content="$iban\nBank: $bankName\niban: $iban\nKontoinhaber: $owner\nAccountType: $accountType"
			
			elif [ "$type" == "wallet.membership.RewardProgram" ]; then
				member_name=$(getField "$line" "member_name")
				company=$(getField "$line" "company_name")
				sinceMM=$(getField "$line" "member_since_mm")
				sinceYY=$(getField "$line" "member_since_yy")
				membershipNo=$(getField "$line" "membership_no")
				pin=$(getField "$line" "pin")
				title=$(getLastField "$line" "title")
				path="membership/$title"
				content="$pin\nName: $member_name\nMembership No: $membershipNo\nMember since: $sinceMM/$sinceYY\nPin: $pin"
				
			elif [ "$type" == "wallet.membership.Membership" ]; then
				membershipNo=$(getField "$line" "membership_no")
				memberName=$(getField "$line" "member_name")
				org_name=$(getField "$line" "org_name")
				title=$(getLastField "$line" "title")
				url=$(getField "$line" "website")
				
				path="membership/$title"
				content="$membershipNo\nMemberShipNo: $membershipNo\nMembershipName: $membershipName\nOrganisation: $org_name\nUrl: $url"
			elif [ "$type" == "wallet.computer.UnixServer" ]; then
				note=$(getField "$line" "notesPlain")
				adminPwd=$(getField "$line" "admin_console_password")
				password=$(getField "$line" "password")
				title=$(getLastField "$line" "title")
				username=$(getField "$line" "username")
				path="passwords/$title"
				content="$adminPwd\nUsername: $username\nPassword: $adminPwd\n Note: $note"
			elif [ "$type" == "wallet.computer.Router" ]; then
				dpassword=$(getField "$line" "disk_password")
				networkName=$(getField "$line" "network_name")
				password=$(getField "$line" "password")
				title=$(getLastField "$line" "title")
				name=$(getField "$line" "name")
				server=$(getField "$line" "server")
				wPassword=$(getField "$line" "wireless_password")
				wSecurity=$(getField "$line" "wireless_security")
				path="wifi/$title"
				content="$password\nDiskPassword: $dpassword\nNetworkName: $networkName\nName: $name\nServer: $server\nWireless Password: $wPAssword\nWireless Security: $wSecurity"
				
			elif [ "$type" == "identities.Identity" ]; then
				echo "Ignoring identity..."
				continue
			elif [ "$type" == "wallet.onlineservices.Email.v2" ]; then
				popAuth=$(getField "$line" "pop_authentication")
				popPwd=$(getField "$line" "pop_password")
				popSecurity=$(getField "$line" "pop_security")
				popServer=$(getField "$line" "pop_server")
				popType=$(getField "$line" "pop_type")
				popUser=$(getField "$line" "pop_username")
				title=$(getLastField "$line" "title")
				path="email/$title"
				content="$popPwd\nUsername: $popUser\nServer: $popServer\nType: $popType\nSecurity: $popSecurity\nAuth: $popAuth\nPassword: $popPwd"
			elif [ "$type" == "wallet.government.SsnUS" ]; then
				name=$(getField "$line" "name")
				number=$(getField "$line" "number")
				title=$(getLastField "$line" "title")
				path="id/$title"
				content="$number\nName: $name\nNumber: $number"
			elif [ "$type" == "wallet.computer.License" ]; then
				regCode=$(getField "$line" "reg_code" | sed -e 's/\\n/ >\n  /g')
				regEmail=$(getField "$line" "reg_email")
				regName=$(getField "$line" "reg_name")
				title=$(getLastField "$line" "title")
				path="licenses/$title"
				content="\nRegisteredEmail: $regEmail\nRegisteredName: $regName\nCode: $regCode";
			else	
				echo "Unhandeled: '$type'"
				echo -e "$line\n" | json_pt $jsonOpts
			fi
			if [ "q$content" == "q" ]; then
				continue
			fi
			if [ $onlyTest -eq 1 ]; then
				echo -e "would insert to $path:\n$content"
			else 
				echo -e "$content" | pass insert -m -f "$path"
			fi
		fi
		#sleep 1
	fi
done  < $fileName
