#!/usr/bin/env bash
#winezgui-template-download

winezgui-template-download()
{
  
  SOURCE "script-check-variables-loaded-or-not"
  script-check-variables-loaded-or-not
  mkdir -p "${TEMPLATES_DIR}"
  if [ -w ${TEMPLATES_DIR} ]; then
       dbug "I: Downloading https://github.com/fastrizwaan/WineZGUI-Templates/releases/download/1.1/DotNet48-Wine8-20230301.tar.zst"
       cd ${TEMPLATES_DIR}
       (wget -c https://github.com/fastrizwaan/WineZGUI-Templates/releases/download/1.1/DotNet48-Wine8-20230301.tar.zst & )|\
       ${ZENITY_CMD} --auto-close --width=500 --progress --pulsate --title="Downloading \"DotNet48-Wine8-20230301.tar.zst\"...";
       
       TEMPLATE_CHECKSUM="4ab3761522df9bebd34966b6a6b87eefc6ca470588462cf770ba45fec108f420"
       DOWNLOAD_CHECKSUM=$(sha256sum "DotNet48-Wine8-20230301.tar.zst"|cut -f1 -d " ")

       dbug "I: Template Downlaod: Shasum github  : ${TEMPLATE_CHECKSUM}"
       dbug "I: Template Downlaod: Shasum Download: ${DOWNLOAD_CHECKSUM}"
       # Compare NEW_EXE_CHECKSUM with one existing in ${SCRIPTFILE}
       if [ "${TEMPLATE_CHECKSUM}" = "${DOWNLOAD_CHECKSUM}" ]; then         
            (${TAR_CMD} -xvf DotNet48-Wine8-20230301.tar.zst -C ${TEMPLATES_DIR};)| \
            ${ZENITY_CMD}  --progress --pulsate --auto-close --title="Extracting \"DotNet48-Wine8-20230301.tar.zst\"" --width=500 \
            --text="Extracting at ${TEMPLATES_DIR}"
            dbug "I: Extracted DotNet48-Wine8-20230301.tar.zst"
            #rm DotNet48-Wine8-20230301.tar.zst && \
            dbug "I: Removed DotNet48-Wine8-20230301.tar.zst after extraction"
            SOURCE "winezgui-template-change"
            winezgui-template-change
            #${ZENITY_CMD} --info --title="Download Complete!" --text="Change templates using Template->Change..."
       else
            killall -KILL wget
            ${ZENITY_CMD} --info --title="Download Checksum Mismatch!" --text="Retry downloading Template..."
       fi
  else 
       echo "E: Cannot write to ${TEMPLATES_DIR}"
  fi

}