SDK Script transfer-file.are

transfer-file.are
/* DESC: This scripts archives a remote file
 * Copyright (C) 2015 NetModule AG, Switzerland
 */
 
usr = "";
pwd = "";
url = "http://host/path";
source = "/tmp/download";
 
ret = nb_transfer_get(usr, pwd, url, source);
if (ret != 0 || !file_exists(source)) {
   printf("download of %s failed\n", url);
   exit(1);
}
 
size = file_size(source);
if (size <= 0) {
   printf("%s is empty or not present\n", source);
   exit(1);
}
 
modified = gmtime(file_mtime(source));
suffix = strftime("%Y%m%d-%H%M%S", modified);
dest = sprintf("%s-%s", source, suffix);
 
written = file_copy(source, dest);
if (written < 0) {
    printf("unable to copy %s to %s\n", source, dest);
    unlink(source);
    exit(1);
}
 
printf("New download in %s (%d bytes)\n", dest, written);
unlink(source);
 
exit(0);