bake/shake
2024-01-24 14:00:46 +00:00

72 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# Originally written by anon, modified
BLUE='\033[34m'
GREEN='\033[32m'
BOLD='\033[1m'
NORMAL='\033[0m'
MARKNAME="@BAKE"
MARK="${MARKNAME} "
MARKSTR="${BLUE}${MARKNAME}${NORMAL}"
enable -n echo
usage() {
IFSTR="${GREEN}<input_file>${NORMAL}"
echo -e "${BOLD}Usage: $0 <input_file>${NORMAL}" >&2
echo -e "\t$0 runs the value of ${MARKSTR}." >&2
echo -e "\tThe point of this script is ease to compialation of single source file (toy) programs." >&2
echo -e "\tThe value of ${MARKSTR} is read from ${IFSTR} in is whatever comes after '${MARK}' until the end of the line." >&2
echo -e "\tInside the value of ${MARKSTR} all mentions of special placeholders are replaced:" >&2
echo -e "\t\t${BLUE}\$@${NORMAL} - ${IFSTR}"
echo -e "\t\t${BLUE}\$*${NORMAL} - ${IFSTR} with the last extension cut off"
echo -e "\t\t${BLUE}\$+${NORMAL} - All remaining arguments"
}
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage
exit 0
fi
run=1
if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
if [[ $# -lt 2 ]]; then
usage
exit 1
fi
run=0
shift 1
fi
input_file=$1
shift 1
if [[ ! -f $input_file ]]; then
echo -e "Input file '$input_file' does not exist." >&2
exit 1
fi
line=$(grep "$MARK" "$input_file" | head -1)
line=${line//\$@/$input_file}
line=${line//\$\*/${input_file%.*}}
line=${line//\$+/$@}
if [[ -n $line ]]; then
command="${line#*${MARK}}"
command=$(echo $command | sed 's/[^\\]\?#.*//')
echo "Exec: $command"
if [[ $run -eq 1 ]]; then
echo "Output:"
sh -c "$command"
fi
else
echo -e "${MARKSTR} is not defined." >&2
fi