pipelines:
  default:
    - step:
        script:
          - git config --global user.email "ivan.zolotukhin@gmail.com"
          - git config --global user.name "Ivan Zolotukhin" 
          - git config --global push.default simple
          
          - if [[ `git diff-tree --no-commit-id --name-only -r master | grep paper2` ]]; then OVERLEAF_ID=16873608wgwhhdhdthvx && export PAPER_DIR=paper2_variability && export TEX_FILE=j1605_variability.tex && export BIB_FILE=j1605_variability.bib ; else export OVERLEAF_ID=13956350mydxfckkcgjr && export PAPER_DIR=paper && export TEX_FILE=IMBH_AGN.tex && export BIB_FILE=IMBH.bib ; fi
          
          - git checkout master
          - git pull # get to the tip of the origin/master from Bitbucket, i.e. this pipeline is not version sensitive, running it on any version should provide similar result
          
          - git clone https://git.overleaf.com/$OVERLEAF_ID
          - cd $OVERLEAF_ID/
          - git checkout `git log 'HEAD^{/Automatic commit}' --format="%H" -n 1` $TEX_FILE $BIB_FILE # get last common ancestor version which is last automatic commit to Overleaf  
          - cp $TEX_FILE ../
          - cp $BIB_FILE ../
          - rm -rf *
          - cp -r ../$PAPER_DIR/* ./ # fetch possible new files (e.g. figures) from Bitbucket repo; remove and copy everything is needed so that files removals also propagate to Overleaf
          - git checkout master $TEX_FILE $BIB_FILE
          
          - set +e # ignore errors of git merge-file as no one understands why this happens
          - git merge-file -L Overleaf_current -L Overleaf_last_sync -L Bitbucket_current $TEX_FILE ../$TEX_FILE ../$PAPER_DIR/$TEX_FILE
          - git merge-file -L Overleaf_current -L Overleaf_last_sync -L Bitbucket_current $BIB_FILE ../$BIB_FILE ../$PAPER_DIR/$BIB_FILE
          - set -e # stop on any error below
          
          - git status # --porcelain
          - if [[ ! `git status --porcelain` ]]; then echo "No changes merged to Overleaf repo, doing empty sync commit to Overleaf" && git commit --allow-empty $TEX_FILE $BIB_FILE -m "Automatic commit from Bitbucket pipeline (empty commit for syncing)" && git push; else echo ".tex or other files merged to Overleaf repo, pushing to Overleaf" && git add . && git commit -a -m "Automatic commit from Bitbucket pipeline" && git push; fi
          
          - cp $TEX_FILE ../$PAPER_DIR/
          - cp $BIB_FILE ../$PAPER_DIR/
          - cd ../$PAPER_DIR/
          - rm -rf ../$OVERLEAF_ID/
          - rm -rf ../$TEX_FILE
          - rm -rf ../$BIB_FILE
          - git config --get remote.origin.url
          - git remote show origin
          - git status # for some reason this is needed to be launched before git diff-index to avoid fake commits of touched but unchanged file. Note that diff-index ignores untracked files unlike git status --porcelain above, that is we only accept .tex file changes from Overleaf
          - if git diff-index --quiet HEAD --; then echo "No change in .tex file"; else echo ".tex file changed, pushing to Bitbucket" && git commit $TEX_FILE $BIB_FILE -m "IMBH-57 Automatic commit from Bitbucket pipeline[skip ci]" && git push; fi
          

# contents of custom pipeline is copy-pasted from the default one
  custom:
    sync-overleaf:
      - step:
          script:
          - git config --global user.email "ivan.zolotukhin@gmail.com"
          - git config --global user.name "Ivan Zolotukhin" 
          - git config --global push.default simple
          
          - if [[ `git diff-tree --no-commit-id --name-only -r master | grep paper2` ]]; then OVERLEAF_ID=16873608wgwhhdhdthvx && export PAPER_DIR=paper2_variability && export TEX_FILE=j1605_variability.tex && export BIB_FILE=j1605_variability.bib ; else export OVERLEAF_ID=13956350mydxfckkcgjr && export PAPER_DIR=paper && export TEX_FILE=IMBH_AGN.tex && export BIB_FILE=IMBH.bib ; fi
          
          - git checkout master
          - git pull # get to the tip of the origin/master from Bitbucket, i.e. this pipeline is not version sensitive, running it on any version should provide similar result
          
          - git clone https://git.overleaf.com/$OVERLEAF_ID
          - cd $OVERLEAF_ID/
          - git checkout `git log 'HEAD^{/Automatic commit}' --format="%H" -n 1` $TEX_FILE $BIB_FILE # get last common ancestor version which is last automatic commit to Overleaf  
          - cp $TEX_FILE ../
          - cp $BIB_FILE ../
          - rm -rf *
          - cp -r ../$PAPER_DIR/* ./ # fetch possible new files (e.g. figures) from Bitbucket repo; remove and copy everything is needed so that files removals also propagate to Overleaf
          - git checkout master $TEX_FILE $BIB_FILE
          
          - set +e # ignore errors of git merge-file as no one understands why this happens
          - git merge-file -L Overleaf_current -L Overleaf_last_sync -L Bitbucket_current $TEX_FILE ../$TEX_FILE ../$PAPER_DIR/$TEX_FILE
          - git merge-file -L Overleaf_current -L Overleaf_last_sync -L Bitbucket_current $BIB_FILE ../$BIB_FILE ../$PAPER_DIR/$BIB_FILE
          - set -e # stop on any error below
          
          - git status # --porcelain
          - if [[ ! `git status --porcelain` ]]; then echo "No changes merged to Overleaf repo, doing empty sync commit to Overleaf" && git commit --allow-empty $TEX_FILE $BIB_FILE -m "Automatic commit from Bitbucket pipeline (empty commit for syncing)" && git push; else echo ".tex or other files merged to Overleaf repo, pushing to Overleaf" && git add . && git commit -a -m "Automatic commit from Bitbucket pipeline" && git push; fi
          
          - cp $TEX_FILE ../$PAPER_DIR/
          - cp $BIB_FILE ../$PAPER_DIR/
          - cd ../$PAPER_DIR/
          - rm -rf ../$OVERLEAF_ID/
          - rm -rf ../$TEX_FILE
          - rm -rf ../$BIB_FILE
          - git config --get remote.origin.url
          - git remote show origin
          - git status # for some reason this is needed to be launched before git diff-index to avoid fake commits of touched but unchanged file. Note that diff-index ignores untracked files unlike git status --porcelain above, that is we only accept .tex file changes from Overleaf
          - if git diff-index --quiet HEAD --; then echo "No change in .tex file"; else echo ".tex file changed, pushing to Bitbucket" && git commit $TEX_FILE $BIB_FILE -m "IMBH-57 Automatic commit from Bitbucket pipeline[skip ci]" && git push; fi
