#!/usr/bin/env python

import os, glob, subprocess

def ofile(url):
    ''' Convert the KSAT-URL into filename '''
    return os.path.join(idir, url.split('=')[-1])

def rename(idir):
    ''' Rename awkward name composed of HTTP request parameters by wget -i download'''
    # list of files downloaded with wget
    ifiles = glob.glob(os.path.join(idir, 'proxy*'))
    for ifile in ifiles:
        print 'Rename ', ifile
        print 'Into ', ofile(ifile)
        if os.path.exists(ifile):
            os.rename(ifile, ofile(ifile))

def download(idir):
    ''' Download new files from list of urls in ksatradarsat2.txt '''
    # file with urls in /Data/sat/downloads/Radarsat2
    urlsFile = 'ksatradarsat2.txt'

    # open text file and read all URLs
    urls = open(os.path.join(idir, urlsFile)).readlines()
    # remove CR from the end of each line
    urls = [url.replace('\n', '') for url in urls]

    # download files
    for url in urls:
        print url
        # skip downloading if the file already exists
        if os.path.exists(ofile(url)):
            continue
        print 'Download ', ofile(url)
        # create the wget download command
        cmdStr = 'wget --user=nersc --password=xp3u9AAn -nc -nd -w 2 --random-wait %s -O %s' % (url, ofile(url))
        print cmdStr
        # launch wget
        subprocess.call(cmdStr.split(' '))

# download directory
idir = '/Data/sat/downloads/Radarsat2'
# if any, rename files downloaded previously by wget (with option -i ksatradarsat2.txt)
rename(idir)
# download new files from the list of uslrs in /Data/sat/downloads/Radarsat2/ksatradarsat2.txt
download(idir)

subprocess.call(['chmod', '755', idir, '-R'])
