#!/usr/bin/env bash # HEADER #======================================================================= # # trombinoscope -- make a simple trombinoscope from names and photos. # # SYNOPSIS # trombinoscope -n -p -o # # DESCRIPTION # Shell script to make a trombinoscope from a given list of names # and a list of photos with names in the same ordre. It first # establish a bijection between names and photos and then write a # LaTeX file. # # POSITIONAL ARGUMENT # -n|names a csv file of two columns (first;last) # -p|photos directory name for photos # # OPTIONS # -h|--help display this help and exit # -c|class class name # -y|year school year # -o|output LaTeX outfile name # -i|img directory of photos with names # # EXAMPLES # $ trombinoscope -n PCSI.csv -p photos_init -c PCSI -y 2023-2024 # $ trombinoscope -n PCSI.csv -p photos_init -o trombinoscope.tex # # DEPENDENCIES # - imagemagick # - GNU coreutils # # Copyright (C) 2023 Nicolas Mesnier # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 or # above as published by the Free Software Foundation. # # 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 . # #======================================================================= # END_OF_HEADER #----------------------------------------------------------------------- Help(){ # Read header to display help and version BOH=$(head -200 ${0} | grep -n "^# HEADER" | cut -f1 -d:) EOH=$(head -200 ${0} | grep -n "^# END_OF_HEADER" | cut -f1 -d:) head -$(($EOH-1)) ${0} | tail -$(($EOH-$BOH-1)) \ | grep -e "^#$" -e "^# " | sed -e "s/^#=*//g" } #----------------------------------------------------------------------- # *** get options *** #----------------------------------------------------------------------- NAMES="" PHOTOS="" CLASS="" YEAR="" TEXFILE="" IMG="img" while [[ ${1} ]]; do case "${1}" in -h|--help) Help exit 0 ;; # *** run function -n|names) if [ -f ${2} ];then NAMES=${2} else echo "File \"${2}\" does not exists." >&2 exit 1 fi shift ;; -p|photos) if [ -d ${2} ];then PHOTOS=${2} else echo "Directory \"${2}\" does not exists." >&2 exit 1 fi shift ;; -o|output) if [ -f ${2} ]; then read -p " File \"${2}\" allready exists. Replace? y/[n] " rep case ${rep} in [Yy]* ) TEXFILE=$(basename ${2} .tex)".tex" ;; * ) exit 1;; esac else TEXFILE=$(basename ${2} .tex)".tex" fi shift ;; -c|class) CLASS=${2} shift ;; -y|year) YEAR=${2} shift ;; -i|img) IMG=${2} shift ;; esac if ! shift; then echo "Missing parameter argument." >&2 exit 1 fi done if [ -z ${NAMES} ];then echo "You must give a CSV file for names." >&2 exit 1 fi if [ -z ${PHOTOS} ];then echo "You must give a directory for photos." >&2 exit 1 fi #----------------------------------------------------------------------- # *** check if their are the same numbers of names and photos #----------------------------------------------------------------------- item(){ echo "$(wc -l ${1} | cut -d ' ' -f1)" } NB_NAMES="$(item ${NAMES})" NB_PHOTOS="$(item <(ls -1 ${PHOTOS}))" difference(){ DIFF=$(($1-$2)) if [ ${DIFF} == 1 ];then echo "${DIFF} ${3} is missing." >&2 else echo "${DIFF} ${3}s are missing." >&2 fi } if [ ${NB_NAMES} != ${NB_PHOTOS} ];then echo "Their must be the same number of names and photos." >&2 if [ "${NB_NAMES}" -gt "${NB_PHOTOS}" ];then difference ${NB_NAMES} ${NB_PHOTOS} "photo" else difference ${NB_PHOTOS} ${NB_NAMES} "name" fi exit 1 fi #----------------------------------------------------------------------- # *** Set the output file name if none is given # but the CLASS and YEAR are. #----------------------------------------------------------------------- if [ -z ${TEXFILE} ];then if [ -z ${CLASS} ] && [ -z ${YEAR} ];then echo "No output file name given." >&2 exit 1 else TEXFILE="trombinoscope_"${CLASS}"_"${YEAR}".tex" echo " Output file fixed to '${TEXFILE}'." fi fi #----------------------------------------------------------------------- # *** check before writing in ${IMG} #----------------------------------------------------------------------- if [ -d ${IMG} ];then read -p "Directory \"${IMG}\" allready exists. Purge? y/[n] " rep case ${rep} in [Yy]* ) find ${IMG} -type f -delete ;; * ) exit 1 ;; esac else mkdir -p ${IMG} fi #----------------------------------------------------------------------- # *** write LaTeX file #----------------------------------------------------------------------- # header cat > ${TEXFILE}<> ${TEXFILE} echo "\includegraphics[angle=90,width=20mm]{"${NEW_PHOTO}"}" >> ${TEXFILE} echo "\newline\vskip-1em{\small ${NOM}\\\\[-2pt] ${PRENOM}}}" >> ${TEXFILE} done < <(paste -d";" ${NAMES} <(ls -1 ${PHOTOS}/)) # footer cat >> ${TEXFILE}<