#!/usr/bin/env php
<?php
/**
 * Look for WordPress readme in current directory or above and convert into markdown readme in same directory
 * @version 1.0.1
 * @author Weston Ruter <weston@xwp.co> (@westonruter)
 * @copyright Copyright (c) 2014, XWP <https://xwp.co/>
 * @license GPLv2+
 */

try {
	if ( php_sapi_name() !== 'cli' ) {
		throw new Exception( 'Only allowed in CLI mode.' );
	}

	$readme_txt_path = null;
	while ( true ) {
		foreach ( array( 'readme.txt', 'README.txt' ) as $readme_filename ) {
			if ( file_exists( $readme_filename ) ) {
				$readme_txt_path = realpath( $readme_filename );
				break;
			}
		}

		$old_cwd = getcwd();
		if ( ! empty( $readme_txt_path ) || ! chdir( '..' ) || getcwd() === $old_cwd ) {
			break;
		}
	}
	if ( empty( $readme_txt_path ) ) {
		throw new Exception( 'Failed to find a readme.txt or README.txt above the current working directory.' );
	}

	$readme_root = dirname( $readme_txt_path );
	$readme_md_path = preg_replace( '/txt$/', 'md', $readme_txt_path );

	require_once __DIR__ . '/class-wordpress-readme-parser.php';

	$readme = new WordPress_Readme_Parser( array( 'path' => $readme_txt_path ) );

	$md_args = array(
		'assets_dir' => 'assets',
	);
	$github_account_repo = null;
	$github_url_regex = '#.+github\.com[:/](?P<account_repo>[^/]+/[^/]+?)(?:\.git)?$#';
	$remote_urls = array();
	foreach ( explode( "\n", `git remote -v | grep fetch` ) as $remote_line ) {
		$remote_line = trim( $remote_line );
		if ( $remote_line ) {
			list( $name, $url ) = preg_split( '/\s+/', $remote_line );
			$remote_urls[ $name ] = $url;
		}
	}
	if ( ! empty( $remote_urls['origin'] ) && preg_match( $github_url_regex, $remote_urls['origin'], $matches ) ) {
		$github_account_repo = $matches['account_repo'];
	} else {
		foreach ( $remote_urls as $remote_name => $remote_url ) {
			if ( preg_match( $github_url_regex, $remote_url, $matches ) ) {
				$github_account_repo = $matches['account_repo'];
				break;
			}
		}
	}
	if ( $github_account_repo ) {
		$env_ini = $readme_root . '/.dev-lib';
		if ( ! file_exists( $env_ini ) ) {
			$env_ini = $readme_root . '/.ci-env.sh';
		}
		if ( file_exists( $env_ini ) ) {
			$env_vars = parse_ini_file( $env_ini );
			if ( isset( $env_vars['COVERALLS_BADGE'] ) ) {
				$coveralls_badge = $env_vars['COVERALLS_BADGE'];
			}
			if ( isset( $env_vars['GEMNASIUM_BADGE'] ) ) {
				$gemnasium_badge = $env_vars['GEMNASIUM_BADGE'];
			}
			if ( isset( $env_vars['GEMNASIUM_DEV_BADGE'] ) ) {
				$gemnasium_dev_badge = $env_vars['GEMNASIUM_DEV_BADGE'];
			}
			if ( isset( $env_vars['TRAVIS_CI_PRO_BADGE'] ) ) {
				$travis_ci_pro_badge = $env_vars['TRAVIS_CI_PRO_BADGE'];
			}
			if ( isset( $env_vars['ASSETS_DIR'] ) ) {
				$md_args['assets_dir'] = $env_vars['ASSETS_DIR'];
			}
		}
		if ( file_exists( $readme_root . '/.travis.yml' ) ) {
			if ( isset( $travis_ci_pro_badge ) ) {
				$md_args['travis_ci_pro_url'] = "https://magnum.travis-ci.com/$github_account_repo";
				$md_args['travis_ci_pro_badge_src'] = $md_args['travis_ci_pro_url'] . ".svg?token=$travis_ci_pro_badge&branch=master";
			}
			if ( ! isset( $md_args['travis_ci_pro_url'] ) ) {
				$md_args['travis_ci_url'] = "https://travis-ci.org/$github_account_repo";
			}
		}
		if ( file_exists( $readme_root . '/.coveralls.yml' ) ) {
			$md_args['coveralls_url'] = "https://coveralls.io/github/$github_account_repo";
			$md_args['coveralls_badge_src'] = "https://coveralls.io/repos/$github_account_repo/badge.svg?branch=master";
			if ( isset( $coveralls_badge ) ) {
				$md_args['coveralls_badge_src'] .= "&service=github&t=$coveralls_badge";
			}
		}
		if ( file_exists( $readme_root . '/Gruntfile.js' ) ) {
			$md_args['grunt_url'] = "gruntjs.com";
		}
		if ( file_exists( $readme_root . '/.david' ) ) {
			$md_args['david_url'] = "https://david-dm.org/$github_account_repo";
		}
		if ( file_exists( $readme_root . '/.david-dev' ) ) {
			$md_args['david_dev_url'] = "https://david-dm.org/$github_account_repo";
		}
		if ( isset( $gemnasium_badge ) ) {
			$md_args['gemnasium_url'] = "https://gemnasium.com/$github_account_repo";
			$md_args['gemnasium_badge_src'] = "https://gemnasium.com/$gemnasium_badge.svg";
		}
		if ( isset( $gemnasium_dev_badge ) ) {
			$md_args['gemnasium_dev_url'] = "https://gemnasium.com/$github_account_repo";
			$md_args['gemnasium_dev_badge_src'] = "https://gemnasium.com/$gemnasium_dev_badge.svg";
		}
		if ( file_exists( $readme_root . '/.gitter' ) ) {
			$md_args['gitter_url'] = "https://gitter.im/$github_account_repo";
		}
	}
	$markdown = $readme->to_markdown( $md_args );

	$is_written = file_put_contents( $readme_md_path, $markdown );
	if ( ! $is_written ) {
		throw new Exception( sprintf( 'Failed to write to %s', $readme_md_path ) );
	}
	fwrite( STDERR, 'Successfully converted WordPress README to Markdown' . PHP_EOL );
	fwrite( STDOUT, $readme_md_path . PHP_EOL );
}
catch( Exception $e ) {
	fwrite( STDERR, $e->getMessage() . PHP_EOL );
	exit( 1 );
}
