#!/bin/bash
if [ "$#" -ne 1  ]; then
   echo "Need to specify scenario output dir"
   exit
fi

cd $1
concatLcFile=lc_concat.txt
concatPriceFile=price_concat.txt
concatLUFile=LandUseSummary.csv
echo "Scenario,Year,Cropland,Pasture,ManForest,UnmanForest,Natural,EnergyCrop,FertCrop,IrrigCrop" > $concatLcFile
echo "Scenario,Year,Crop,Imports,Exports,Price,Stocks" > $concatPriceFile
echo "Scenario,cropType,yield,irrig,fert,otherint,area,Year" > $concatLUFile

find . -name "*lc.txt"|while read fname; do
        scenarioDir="$(dirname $fname)"
	scenario="$(echo $scenarioDir | sed 's,\.\/,,' | sed 's,\/,_,g')"
	echo "$scenario: $fname"

	i=1
	while read -r line
	do
		test $i -eq 1 && ((i=i+1)) && continue
		echo "${scenario},$line" >> $concatLcFile
	done < $fname

	# Should write a function for this, rather than duplicating, but do that later
	j=1
	while read -r line
	do
		test $j -eq 1 && ((j=j+1)) && continue
		echo "${scenario},$line" >> $concatPriceFile
	done < $scenarioDir/prices.txt

	k=1
	while read -r line
	do
		test $k -eq 1 && ((k=k+1)) && continue
		echo "${scenario},$line" >> $concatLUFile
	done < $scenarioDir/LandUseSummary.csv
done
