#!/bin/bash
cd "$(dirname "$0")"

function prepareFile(){
    path="$1"
    tmp_path="$(basename "$path")"
    (
    sed -n '/CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh/q;p' "$path"
    echo -e '\t// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh\n'
    ) > "$tmp_path"
    echo "$tmp_path"
}

function finishFile(){
    path="$1"
    tmp_path="$(basename "$path")"
    echo -e "}\n" >> "$tmp_path"
    mv "$tmp_path" "$path"
}


result="$(prepareFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java")"

# single object types
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | egrep '(toObject|toSingleMap)\(' | grep ', Calendar cal)' | while read method
do
    #echo "method: $method"
    method_name=$(echo $method | egrep -o '[^ ]+\(')
    echo "ResultSetMapper.$method_name)"

        cat >> "$result" <<EOF
	$(echo $method | sed "s/, Calendar cal)/)/")
		return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
	}

EOF
done

#everything else
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | grep '(ResultSet rs' | grep ', int arrayMaxLength, Calendar cal)' | while read method
do
    #echo "method: $method"
    method_name=$(echo $method | egrep -o '[^ ]+\(')
    echo "ResultSetMapper.$method_name)"

    for args in '' ', int arrayMaxLength' ', Calendar cal'
    do
        cat >> "$result" <<EOF
	$(echo $method | sed "s/, int arrayMaxLength, Calendar cal)/$args)/")
		return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
	}

EOF
    done
done

finishFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java"

query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java")"
caching_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java")"
null_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/NullQueryMapper.java")"

cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | grep '(ResultSet rs' | egrep -v '(int arrayMaxLength|Calendar cal)' | while read method
do
    #echo "method: $method"
    method_name=$(echo $method | egrep -o '[^ ]+\(')
    echo "QueryMapper.$method_name)"

    # QueryMapper.java
    cat >> "$query" <<EOF
	$(echo $method | sed -e 's/ResultSet rs/PreparedStatement ps/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
		return cm.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/bindExecute(ps, bindObjects)/' -e 's/) {/);/')
	}

	$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/ps/' -e 's/) {/, bindObjects);/')
		} finally {
			tryClose(ps);
		}
	}

EOF

    # CachingQueryMapper.java
    cat >> "$caching_query" <<EOF
	@Override
	$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
		return super.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/getPreparedStatement(sql)/' -e 's/) {/, bindObjects);/')
	}

EOF


    # NullQueryMapper.java
    for type in PreparedStatement String
    do
        cat <<EOF
	@Override
	$(echo $method | sed -e "s/ResultSet rs/$type query/" -e 's/) {/, final Object... bindObjects) {/')
		try {
			return delegate.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/query/' -e 's/) {/, bindObjects);/')
		} catch (Throwable e) {
			if (verbose) e.printStackTrace();
		}
		return null;
	}

EOF
    done >> "$null_query"
done

finishFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java"
finishFile "src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java"
finishFile "src/main/java/com/moparisthebest/jdbc/NullQueryMapper.java"
