Set up an F-Droid App Repo.

    More infos:
     * https://f-droid.org/wiki/page/Setup_an_FDroid_App_Repo
     * https://f-droid.org/wiki/page/Installing_the_Server_and_Repo_Tools

def fdroid():
    '''Set up an F-Droid App Repo.

    More infos:
     * https://f-droid.org/wiki/page/Setup_an_FDroid_App_Repo
     * https://f-droid.org/wiki/page/Installing_the_Server_and_Repo_Tools
    '''
    hostname = re.sub(r'^[^@]+@', '', env.host)  # without username if any
    sitename = query_input(
                   question='\nEnter site-name of Your F-Droid web service',
                   default=flo('fdroid.{hostname}'))
    username = env.user

    fabfile_data_dir = FABFILE_DATA_DIR

    print(magenta(' install fdroidserver'))
    res = run('dpkg --get-selections | '
              'grep -q "^fdroidserver[[:space:]]*install$" >/dev/null',
              warn_only=True)
    package_installed = res.return_code == 0
    question = 'package fdroidserver already installed, update? ' \
               '(needs some time)'
    if package_installed and not query_yes_no(question, default='no'):
        print('skip update')
    else:
        with hide('output'):
            sudo('yes "" | add-apt-repository  ppa:guardianproject/ppa')
            sudo('apt-get update')
            # why 'android-libhost-dev' (avoid "Failed to get apk information"
            # on 'fdroid update --create-metadata'):
            # https://f-droid.org/forums/topic/failed-to-get-apk-information-2/#post-15777
            install_packages(['fdroidserver', 'android-libhost-dev'])
            sudo('yes "" | add-apt-repository --remove  '
                 'ppa:guardianproject/ppa')
            sudo('apt-get update')

    site_dir = flo('/home/{username}/sites/{sitename}')
    apks_dir   = flo('{site_dir}/apks')
    fdroid_dir = flo('{site_dir}/fdroid')
    repo_dir   = flo('{site_dir}/fdroid/repo')

    print(magenta(' init f-droid repo'))
    question = ' '.join(['already initialized, initialize again?',
                         '(creates a new repo key)'])
    if exists(repo_dir) and not query_yes_no(question, default='no'):
        print('skip initialization')
    else:
        with warn_only():
            run(flo('rm -rf  {fdroid_dir}'))
        run(flo('mkdir -p  {repo_dir}'))
        run(flo('cd {fdroid_dir}  &&  fdroid init'))
        run(flo('cd {site_dir}  &&  tree'))

    print(magenta(' update apk files of the fdroid repo'))
    run(flo('mkdir -p  {apks_dir}'))
    run(flo('rm -rf {repo_dir}/*.apk'))
    run(flo("find {apks_dir} -type f | rename 's/ /_/g'"))
    run(flo("find {apks_dir} -type f | rename 's/[^[:ascii:]]//g'"))
    run(flo('chmod 644 {apks_dir}/*.apk'))
    run(flo('cp -v {apks_dir}/*.apk  {repo_dir}'), warn_only=True)
    run(flo('cd {fdroid_dir}  &&  fdroid update --create-metadata'))

    print(magenta(' setup nginx for F-Droid'))

    run(flo('echo -e "User-agent: *\\nDisallow: /" > {fdroid_dir}/robots.txt'))

    filename = 'fdroid_site_config.template'
    path = flo('{fabfile_data_dir}/files/etc/nginx/sites-available/{filename}')
    from_str = filled_out_template(path, username=username, sitename=sitename,
                                   hostname=hostname)
    with tempfile.NamedTemporaryFile(prefix=filename) as tmp_file:
        with open(tmp_file.name, 'w') as fp:
            fp.write(from_str)
        put(tmp_file.name, flo('/tmp/{filename}'))
    to = flo('/etc/nginx/sites-available/{sitename}')
    sudo(flo('mv /tmp/{filename} {to}'))
    sudo(flo('chown root.root {to}'))
    sudo(flo('chmod 644 {to}'))
    sudo(flo(' '.join([
            'ln -snf ../sites-available/{sitename}',
            '/etc/nginx/sites-enabled/{sitename}',
    ])))

    sudo('service nginx reload')