#!/bin/bash

# check if 1st argument spceified; if it is, then set that to SAVE_TO
if [ -n "$1" ]; then
    SAVE_TO="$1"
else
    SAVE_TO=`pwd`
fi

echo "Saving to $SAVE_TO"

psql jam -c "COPY( select id, original_artist, name , original_artist_slug, name_slug, plan_code, slug, allow_free, ('https://www.jamkazam.com/backing-tracks/' || original_artist_slug || '/' || name_slug) as \"URL\", (select name from jam_track_licensors l where l.id = licensor_id) as \"Licensor\", vendor_id  as \"Vendor ID\" FROM jam_tracks) TO '$SAVE_TO/jam_tracks_for_jam_ui.$USER.csv' with CSV HEADER;"

//https://jamkazam-public.s3.amazonaws.com

# dump all artists
psql jam -c "COPY( select original_artist, original_artist_slug, ('https://www.jamkazam.com/backing-tracks/' || original_artist_slug ) as \"URL\" FROM jam_tracks group by original_artist, original_artist_slug) TO '$SAVE_TO/jam_tracks_for_jam_ui_artists.$USER.csv' with CSV HEADER;"

psql jam -c "COPY( select id, part, instrument_id, (select description from instruments where id = instrument_id) as instrument_description, track_type, position, preview_mp3_url, preview_url as preview_ogg_url, preview_acc_url from jam_track_tracks) TO '$SAVE_TO/jam_track_tracks_for_jam_ui.$USER.csv' with CSV HEADER;"

echo "Moving personal files to $SAVE_TO"
sudo mv $SAVE_TO/jam_tracks_for_jam_ui.$USER.csv .
sudo mv $SAVE_TO/jam_tracks_for_jam_ui_artists.$USER.csv .
sudo mv $SAVE_TO/jam_track_tracks_for_jam_ui.$USER.csv .