2023-10-15 20:56:16 -04:00
|
|
|
#!/bin/bash
|
2023-10-16 15:17:21 -04:00
|
|
|
|
2023-11-12 23:35:46 -05:00
|
|
|
# Originally written by anon, modified
|
2023-10-16 15:17:21 -04:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
BLUE='\033[34m'
|
|
|
|
GREEN='\033[32m'
|
|
|
|
BOLD='\033[1m'
|
|
|
|
NORMAL='\033[0m'
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2023-11-12 23:35:46 -05:00
|
|
|
MARKNAME="@BAKE"
|
2023-10-16 03:53:44 -04:00
|
|
|
MARK="${MARKNAME} "
|
|
|
|
MARKSTR="${BLUE}${MARKNAME}${NORMAL}"
|
2023-10-15 20:56:16 -04:00
|
|
|
|
|
|
|
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
|
2023-10-16 03:53:44 -04:00
|
|
|
echo -e "\tThe point of this script is ease to compialation of single source file (toy) programs." >&2
|
2023-10-15 20:56:16 -04:00
|
|
|
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"
|
2023-10-16 03:53:44 -04:00
|
|
|
echo -e "\t\t${BLUE}\$+${NORMAL} - All remaining arguments"
|
2023-10-15 20:56:16 -04:00
|
|
|
}
|
2023-10-16 03:53:44 -04:00
|
|
|
|
|
|
|
if [[ $# -lt 1 ]]; then
|
2023-10-15 20:56:16 -04:00
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
fi
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2023-10-16 18:00:21 -04:00
|
|
|
run=1
|
|
|
|
if [[ $1 == "-n" ]] || [[ $1 == "--dry-run" ]]; then
|
|
|
|
if [[ $# -lt 2 ]]; then
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
run=0
|
|
|
|
shift 1
|
|
|
|
fi
|
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
input_file=$1
|
2023-10-16 03:53:44 -04:00
|
|
|
shift 1
|
2023-10-15 20:56:16 -04:00
|
|
|
|
|
|
|
if [[ ! -f $input_file ]]; then
|
|
|
|
echo -e "Input file '$input_file' does not exist." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-10-16 03:53:44 -04:00
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
line=$(grep "$MARK" "$input_file" | head -1)
|
|
|
|
line=${line//\$@/$input_file}
|
|
|
|
line=${line//\$\*/${input_file%.*}}
|
2023-10-16 03:53:44 -04:00
|
|
|
line=${line//\$+/$@}
|
|
|
|
|
2023-10-15 20:56:16 -04:00
|
|
|
if [[ -n $line ]]; then
|
2023-10-16 03:53:44 -04:00
|
|
|
command="${line#*${MARK}}"
|
2024-01-24 09:00:46 -05:00
|
|
|
command=$(echo $command | sed 's/[^\\]\?#.*//')
|
2023-10-16 03:53:44 -04:00
|
|
|
echo "Exec: $command"
|
2023-10-16 18:00:21 -04:00
|
|
|
if [[ $run -eq 1 ]]; then
|
|
|
|
echo "Output:"
|
2024-01-24 09:00:46 -05:00
|
|
|
sh -c "$command"
|
2023-10-16 18:00:21 -04:00
|
|
|
fi
|
2023-10-15 20:56:16 -04:00
|
|
|
else
|
|
|
|
echo -e "${MARKSTR} is not defined." >&2
|
|
|
|
fi
|