FediStatusPosterExtras/pick_ordered.sh

46 lines
1.6 KiB
Bash
Raw Normal View History

2024-03-09 18:29:43 -08:00
#! /usr/bin/env sh
# FediStatusPoster - Simple CLI tools and scripts to post to the fediverse
# Copyright (C) 2024 <@Anon@yandere.cc>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
[ ! $# -ge 2 ] &&\
echo "Invalid number of arguments..." &&\
echo "$(basename "$0") [master_file] [log_file] usage:" &&\
echo " master_file - path to the master file" &&\
echo " log_file - path to the log file" &&\
exit 1
# Get the runtime path of the bot
ABS_PATH="$(readlink -f "$0")"
RUN_DIR="$(dirname "$ABS_PATH")"
MASTER_FILE="$1"
LOG_FILE="$2"
INDEX=1
[ ! -f "$MASTER_FILE" ] && echo "Could not find: ${MASTER_FILE}" && exit 1
[ ! -s "$MASTER_FILE" ] && echo "File is empty: ${MASTER_FILE}" && exit 1
NLINES="$(wc -l "$MASTER_FILE" | cut -d ' ' -f1)"
if [ -s "$LOG_FILE" ];then
INDEX="$(grep ".*Index: [0-9]*$" "$LOG_FILE" | tail -n 1 | grep -o "[0-9]*$")"
2024-03-09 18:29:43 -08:00
INDEX="$(expr "$INDEX" '%' "$NLINES" '+' '1')"
fi
PICKED="$(sed "${INDEX}q;d" "$MASTER_FILE")"
echo "$(date +'%F %T') | Picked: ${PICKED} | Index: ${INDEX}" >> "$LOG_FILE"
echo "$PICKED"