#!/bin/bash
# Generate Maven pom.xml for decompiled project
# Usage: ./generate-pom.sh <project_dir> <group_id> <artifact_id>

set -e

PROJECT_DIR="${1:?Usage: generate-pom.sh <project_dir> <group_id> <artifact_id>}"
GROUP_ID="${2:-com.example}"
ARTIFACT_ID="${3:-$(basename "$PROJECT_DIR")}"
VERSION="1.0.0"

POM_FILE="$PROJECT_DIR/pom.xml"

echo "Generating Maven pom.xml..."
echo "  Group: $GROUP_ID"
echo "  Artifact: $ARTIFACT_ID"
echo "  Output: $POM_FILE"

# Detect common frameworks from class names
SPRING=false
STRUTS=false
IBATIS=false
HIBERNATE=false
AXIS=false

# Scan for framework indicators
if find "$PROJECT_DIR" -name "*.java" -exec grep -l "org.springframework" {} \; 2>/dev/null | head -1 | grep -q .; then
    SPRING=true
fi
if find "$PROJECT_DIR" -name "*.java" -exec grep -l "org.apache.struts" {} \; 2>/dev/null | head -1 | grep -q .; then
    STRUTS=true
fi
if find "$PROJECT_DIR" -name "*.java" -exec grep -l "com.ibatis" {} \; 2>/dev/null | head -1 | grep -q .; then
    IBATIS=true
fi
if find "$PROJECT_DIR" -name "*.java" -exec grep -l "org.hibernate" {} \; 2>/dev/null | head -1 | grep -q .; then
    HIBERNATE=true
fi
if find "$PROJECT_DIR" -name "*.java" -exec grep -l "org.apache.axis" {} \; 2>/dev/null | head -1 | grep -q .; then
    AXIS=true
fi

# Generate pom.xml
cat > "$POM_FILE" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>${GROUP_ID}</groupId>
    <artifactId>${ARTIFACT_ID}</artifactId>
    <version>${VERSION}</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Servlet API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSP API -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
EOF

# Add Spring dependencies if detected
if [ "$SPRING" = true ]; then
    cat >> "$POM_FILE" << EOF

        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.18.RELEASE</version>
        </dependency>
EOF
fi

# Add Struts if detected
if [ "$STRUTS" = true ]; then
    cat >> "$POM_FILE" << EOF

        <!-- Struts 2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.37</version>
        </dependency>
EOF
fi

# Add iBatis if detected
if [ "$IBATIS" = true ]; then
    cat >> "$POM_FILE" << EOF

        <!-- iBatis -->
        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-sqlmap</artifactId>
            <version>2.3.4.726</version>
        </dependency>
EOF
fi

# Add Axis if detected
if [ "$AXIS" = true ]; then
    cat >> "$POM_FILE" << EOF

        <!-- Apache Axis (Web Services) -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxrpc-api</artifactId>
            <version>1.1</version>
        </dependency>
EOF
fi

# Add Oracle JDBC (common in enterprise apps)
cat >> "$POM_FILE" << EOF

        <!-- Oracle JDBC (install manually to local repo) -->
        <!--
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.4</version>
        </dependency>
        -->

        <!-- Logging -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- JUnit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${ARTIFACT_ID}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
    </build>
</project>
EOF

echo "Generated: $POM_FILE"
echo ""
echo "Detected frameworks:"
[ "$SPRING" = true ] && echo "  ✓ Spring"
[ "$STRUTS" = true ] && echo "  ✓ Struts"
[ "$IBATIS" = true ] && echo "  ✓ iBatis"
[ "$AXIS" = true ] && echo "  ✓ Axis Web Services"
echo ""
echo "Note: You may need to manually install Oracle JDBC to local Maven repo:"
echo "  mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4 -Dpackaging=jar"
