plugins {
    id 'application'
    id 'java'
    id 'war'
    id 'com.netflix.nebula.ospackage' version '11.8.0'
    id 'org.springframework.boot' version '3.0.6'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'checkstyle'
}

// Get version from main project gradle
def packVersion = properties.get("packageVersion");
def jarVersion = properties.get("jarVersion");
def projVersion = properties.get("projVersion");
//println "packageVersion is ${projVersion}"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

bootRun {
  if (project.hasProperty('debug')) {
    jvmArgs project.debug
  }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    all*.exclude module: 'spring-boot-starter-logging'
}

repositories {
    flatDir { dirs "lib" }
    mavenCentral()
}

dependencies {
    implementation project(':HIRS_Utils')
    implementation project(':HIRS_AttestationCA')

    implementation libs.pci
    implementation libs.gson
    implementation libs.bouncycastle
    implementation libs.guava
    implementation libs.jakarta.servlet
    implementation libs.jakarta.api
    implementation libs.jakarta.xml

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'org.apache.logging.log4j:log4j-spring-boot'
    implementation 'org.projectlombok:lombok'
    implementation 'commons-fileupload:commons-fileupload:1.5'
    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:10.1.5'

    compileOnly 'org.projectlombok:lombok'
    implementation 'org.mariadb.jdbc:mariadb-java-client:3.1.4'
    annotationProcessor 'org.projectlombok:lombok'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
    testImplementation 'org.hsqldb:hsqldb'
}

checkstyle {
    toolVersion = '10.12.7'
    configFile file("${rootDir}/config/checkstyle/checkstyle.xml")
}
// https://github.com/checkstyle/checkstyle/issues/14211#issuecomment-1884129948
configurations.checkstyle {
    resolutionStrategy.capabilitiesResolution.withCapability("com.google.collections:google-collections") {
        select("com.google.guava:guava:0")
    }
}
checkstyleMain {
    source ='src/main/java'
}
checkstyleTest {
    source ='src/test/java'
}
tasks.withType(Checkstyle) {
    reports {
        xml.required = false
        html.required = true
    }
}

test {
    useJUnitPlatform()
}

task buildVersion() {
    doLast {
        def verFile = new File(projectDir, "build/VERSION")
        verFile.write("${jarVersion}")
    }
}

ospackage {
    packageName = 'HIRS_AttestationCA'
    description = 'HIRS Attestation CA. Use systemctl status hirs-aca'
    os = LINUX
    arch = NOARCH
    version = "$packVersion"
    release = '2'
    
 
    user 'root'
    fileMode = 0755

    addParentDirs = true
    createDirectoryEntry true 

    into ('/etc/hirs') {
          from '../VERSION'
     }
    // copy json tables
    into ('/etc/hirs/aca/default-properties') {
         from '../HIRS_AttestationCA/src/main/resources/component-class.json'
         from '../HIRS_Utils/src/main/resources/vendor-table.json'
    }
    // copy springboot property file
    into ('/etc/hirs/aca/') {
         from '../HIRS_AttestationCAPortal/src/main/resources/application.properties'
         from 'build/VERSION'
    }
    // copy setup scripts to /opt/hirs/aca
    into ('/opt/hirs/aca/scripts/') {
         from '../package/linux/'
    }
    // copy the war file into /opt/hirs/aca
    into ('/opt/hirs/aca/') {
         from '../HIRS_AttestationCAPortal/build/libs/HIRS_AttestationCAPortal.war'
         user 'root'
         fileMode = 0755 
    }

    // Install - check for existing setup
    preInstall 'if [ -d /etc/hirs ]; then echo "Error: /etc/hirs/ exists, aborting install"; exit 1; fi;'
    preInstall 'if [ -d /opt/hirs ]; then echo "Error: /opt/hirs/ exists, aborting install"; exit 1; fi;'
    // Uninstall
    preUninstall 'bash /opt/hirs/aca/scripts/aca/aca_remove_setup.sh $1'
    preUninstall 'bash /opt/hirs/aca/scripts/systemd/aca_disable_service.sh'
    postUninstall 'if [ -d /etc/hirs ]; then rm -rf /etc/hirs; fi;'
    postUninstall 'if [ -d /opt/hirs ]; then rm -rf /opt/hirs; fi;'

    buildRpm {
        dependsOn  ':HIRS_AttestationCAPortal:buildVersion'
        dependsOn  ':HIRS_AttestationCAPortal:bootWar'
        arch = X86_64
        // Required apps will be aut installed by dnf if not present
        requires('java-17-openjdk', '17.0', GREATER | EQUAL)
        requires('mariadb-server' , '10.3' , GREATER | EQUAL)
        requires('procps-ng', '3.3.15', GREATER | EQUAL)
        requires('alternatives', '1.19', GREATER | EQUAL)
        // Post Trans stage (Occurs after required app and postInstall stage)
        // Note postInstall wont wait forrequired apps
        postTrans 'update-alternatives --set java java-17-openjdk.x86_64'
        postTrans 'firewall-cmd --add-port=8443/tcp --permanent'
        postTrans 'firewall-cmd --reload'
        postTrans 'bash /opt/hirs/aca/scripts/aca/aca_setup.sh -u'
        postTrans 'bash /opt/hirs/aca/scripts/systemd/aca_enable_service.sh'
        postTrans 'chmod +x /opt/hirs/aca/scripts/aca/*'
        postTrans 'if [ -f /opt/hirs/aca/VERSION ]; then rm /opt/hirs/aca/VERSION; fi;'
        // Wait for ACA to start up before finishing the install
        postTrans 'bash /opt/hirs/aca/scripts/aca/check_for_aca.sh'
    }

    buildDeb {
        dependsOn  ':HIRS_AttestationCAPortal:buildVersion'
        dependsOn  ':HIRS_AttestationCAPortal:bootWar'
        packageName = 'hirs-attestationca'
        arch = 'amd64'
        requires('openjdk-17-jdk', '17.0', GREATER | EQUAL)
        requires('mariadb-server' , '10.3' , GREATER | EQUAL)
        requires('curl')
        // Install after required packages
        postInstall 'bash /opt/hirs/aca/scripts/aca/aca_setup.sh -u'
        postInstall 'bash /opt/hirs/aca/scripts/systemd/aca_enable_service.sh'
        postInstall 'chmod +x /opt/hirs/aca/scripts/aca/*'
        postInstall 'if [ -f /opt/hirs/aca/VERSION ]; then rm /opt/hirs/aca/VERSION; fi;'
        // Wait for ACA to start up before finishing the install
        postInstall 'bash /opt/hirs/aca/scripts/aca/check_for_aca.sh'
    }
}
