refactor: rewrite create_conf script

This commit is contained in:
Yorick Barbanneau 2024-10-13 11:30:08 +02:00
parent 3e497c997a
commit f113664131

View file

@ -1,7 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
error() { error() {
>&2 printf "\e[31mE\e[0m %s\n" "$1" printf -v message "\e[31mERROR:\e[0m %s\n" "$1"
message="${message} \e[1mStack trace\e[0m:\n"
for (( i=1; i<${#FUNCNAME[@]}; i++ ))
do
if [[ $i = $((${#FUNCNAME[@]} - 1)) ]]; then
message="${message} └"
else
message="${message} ├"
fi
message="${message}─ source:\e[3;34m${BASH_SOURCE[$i]}\e[0m"
message="${message} function:\e[3;34m${FUNCNAME[$i]}\e[0m"
message="${message} line:\e[3;34m${BASH_LINENO[$i-1]}\e[0m\n"
done
>&2 printf "%b\n" "${message}"
} }
die() { die() {
@ -21,6 +34,7 @@ process_args() {
shift shift
;; ;;
*) *)
conf="$*"
break break
esac esac
shift shift
@ -28,6 +42,10 @@ process_args() {
} }
mount_device() { mount_device() {
if [[ $# -lt 1 || $# -gt 2 ]]; then
error "mount_device need 1 or 2 arguments, $# provided : '$*'"
return 1
fi
local label="$1" local label="$1"
local unmount="${2:-0}" local unmount="${2:-0}"
local device local device
@ -60,6 +78,10 @@ mount_device() {
} }
get_file_hash() { get_file_hash() {
if [[ ! $# -eq 2 ]]; then
error "get_file_size need 2 arguments, $# provided : '$*'"
return 1
fi
local file local file
local file_size local file_size
@ -91,23 +113,31 @@ get_file_hash() {
} }
move_file(){ move_file(){
if [[ ! $# -eq 2 ]]; then
error "move_file need 2 arguments, $# provided : '$*'"
return 1
fi
local source="$1" local source="$1"
local destination="$2" local destination="$2"
printf "Move %s to %s:\n" "$source" "$destination" printf "Move %s to %s\n" "$source" "$destination"
if [[ ! -d "${destination%/*}" ]]; then if [[ ! -d "${destination%/*}" ]]; then
error "$destination directory does not exists" error "$destination directory does not exists"
return 1 return 1
fi fi
if ! mv "$source" "$destination"; then if ! mv "$source" "$destination"; then
error "Error moving $source file, file must not exists" error "Error moving $source file, file seems to not exists"
return 1 return 1
fi fi
return 0 return 0
} }
get_file_size() { get_file_size() {
if [[ ! $# -eq 1 ]]; then
error "get_file_size need 2 arguments, $# provided : '$*'"
return 1
fi
local file local file
file=$1 file=$1
if [ -f "$file" ] if [ -f "$file" ]
@ -119,30 +149,39 @@ get_file_size() {
} }
insert_media() { insert_media() {
if [[ ! $# -eq 2 ]]; then
error "insert_media need 2 arguments, $# provided : '$*'"
return 1
fi
local filename filesize filehash tag local filename filesize filehash tag
filename="$1" filename="$1"
tag="$2" tag="$2"
[ ! -f "$filename" ] && { if [[ ! -f "$filename" ]]; then
error "File $filename not found, can't insert media"; error "File $filename not found, can't insert media"
return; return 1
} fi
[ -z "$tag" ] && { if [[ -z "$tag" ]]; then
error "Tag not defined, can't insert media" error "Tag not defined, can't insert media (filename: $filename)"
} return 1
fi
filesize=$(get_file_size "$filename") filesize=$(get_file_size "$filename")
filehash=$(get_file_hash "$filename" "$filesize") filehash=$(get_file_hash "$filename" "$filesize")
sed -i -e "s#\[\[${tag}\]\]#${filename}#g" \ if ! sed -i \
-e "s#\[\[${tag}\]\]#${filename}#g" \
-e "s#\[\[${tag}_filesize\]\]#${filesize}#g" \ -e "s#\[\[${tag}_filesize\]\]#${filesize}#g" \
-e "s#\[\[${tag}_filehash\]\]#${filehash}#g" \ -e "s#\[\[${tag}_filehash\]\]#${filehash}#g" \
"${folder}/montage.kdenlive" || { "${folder}/montage.kdenlive"; then
error "sed command error on insert media!" error "sed command error on insert media!"
return 1
fi
return 0
} }
}
function main() {
process_args "$@"
base_dir="${HOME}/medias/videos/sambab" base_dir="${HOME}/medias/videos/sambab"
key_label=${key_label:-"SAMBAB"}
conf="$*"
key_label=${key_label:-SAMBAB}
recorder_label=${recorder_label:-"LS-100"} recorder_label=${recorder_label:-"LS-100"}
printf "Creation de la conférence de %s\n" "$conf" printf "Creation de la conférence de %s\n" "$conf"
@ -177,26 +216,26 @@ insert_media "$folder/titles/logo_amis.png" "logo"
## Get file from USB key ## Get file from USB key
if mount_device "$key_label"; then if mount_device "$key_label"; then
if move_file "/run/media/${USER}/${key_label}/RECORD/EXREC/"*.MP3 "${folder}/audio/audio.mp3"; then move_file "/run/media/${USER}/${key_label}/RECORD/EXREC/"*.MP3 "${folder}/audio/audio.mp3"
insert_media "${folder}/audio/audio.mp3" move_file "/run/media/${USER}/${key_label}/$(date --date='last Friday' +'%Y-%m-%d')"\ *.mkv "${folder}/rushes/obs.mkv"
fi
if move_file "/run/media/${USER}/${key_label}/$(date --date='last Friday' +'%Y-%m-%d')"\ *.mkv "${folder}/rushes/obs.mkv";
then
insert_media "${folder}/rushes/obs.mkv" video
fi
mount_device "$key_label" 1 mount_device "$key_label" 1
fi fi
## Get audio file from RECORDER ## Get audio file from RECORDER
if mount_device "$recorder_label"; then if mount_device "$recorder_label"; then
if move_file "/run/media/${USER}/${recorder_label}/RECORDER/FOLDER_A/"VOC_*.wav "${folder}/audio/recorder.wav"; then move_file "/run/media/${USER}/${recorder_label}/RECORDER/FOLDER_A/"VOC_*.wav "${folder}/audio/recorder.wav"
insert_media "${folder}/audio/recorder.wav"
fi
mount_device "${recorder_label}" 1
fi fi
move_file "$HOME/downloads/$(date --date='last Friday' +'%Y%m%d')"_*.mp4 "${folder}/rushes/presensation.mp4"
insert_media "${folder}/audio/audio.mp3" audio
insert_media "${folder}/rushes/obs.mkv" video
insert_media "${folder}/audio/recorder.wav" audio_recorder
insert_media "${folder}/rushes/presensation.mp4" video_intro
printf "Change root folder in montage.kdenlive to %s\n" "$folder" printf "Change root folder in montage.kdenlive to %s\n" "$folder"
# sed -i -e "s#\[\[root_folder\]\]#${folder}#g" "${folder}/montage.kdenlive" sed -i -e "s#\[\[root_folder\]\]#${folder}#g" "${folder}/montage.kdenlive"
}
main "$@"
exit 0 exit 0