#!/usr/bin/env Rscript
args <- commandArgs(TRUE)
if (length(args) < 1) {
stop("usage: book.R page_count [max_pages]")
}
page_count <- as.numeric(args[1])
max_pages_per_day <- if (length(args) == 2) args[2] else 60
weekly_session_count <- 9 # 7 sessions per day +2 for extra weekend readings
week_count <- 1
while (page_count / (weekly_session_count * week_count) > max_pages_per_day) {
week_count <- week_count + 1
}
pages_per_session <- round(page_count / (weekly_session_count * week_count))
woche <- c("MO", "DI", "MI", "DO", "FR", "SA", "SO")
weekly_session <- function(start) {
pages <- c(
seq(pages_per_session, (5 * pages_per_session),
by = pages_per_session),
6 * pages_per_session,
9 * pages_per_session)
return(pages + start)
}
cat(woche, "\n", sep="\t")
start <- 0
ideal_sessions <- numeric()
k <- 0
while (k < week_count) {
current_week <- (weekly_session(start))
ideal_sessions <- c(ideal_sessions, current_week)
cat(current_week, "\n", sep="\t")
start <- current_week[length(current_week)]
k <- k + 1
}
actual_sessions <- numeric()
for (s in ideal_sessions) {
cat(sprintf("Get to p.%s: ", s))
# Rscripting is non-interactive, so you gotta do this to get input
# https://thesquareplanet.com/blog/interactive-r-scripts/
input <- readLines(file("stdin"), n = 1)
# unlist comma input from here:
# https://stackoverflow.com/a/11007431
input <- as.numeric(unlist(strsplit(input, ",")))
if (length(input) == 2) {
input <- if (abs(s - input[1]) > abs(s - input[2])) input[2] else input[1]
cat(input, "\n")
}
actual_sessions <- c(actual_sessions, input)
}
cat("SCHEDULE:\n")
i <- 0
last <- 999
for (tag in woche) {
i <- i + 1
daily_schedule <- sprintf("%s: pp. ", tag)
j <- 0
while (j < week_count) {
index <- i + (7 * j)
current <- actual_sessions[index]
last <- if (index > 1) actual_sessions[index - 1] else 1
daily_schedule <- paste(daily_schedule, sprintf("%s-%s", last, current), sep="")
j <- j + 1
daily_schedule <- paste(daily_schedule, if (j != week_count) ", " else "\n", sep = "")
}
cat(daily_schedule)
}