version=$1

DIRNAME=`dirname "$0"`
if [ "$DIRNAME" = "." ]; then
  DIRNAME="$PWD"
fi

mkdir -p "$DIRNAME/target/tomcat/"

pushd "$DIRNAME" > /dev/null

for d in *; do
  bn=$(basename $d)
  if [ -d "$d/src/main/config/patch" ]; then
    curl https://repo1.maven.org/maven2/org/apache/tomcat/tomcat-$bn/$version/tomcat-$bn-$version-sources.jar > $DIRNAME/target/tomcat/tomcat-$bn-$version-sources.jar
    pushd "$d/src/main/config/patch" > /dev/null
    for f in *; do
      bn2=$(basename $f)
      fn=$(echo $bn2 | sed 's|\.|/|g').java
      pushd "../../java" > /dev/null
      if [ "$2" != "" ]; then
        cp $fn $fn.new
        jar xf $DIRNAME/target/tomcat/tomcat-$bn-$version-sources.jar $fn
        dos2unix $fn
        diff -u $fn $fn.new > ../config/patch/$f
        sed -i~ 's/^\(+++\|---\)\(.*\)\t[0-9]\+-[0-9]\+-[0-9]\+ [0-9]\+:[0-9]\+:[0-9]\+\.[0-9]\+ .[0-9]\+/\1\2\t2021-01-01 00:00:00.000000000 +0000/g' ../config/patch/$f
        rm ../config/patch/$f~
        rm $fn.new     
        git checkout HEAD -- $fn
      else
        cp $fn $fn.back
        jar xf $DIRNAME/target/tomcat/tomcat-$bn-$version-sources.jar $fn
        dos2unix $fn
        patch $fn ../config/patch/$f
        rm $fn.back
      fi
      popd > /dev/null
    done
    popd > /dev/null
  fi
done

popd > /dev/null

MAIN_VERSION=$(echo $version | cut -d. -f1)

rm -rf "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer/conf"
rm -rf "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer/root"
mkdir "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer/root"
rm -rf "$DIRNAME/full_installer/src/main/resources/org/apache/tomcat/vestige/full_installer/webapps"

if [ ! -f "$DIRNAME/target/tomcat/apache-tomcat-$version.zip" ]; then
  curl https://archive.apache.org/dist/tomcat/tomcat-$MAIN_VERSION/v$version/bin/apache-tomcat-$version.zip > "$DIRNAME/target/tomcat/apache-tomcat-$version.zip"
fi

rm -rf "$DIRNAME/target/tomcat/apache-tomcat-$version"
mkdir -p "$DIRNAME/target/tomcat/apache-tomcat-$version"
pushd "$DIRNAME/target/tomcat" > /dev/null
unzip apache-tomcat-$version.zip
pushd apache-tomcat-$version > /dev/null


for dep in tomcat-catalina tomcat-servlet-api tomcat-el-api tomcat-dbcp; do
  xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/APPLICATION" \
  -d '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[starts-with(@artifactId, "tomcat-i18n-") and @artifactId != "tomcat-i18n-fr"]' \
  ../../../descriptors/src/main/config/tomcat.xml
  xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/APPLICATION" \
  -d '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/text()[last()]' \
  -a '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t text -n "" -v "
      " \
  ../../../descriptors/src/main/config/tomcat.xml


  xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/VERIFY" \
  -d '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[starts-with(@artifactId, "tomcat-i18n-") and @artifactId != "tomcat-i18n-fr"]' \
  ../../../descriptors/src/main/config/verify.xml
  xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/VERIFY" \
  -d '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/text()[last()]' \
  -a '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t text -n "" -v "
      " \
  ../../../descriptors/src/main/config/verify.xml


  for tlib in $(find lib -name 'tomcat-i18n-*.jar'); do

    lan=$(echo $tlib | sed 's|lib/tomcat-i18n-\(.*\)\.jar|\1|')
    if [ "$lan" = "fr" ]; then
      continue
    fi
    xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/APPLICATION" \
    -a '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t elem -n addDependency -v "" \
    ../../../descriptors/src/main/config/tomcat.xml
  
    xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/APPLICATION" \
    -i '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t text -n "" -v "
        " \
    -i '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t attr -n "artifactId" -v "tomcat-i18n-$lan" \
    -i '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t attr -n "groupId" -v "org.apache.tomcat" \
    -i '/va:application/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t attr -n "version" -v '${tomcat.version}' \
    ../../../descriptors/src/main/config/tomcat.xml
    
    xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/VERIFY" \
    -a '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t elem -n addDependency -v "" \
    ../../../descriptors/src/main/config/verify.xml
  
    xmlstarlet ed -L -P -N va="http://gaellalire.fr/vestige/VERIFY" \
    -i '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t text -n "" -v "
        " \
    -i '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t attr -n "artifactId" -v "tomcat-i18n-$lan" \
    -i '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t attr -n "groupId" -v "org.apache.tomcat" \
    -i '/va:verify/va:configurations/va:mavenConfig/va:modifyDependency[@artifactId = "'$dep'"]/va:addDependency[last()]' -t attr -n "version" -v '${tomcat.version}' \
    ../../../descriptors/src/main/config/verify.xml
    
  done
done

rm -rf bin lib logs temp RUNNING.txt
find . -depth 1 -type f  | sed 's/^\..//' > "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer/root.txt"
find . -depth 1 -type f -exec mv \{\} "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer/root/" \;

popd > /dev/null
popd > /dev/null


mv "$DIRNAME/target/tomcat/apache-tomcat-$version/conf" "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer"
mv "$DIRNAME/target/tomcat/apache-tomcat-$version/webapps" "$DIRNAME/full_installer/src/main/resources/org/apache/tomcat/vestige/full_installer/"

pushd "$DIRNAME/installer/src/main/resources/org/apache/tomcat/vestige/installer"

sed -i~ 's/autoDeploy="true"/autoDeploy="true" deployXML="true"/' conf/server.xml
rm conf/server.xml~

sed -i~ 's/StandardJarScanFilter.jarsToSkip=\\/StandardJarScanFilter.jarsToSkip=vestige.core-*.jar,javax.servlet-api-*.jar,\\/' conf/catalina.properties
rm conf/catalina.properties~

xmlstarlet ed -L -P  \
-s '/Context' -t text -n "" -v "    " \
-s /Context -t elem -n Resources -v "
      " \
-i '/Context/Resources[last()]' -t attr -n "className" -v "org.apache.catalina.webresources.StandardRoot" \
-s '/Context/Resources[last()]' -t elem -n "PreResources" -v "" \
-i '/Context/Resources[last()]/PreResources' -t attr -n "className" -v "org.apache.catalina.webresources.DirResourceSet" \
-i '/Context/Resources[last()]/PreResources' -t attr -n "base" -v '${catalina.base}/pre-resources' \
-i '/Context/Resources[last()]/PreResources' -t attr -n "internalPath" -v "/"  \
-i '/Context/Resources[last()]/PreResources' -t attr -n "webAppMount" -v "/WEB-INF/classes" \
-s '/Context/Resources[last()]' -t text -n "" -v "
    " \
-a '/Context/Resources[last()]' -t text -n "" -v "
" conf/context.xml

find conf -type f > conf.txt
popd

pushd "$DIRNAME/full_installer/src/main/resources/org/apache/tomcat/vestige/full_installer"
find webapps -type f > webapps.txt
popd

mvn versions:set -DnewVersion=$version.1-SNAPSHOT -DgenerateBackupPoms=false

sed -i~ 's/<tomcat.version>.*<\/tomcat.version>/<tomcat.version>'${version}'<\/tomcat.version>/' pom.xml
rm pom.xml~

sed -i~ 's/version: .*/version: '${version}'/' README.md
rm README.md~

sed -i~ 's/tomcat-.* \./tomcat-'${version}' \./' docker/build
rm docker/build~

sed -i~ 's/tomcat-minimal-.*\.xml/tomcat-minimal-'${version}'\.xml/' docker/Dockerfile
rm docker/Dockerfile~

sed -i~ 's/tomcat-.*`/tomcat-'${version}'`/' docker/README.txt
rm docker/README.txt~

