diff --git a/data/templates/maths/project_build_script.sh b/data/templates/maths/project_build_script.sh index 4108641..e601121 100644 --- a/data/templates/maths/project_build_script.sh +++ b/data/templates/maths/project_build_script.sh @@ -1,241 +1,267 @@ #!/bin/bash # This part is specific to the template. # Entry point from bamc: build_project $exam # $exam: name of the exam to build # # Useful variable # --------------- # DIR_WORKSPACE [current workspace dir (absolute)] # DIR_PROJECTS [projects target directory] # DIR_EXAMS [exams'directory in workspace] e.g. $DIR_WORKSPACE/$DIR_EXAMS/$exam # DIR_PROJECTS [projects's directory in workspace] # DIR_EXAM_TEMPLATE [directory of the exam template in use] # FILE_STUDENTS # ... # see 'conf/bamc.conf' for full reference # # Useful functions # ---------------- # verbose "text" # debug "text" # get_lang $exam # ... # see 'lib/bash/*.sh' files for full reference function build_project() { local exam=$1 lang=$(get_lang $exam) assert_exam $exam verbose "Project '$exam' uses '$lang' language" if [ $? -eq 1 ]; then return 1; fi verbose "Building AMC project for '$exam' in workspace '$DIR_WORKSPACE'..." # Project structure build_amc_project $exam if [ $? -ne 0 ]; then error "Could not build AMC project :("; return 1; fi # Build LaTeX sections build_sections $exam $lang if [ $? -ne 0 ]; then error "Could not build project sections :("; return 1; fi + # Add media files + import_media_files $exam $lang + if [ $? -ne 0 ]; then error "Could not import media files :("; return 1; fi + + # Import LaTeX files import_tex_files $exam $lang if [ $? -ne 0 ]; then error "Could not import LaTeX files :("; return 1; fi # Import local LaTeX files override_tex_files $exam $lang if [ $? -ne 0 ]; then error "Could not override LaTeX files :("; return 1; fi # Customize LaTeX file customize_tex_files $exam $lang if [ $? -ne 0 ]; then error "Could not customize LaTeX files :("; return 1; fi # Import media files add_media_files $exam if [ $? -ne 0 ]; then error "Could not import media files :("; return 1; fi # Build student list file import_student_file $exam if [ $? -ne 0 ]; then error "Could not create student list :("; return 1; fi return 0 } function import_student_file() { verbose "Customizing student files" local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 local exam=$DIR_WORKSPACE/$DIR_EXAMS/$1 local lang=$2 local FILE=$exam/$FILE_STUDENTS check_file_exists $FILE if [ $? -ne 0 ]; then return 1; fi cp $FILE $project/$FILE_STUDENTS # Add extra students local nb IFS=$'\n' local id=$(tail -n 1 $project/$FILE_STUDENTS | cut -d ',' -f 1) local sample=$(head -n 1 $DIR_EXAM_TEMPLATE/csv/extra.csv) nb=0 while [ $nb -lt $EXTRA_STUDENTS ]; do ((nb++)) ((id++)) echo $sample |\ sed "s/#NB#/$nb/g" |\ sed "s/#ID#/$id/g" |\ sed "s/#SCIPER#/FAKE-$nb/g" |\ sed "s/#EMAIL#/$DEFAULT_EMAIL/g" |\ sed "s/#SEMESTER#/$SEMESTER/g" >> $project/$FILE_STUDENTS done return 0 } function add_media_files() { verbose "Importing media files" local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 cp $DIR_EXAM_TEMPLATE/media/* $project/media/ } function customize_tex_files() { verbose "Customizing LaTeX files" local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 local exam=$DIR_WORKSPACE/$DIR_EXAMS/$1 local lang=$2 local replace IFS=$'\n' local FILE=$exam/$FILE_EXAM check_file_exists $FILE if [ $? -ne 0 ]; then return 1; fi # professor.tex local prof_file=$project/professor.tex check_file_exists $prof_file if [ $? -ne 0 ]; then return 1; fi for replace in $(cat $FILE); do replace_in_file "#$(echo $replace | cut -d ':' -f 1)#" "$(echo $replace | cut -d ':' -f 2-)" $prof_file done # exam.tex replace_in_file "#LANG#" $(echo $lang | tr '[a-z]' '[A-Z]') $project/exam.tex # Add extra_packages.tex file if [ -r $project/common_packages.tex ]; then replace_in_file '% #COMMON_PACKAGES#' '\\input{./common_packages.tex}' $project/exam.tex fi # Add extra_packages.tex file if [ -r $project/common_commands.tex ]; then replace_in_file '% #COMMON_COMMANDS#' '\\input{./common_commands.tex}' $project/exam.tex fi # Add extra_section.tex file if [ -r $project/extra_section.tex ]; then replace_in_file '% #EXTRA_SECTION#' '\\input{./extra_section.tex}' $project/exam.tex fi # Add extra_packages.tex file if [ -r $project/extra_packages.tex ]; then replace_in_file '% #EXTRA_PACKAGES#' '\\input{./extra_packages.tex}' $project/exam.tex fi # Add extra_packages.tex file if [ -r $project/extra_commands.tex ]; then replace_in_file '% #EXTRA_COMMANDS#' '\\input{./extra_commands.tex}' $project/exam.tex fi return 0 } function override_tex_files() { verbose "Importing supplementary LaTeX files" local common=$DIR_WORKSPACE/$DIR_COMMON local exam=$DIR_WORKSPACE/$DIR_EXAMS/$1 local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 local lang=$2 local f # Common (without lang) - for f in $(find $common/ -maxdepth 1 -type f -name '*.tex'); do - verbose "- Common LaTeX file found: $f" - cp $f $project/ - done + if [ -d $common ]; then + for f in $(find $common -maxdepth 1 -type f -name '*.tex'); do + verbose "- Common LaTeX file found: $f" + cp $f $project/ + done + fi # Common (with lang) - for f in $(find $common/$lang/ -type f -name '*.tex'); do - verbose "- Common LaTeX file found ($lang): $f" - cp $f $project/ - done + if [ -d $common/$lang ]; then + for f in $(find $common/$lang -type f -name '*.tex'); do + verbose "- Common LaTeX file found ($lang): $f" + cp $f $project/ + done + fi # Extra for f in $(find $exam/ -type f -name '*.tex'); do verbose "- Extra LaTeX file found: $f" done find $exam/ -type f -name '*.tex' -exec cp {} $project/ \; return 0 } function import_tex_files() { verbose "Importing LaTeX files" local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 local lang=$2 cp $DIR_EXAM_TEMPLATE/base/* $project/ cp $DIR_EXAM_TEMPLATE/$lang/* $project/ return 0 } function build_amc_project() { local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 verbose "Creating AMC project structure" mkdir -p $project/cr/corrections/jpg mkdir -p $project/cr/corrections/pdf mkdir -p $project/cr/diagnostic mkdir -p $project/cr/zooms mkdir -p $project/data mkdir -p $project/exports mkdir -p $project/scans mkdir -p $project/copies mkdir -p $project/media cp $DIR_EXAM_TEMPLATE/config/$FILE_AMC_OPTIONS $project/$FILE_AMC_OPTIONS return $? } +function import_media_files() { + local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 + local exam=$DIR_WORKSPACE/$DIR_EXAMS/$1 + local lang=$2 + local questions_dir=$DIR_WORKSPACE/$DIR_QUESTIONS/$lang + local media_file + + verbose "Importing media files" + + for media_file in $(find $questions_dir -type f -name -not '*\.tex'); do + verbose "Importing file '$media_file'" + cp $media_file $DIR_WORKSPACE/$DIR_PROJECTS/$1/ + done + + return 0 +} + function build_sections() { local project=$DIR_WORKSPACE/$DIR_PROJECTS/$1 local exam=$DIR_WORKSPACE/$DIR_EXAMS/$1 local lang=$2 local question_file question section_file section local IFS=$'\n' FILE=$exam/$FILE_SECTIONS verbose "Importing questions" check_file_exists $FILE if [ $? -ne 0 ]; then return 1; fi local section_num=0 rm -f $project/sections.tex $project/random-sections.tex for section in $(cat $FILE); do ((section_num++)) section_file=$project/section_${section_num}.tex rm -f $section_file for question in $(echo $section | tr ',' '\n'); do verbose "Adding $question to section $section_file..."; question_file=$DIR_WORKSPACE/$DIR_QUESTIONS/$lang/$question check_file_exists $question_file if [ $? -ne 0 ]; then return 1; fi echo "%% From $lang/$question =======================================" >> $section_file echo '\element{section'$section_num'}{' >> $section_file cat $question_file >> $section_file echo '}' >> $section_file done echo "\input{./section_${section_num}.tex}" >> $project/sections.tex echo "\input{./header_${section_num}.tex} \melangegroupe{section${section_num}} \restituegroupe{section${section_num}} " >> $project/random-sections.tex done return 0 } # EOF