set -e
source mysercetfile
echo 0 > flag_change
gh config set -h github.com git_protocol https
echo "$repo_openeuicc_github_token_t" | gh auth login --with-token
gh repo clone hzy132/OpenEUICC_for_Magisk
cd OpenEUICC_for_Magisk
recorded_commit=`cat commit-openeuicc`
date > .temp
git config --global user.name "hzy132"
git config --global user.email "35918116+hzy132@users.noreply.github.com"
git add .temp
git commit -m "useless file"
git remote set-url origin https://x-access-token:$repo_openeuicc_github_token_t@github.com/hzy132/OpenEUICC_for_Magisk.git
git push origin main
cd ..
git clone https://gitea.angry.im/PeterCxy/OpenEUICC.git
cd OpenEUICC
now_commit=`git rev-parse HEAD`
now_commit_short=`git rev-parse --short HEAD`
cd ../OpenEUICC_for_Magisk
if [ "$now_commit" != "$recorded_commit" ]
then
  echo 1 > ../flag_change
  echo "$now_commit" > commit-openeuicc
  git add commit-openeuicc
  git commit -m "Update the latest commit hash of OpenEUICC"
  git remote set-url origin https://x-access-token:$repo_openeuicc_github_token_t@github.com/hzy132/OpenEUICC_for_Magisk.git
  git push origin main
  cd ../OpenEUICC
  echo "$key_file_openeuicc_base" | base64 -d > openeuicc.jks
  echo "$key_file_keystore_properties_half_base" | base64 -d > keystore.properties
  nowpath=`pwd`
  echo "storeFile=""$nowpath""/openeuicc.jks" >> keystore.properties
  git submodule update --init
  ./gradlew :app:assembleRelease
  ./gradlew :app-unpriv:assembleRelease
  cd ..
  mkdir module_magisk_output
  mkdir output
  touch output/magisk_module_filename
  touch output/apk_unpriv_filename
  touch output/release_bodyfile
  touch output/version_str_commit
  outmeta=`cat OpenEUICC/app/build/outputs/apk/release/output-metadata.json`
  outmeta_un=`cat OpenEUICC/app-unpriv/build/outputs/apk/release/output-metadata.json`
  str_keyword_1="\"version\": "
  str_keyword_2="\"versionCode\": "
  str_keyword_3="\"versionName\": \""
  version_t1=${outmeta#*$str_keyword_1}
  version_t2=${version_t1%%,*}
  versioncode_t1=${outmeta#*$str_keyword_2}
  versioncode=${versioncode_t1%%,*}
  version=$version_t2"("$versioncode")"
  versionname_t1=${outmeta_un#*$str_keyword_3}
  versionname_t2=${versionname_t1%%\"*}
  versionname="app-"$versionname_t2"-release.apk"
  cp OpenEUICC/app-unpriv/build/outputs/apk/release/app-unpriv-release.apk output/$versionname
  cp -R OpenEUICC_for_Magisk/magisk-module/META-INF module_magisk_output
  cp -R OpenEUICC_for_Magisk/magisk-module/system module_magisk_output
  cp OpenEUICC_for_Magisk/magisk-module/customize.sh module_magisk_output
  cp OpenEUICC_for_Magisk/magisk-module/uninstall.sh module_magisk_output
  str_module_prop_t1=`cat OpenEUICC_for_Magisk/magisk-module/module.prop.temp`
  str_module_prop_t2=${str_module_prop_t1/openeuiccversion/$version}
  str_module_prop=${str_module_prop_t2/openeuiccversioncode/$versioncode}
  echo "$str_module_prop" > module_magisk_output/module.prop
  mkdir -p module_magisk_output/system/system_ext/priv-app/OpenEUICC
  cp OpenEUICC/app/build/outputs/apk/release/app-release.apk module_magisk_output/system/system_ext/priv-app/OpenEUICC/OpenEUICC.apk
  rm module_magisk_output/system/system_ext/etc/permissions/privapp_whitelist_im.angry.openeuicc.xml
  cp OpenEUICC/privapp_whitelist_im.angry.openeuicc.xml module_magisk_output/system/system_ext/etc/permissions/privapp_whitelist_im.angry.openeuicc.xml
  cd module_magisk_output
  modulefilename="OpenEUICC-Magisk_module-"$version".zip"
  zip -q -r $modulefilename *
  cd ..
  cp module_magisk_output/$modulefilename output
  echo $modulefilename > output/magisk_module_filename
  echo $versionname > output/apk_unpriv_filename
  echo -e "This release is automatically generated by GitHub Actions.\nUpdate to the commit with hash "$now_commit" of OpenEUICC repository." > output/release_bodyfile
  echo "$version""-""$now_commit_short" > output/version_str_commit
  updatejsontemp_t1=`cat OpenEUICC_for_Magisk/magisk-module/updateJson.temp`
  updatejsontemp_t2=${updatejsontemp_t1/versionstring/$version}
  updatejsontemp_t3=${updatejsontemp_t2/versioncodeint/$versioncode}
  releasemodulefilename="OpenEUICC-Magisk_module-"$version_t2"."$versioncode".zip"
  updatejsontemp=${updatejsontemp_t3/OpenEUICC_Magisk_module_filename/$releasemodulefilename}
  updatejson_now=`cat OpenEUICC_for_Magisk/magisk-module/updateJson`
  if [ "$updatejsontemp" != "$updatejson_now" ]
  then
    cd OpenEUICC_for_Magisk
    echo "$updatejsontemp" > magisk-module/updateJson
    git add magisk-module/updateJson
    git commit -m "Update info for the latest version"
    git remote set-url origin https://x-access-token:$repo_openeuicc_github_token_t@github.com/hzy132/OpenEUICC_for_Magisk.git
    git push origin main
    cd magisk-module
    cp updateJson update.json
    cd ..
    git add magisk-module/update.json
    git commit -m "Update info for the latest version"
    git remote set-url origin https://x-access-token:$repo_openeuicc_github_token_t@github.com/hzy132/OpenEUICC_for_Magisk.git
    git push origin main
  fi
fi

