2024-03-06 13:00:16 -05:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
2024-03-07 16:27:03 -05:00
|
|
|
// get resource path from var or default
|
|
|
|
string resourcePath = Environment.GetEnvironmentVariable("FORTUNE_CS_DIR");
|
|
|
|
if(resourcePath == "" || !Directory.Exists(resourcePath))
|
|
|
|
resourcePath = "/usr/share/fortune-cs/";
|
2024-03-07 16:16:13 -05:00
|
|
|
|
|
|
|
// pull file arg if 1 argument is provided
|
|
|
|
string file = null;
|
|
|
|
if(args.Length == 1) {
|
|
|
|
file = args[0];
|
|
|
|
// if the file doens't exist, see if it's in `resourcePath`
|
|
|
|
if(!File.Exists(file)) {
|
|
|
|
if(!file.EndsWith(".txt"))
|
|
|
|
file = file + ".txt";
|
|
|
|
file = resourcePath + file;
|
|
|
|
if(!File.Exists(file)) {
|
|
|
|
// don't try to read a file that doesn't exist
|
|
|
|
Console.WriteLine($"fortune-cs: no file '{file}' found.");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// run merge if more than 1 argument is provided
|
|
|
|
else if(args.Length > 0) {
|
2024-03-06 19:51:24 -05:00
|
|
|
Utilities.Merge(args);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-06 18:51:23 -05:00
|
|
|
// make sure fortune directory exists
|
2024-03-06 13:00:16 -05:00
|
|
|
if(!Directory.Exists(resourcePath)) {
|
|
|
|
Console.WriteLine("fortune-cs: directory '/usr/share/fortune-cs/' does not exist");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-03-06 18:51:23 -05:00
|
|
|
// pull file list
|
2024-03-06 13:00:16 -05:00
|
|
|
var files = Directory.GetFiles(resourcePath, "*.txt");
|
|
|
|
|
2024-03-07 16:16:13 -05:00
|
|
|
// choose file if no arg provided
|
|
|
|
if(file == null)
|
|
|
|
file = files[RandomNumberGenerator.GetInt32(files.Length)];
|
|
|
|
// read the file and choose a line
|
2024-03-06 13:00:16 -05:00
|
|
|
var lines = File.ReadAllLines(file);
|
|
|
|
var line = lines[RandomNumberGenerator.GetInt32(lines.Length)];
|
|
|
|
|
2024-03-07 16:16:13 -05:00
|
|
|
// process line breaks
|
2024-03-06 18:51:23 -05:00
|
|
|
line = line.Replace("\\n", "\n");
|
|
|
|
|
2024-03-07 16:16:13 -05:00
|
|
|
// write the fortune
|
2024-03-06 13:00:16 -05:00
|
|
|
Console.WriteLine(line);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|