#!/usr/bin/fish

echo 'Deploy website through FTP'

function require
        set var $argv[1]
        if not set -q $var; or test -z "$var"
                echo "Please provide a value for $var"
                echo "Did you run the command source against your .env.local?"
                exit
        end
end
require FTP_HOST
require FTP_USER
require FTP_DEST
require DIST_DIR

set make_dirs (find $DIST_DIR -type d -printf "mkdir $FTP_DEST/%P\n")
set files (find $DIST_DIR -type f)
echo 'Directories to be created :'
echo $make_dirs
echo 'Files to be sent :'
printf %s\n $files

# ftp operation
echo "FTP to $FTP_HOST"

set ftp_sync "put -R $DIST_DIR/* $FTP_DEST"

echo "Format whole command"
printf %s\n $make_dirs $ftp_sync 'bye'
echo "Forward whole command to FTP"
printf %s\n $make_dirs $ftp_sync 'bye' | sftp -P $FTP_PORT $FTP_USER@$FTP_HOST
