#!/usr/bin/env php * Martin Tarenskeen */ function do_curl($url, $opts) { $curl = curl_init($url); curl_setopt_array($curl, $opts); $result = curl_exec($curl); curl_close($curl); return $result; } $VERSION = "lilybin v0.2 (2017-03-27)" . PHP_EOL; $opts = getopt('dhuv', $argv); $sep = array_search('--', $argv); $files = array_slice($argv, $sep ? ++$sep : count($opts) + 1); $download = isset($opts['d']); $unstable = isset($opts['u']); $help = "Usage:" . PHP_EOL . "\tlilybin [-u] INFILE.ly OUTFILE.pdf" . PHP_EOL . "\tlilybin [-u] INFILE.ly OUTFILE.mid" . PHP_EOL . "\t\t=> Download and directly save result" . PHP_EOL . "\tlilybin [-u] INFILE.ly" . PHP_EOL . "\t\t=> Only print ID for download" . PHP_EOL . "\tlilybin -d (ID.pdf|ID.midi)" . PHP_EOL . "\t\t=> Download result from remote server" . PHP_EOL . "Other options:" . PHP_EOL . "\t-h print this Help message" . PHP_EOL . "\t-u use Unstable lilypond-devel version" . PHP_EOL; if (count($files) === 0 || count($files) > 2) { echo $VERSION; die($help); } if (count($files) == 2) { $all = True; $ext = "." . pathinfo($files[1])['extension']; if ($ext == ".mid") { $ext = ".midi"; } } else { $all = False; } if ($unstable) { $stable = 'unstable'; } else { $stable = 'stable'; } if ($all || !$download) { $url = 'https://7icpm9qr6a.execute-api.us-west-2.amazonaws.com/prod/prepare_preview/' . $stable; $result = json_decode(do_curl($url, [ CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POST => 1, CURLOPT_POSTFIELDS => json_encode(['code' => file_get_contents($files[0])]), CURLOPT_RETURNTRANSFER => true, ])); if (!$result->id) { echo "ERROR:", PHP_EOL, $result->stderr; exit(1); } if ($all) { $files[0] = $result->id . $ext; } else { echo $argv[0], ' -d ', $result->id, ".(pdf|midi)", PHP_EOL; exit; } } if ($all || $download) { $url = 'https://s3-us-west-2.amazonaws.com/lilybin-scores/' . $files[0]; $file = fopen(@$files[1] ?: $files[0], 'w'); do_curl($url, [CURLOPT_FILE => $file]); fclose($file); }