79 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eEuo pipefail
. "${SCRIPT_DIR}/read.bash"
_update_course() {
local dept_code="${COURSE_DATA["dept_code"]^^}"
local dept_name="${COURSE_DATA["dept_name"]}"
local course_num="${COURSE_DATA["course_num"]}"
local course_name="${COURSE_DATA["course_name"]}"
local course_sched="${COURSE_DATA["course_sched"]}"
local course_start="${COURSE_DATA["course_start"]}"
local course_end="${COURSE_DATA["course_end"]}"
local course_hours="${COURSE_DATA["course_hours"]}"
local course_size="${COURSE_DATA["course_size"]}"
cat <<-__EOS__ >"${DATA_DIR}/${dept_code}${course_num}.crs"
${dept_code} ${dept_name}
${course_name}
${course_sched} ${course_start} ${course_end} ${course_hours} ${course_size}
__EOS__
}
update_course() {
local create="${1:-}"
local dept_code
local dept_name
local course_num
local course_name
local course_sched
local course_start
local course_end
local course_hours
local course_size
read -r -p "Department code: " dept_code
read -r -p "Department name: " dept_name
read -r -p "Course number: " course_num
read -r -p "Course name: " course_name
read -r -p "Scheduled days: " course_sched
read -r -p "Course start: " course_start
read -r -p "Course end: " course_end
read -r -p "Credit hours: " course_hours
read -r -p "Enrolled students: " course_size
if [[ -z "$dept_code" ]] || [[ -z "$course_num" ]]; then
err "must provide a department code and course number"
return
fi
local course_file="${DATA_DIR}/${dept_code^^}${course_num}.crs"
if [[ "$create" ]]; then
if [[ -f "$course_file" ]]; then
err "course already exists"
return 1
fi
else
_read_course "${dept_code^^}" "$course_num" || return
fi
COURSE_DATA["dept_code"]="${dept_code:-${COURSE_DATA["dept_code"]}}"
COURSE_DATA["dept_name"]="${dept_name:-${COURSE_DATA["dept_name"]}}"
COURSE_DATA["course_num"]="${course_num:-${COURSE_DATA["course_num"]}}"
COURSE_DATA["course_name"]="${course_name:-${COURSE_DATA["course_name"]}}"
COURSE_DATA["course_sched"]="${course_sched:-${COURSE_DATA["course_sched"]}}"
COURSE_DATA["course_start"]="${course_start:-${COURSE_DATA["course_start"]}}"
COURSE_DATA["course_end"]="${course_end:-${COURSE_DATA["course_end"]}}"
COURSE_DATA["course_hours"]="${course_hours:-${COURSE_DATA["course_hours"]}}"
COURSE_DATA["course_size"]="${course_size:-${COURSE_DATA["course_size"]}}"
_update_course
if [[ "$create" ]]; then
log "CREATED" "${COURSE_DATA["dept_code"]} ${COURSE_DATA["course_num"]} ${COURSE_DATA["course_name"]}"
else
log "UPDATED" "${COURSE_DATA["dept_code"]} ${COURSE_DATA["course_num"]} ${COURSE_DATA["course_name"]}"
fi
}