#!/bin/bash  

# define resource
resource=sandbox

# add the time_stamped_folder
cd /data/projects/glygen/downloads/$resource/

new_dir=$(date +%m_%d_%Y)
mkdir $new_dir && echo "Created new directory /data/projects/glygen/downloads/$resource/$new_dir"

# create accessionlist.csv and glycotree_annotated_glycans.csv
cd /software/glygen/
python3 sandbox-xref-download.py -d accessionlist

# move files to new folder
mv /tmp/accessionlist.csv /data/projects/glygen/downloads/$resource/$new_dir

# download allPaths.json to new folder
cd /data/projects/glygen/downloads/$resource/$new_dir

wget https://raw.githubusercontent.com/glygen-glycan-data/glycoTree/master/portal/api/paths/allPaths.json

sleep 5

# download glycotree_annotated_glycans.tsv to new folder
wget https://github.com/glygen-glycan-data/PyGly/archive/refs/tags/GlyGen-GlycanData-Export-Current.zip 

# unzip export folder
echo "Unzipping export..."
unzip GlyGen-GlycanData-Export-Current.zip

# move export folder to downloads, delete unused PyGly directory
mv PyGly-GlyGen-GlycanData-Export-Current/smw/glycandata/export/glycotree_annotated_glycans.tsv.gz /data/shared/glygen/downloads/$resource/$new_dir/ 

# remove any other files that are not needed for this download 
rm -rf PyGly-GlyGen-GlycanData-Export-Current/
rm -rf GlyGen-GlycanData-Export-Current.zip

#unzip glycotree_annotated_glycans.tsv.gz
gunzip glycotree_annotated_glycans.tsv.gz

# update new folder permissions
chmod -R 775 /data/shared/glygen/downloads/$resource/$new_dir/ 

# create symbolic link
cd /data/projects/glygen/downloads/$resource/
rm current
ln -s $new_dir current