StudyManagerV1/studyManager.sh

310 lines
6.6 KiB
Bash
Executable File

#!/bin/bash
APPNAME=$(basename $0 | sed "s/\.sh$//")
CONFIGFILE="${XDG_CONFIG_HOME}/studyManager/config"
sm_log_info() {
echo "$APPNAME: $1" > /dev/tty
}
sm_log_error() {
echo "$APPNAME: ERROR: $1" > /dev/tty
}
sm_parse_yaml() {
#
# Parse yaml provided in $1 and put into array $2
#
# Not sufficient arg provided
if [[ $# -lt 2 ]]
then
exit 1
fi
# File does not exist
if [[ ! -f "$1" ]]
then
exit 2
fi
local -n arr="$2"
while read -r key val
do
str="$key=$val"
# Remove leading/trailing quote
# val="${val%\'}"
# val="${val#\'}"
arr+=("$key=$val")
done < <(sed '/:/!d;/^ *#/d;s/:/ /;' < "$1")
sm_log_info "Successfully parsed $1 into $2"
}
sm_load_yaml_vars() {
#
# Define all variables defined in $1
#
if [[ $# -eq 0 ]]
then
sm_log_error "Insufficient number of arguments"
exit 1
fi
# Todo: error code check is weird
sm_parse_yaml "$1" config
if [[ $? -eq 2 ]]
then
sm_log_error "Config $1 does not exist, please create it"
exit 1
fi
# Define variables
for conf in "${config[@]}"
do
eval "declare -g ${conf}"
done
sm_log_info "Successfully load vars of $1"
}
sm_load_yaml_vars $CONFIGFILE
COURSES=( $( ls $ROOT_COURSE_DIR ) )
sm_usage() {
local txt=(
"Utility $APPNAME for managing your studies."
"Usage: $APPNAME [options]"
""
"Options:"
" --help, -h Print help."
" --set-current, -c Select new course."
" --list, -l List course content."
)
printf "%s\n" "${txt[@]}"
}
sm_bad_usage() {
local message="$1"
local txt=(
"For an overview of the command, execute:"
"$APPNAME --help"
)
[[ $message ]] && printf "$message\n"
printf "%s\n" "${txt[@]}"
}
_sm_gen_courses() {
local options=""
for e in "${COURSES[@]}"
do
options="${options}${e}\n"
done
echo $options
}
sm_link_course() {
#
# Propt user to select a course and create symlink for the choosen one
#
local selection="$(echo -e $(_sm_gen_courses) | displayer rofi -lines ${#COURSES[@]} -dmenu -p "Current Course" -a 0 -no-custom)"
echo $selection
if [[ -z "$selection" ]]
then
exit 1
fi
src="${ROOT_COURSE_DIR}/${selection}"
ln --force --symbolic --no-dereference $src $CURRENT_COURSE_DIR
sm_log_info "Link $src to $CURRENT_COURSE_DIR"
}
_sm_gen_contents() {
local info_file="$CURRENT_COURSE_DIR/$RELATIVE_INFO_FILE"
local options=""
while read -r key val
do
str="$key=$val"
options="${options}${val}\n"
done < <(sed '/:/!d;/^ *#/d;s/:/ /;' < "$info_file")
echo $options
}
sm_list_contents() {
#
# List course content and copy selected to clipboard
#
selection="$(echo -e $(_sm_gen_contents) | displayer rofi -lines 5 -dmenu -p 'Info Content' -a 0 -no-custom)"
selection="${selection%\'}"
selection="${selection#\'}"
echo -n $selection | xclip -selection clipboard
}
sm_timetable() {
#
# Use timetable to create task, events etc.
#
sm_tt_usage() {
local txt=(
"Utility $APPNAME for managing your studies."
"Usage: $APPNAME import [options]"
""
"Options:"
" --help, -h Print help."
)
printf "%s\n" "${txt[@]}"
}
sm_tt_bad_usage() {
local message="$1"
local txt=(
"For an overview of the command, execute:"
"$APPNAME timetable --help"
)
[[ $message ]] && printf "$message\n"
printf "%s\n" "${txt[@]}"
}
sm_tt_khal() {
local info_file="$CURRENT_COURSE_DIR/$RELATIVE_INFO_FILE"
local timetable_file="$CURRENT_COURSE_DIR/$RELATIVE_TIMETABLE_FILE"
local start_date="2021-09-20"
local end_date="2021-12-25"
sm_load_yaml_vars "${info_file}"
sm_parse_yaml "${timetable_file}" timetable
for e in "${timetable[@]}"
do
val=${e#*=}
key=${e%%=*}
val="${val%\'}"
val="${val#\'}"
val=($val)
local event_date=""
case "${val[0]}" in
"Mon")
event_date=$(date --date="${start_date} +0 days" +%d/%m/%Y)
;;
"Tue")
event_date=$(date --date="${start_date} +1 days" +%d/%m/%Y)
;;
"Wed")
event_date=$(date --date="${start_date} +2 days" +%d/%m/%Y)
;;
"Thu")
event_date=$(date --date="${start_date} +3 days" +%d/%m/%Y)
;;
"Fri")
event_date=$(date --date="${start_date} +4 days" +%d/%m/%Y)
;;
"Thu")
event_date=$(date --date="${start_date} +5 days" +%d/%m/%Y)
;;
esac
command="khal new -a Timetable -r weekly -u $(date --date=${end_date} +%d/%m/%Y) -l ${val[3]//\\0040/' '} $event_date ${val[1]} ${val[2]} ${title} ${key}"
echo "$command"
read -p "Run above command [yn]? " -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
eval "${command}"
fi
done
exit 0
}
sm_tt_task() {
exit 0
}
while (( $# ))
do
case $1 in
-h|--help)
shift
sm_tt_usage
exit 0
;;
--khal|-k)
shift
sm_tt_khal
exit 0
;;
--task|-t)
shift
sm_tt_task
exit 0
;;
*)
sm_tt_bad_usage "Option/command not recognized."
;;
esac
done
sm_tt_bad_usage
exit 1
}
while (( $# ))
do
case $1 in
-h|--help)
shift
sm_usage
exit 0
;;
-c|--set-current)
sm_link_course
exit 0
shift
;;
-l|--list)
sm_list_contents
exit 0
shift
;;
timetable)
command=$1
shift
sm_"$command" "$@"
exit 0
;;
*)
sm_bad_usage "Option/command not recognized."
exit 1
;;
esac
shift
done
sm_bad_usage
exit 1