From: Piotr Dymacz Date: Fri, 23 Sep 2016 20:20:47 +0000 (+0200) Subject: Rework makefsdatac shell script X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=9e7b935e15db7884384466c201bf386348dbf35a;p=oweals%2Fu-boot_mod.git Rework makefsdatac shell script --- diff --git a/u-boot/httpd/vendors/makefsdatac b/u-boot/httpd/vendors/makefsdatac index eac4798..adb1878 100755 --- a/u-boot/httpd/vendors/makefsdatac +++ b/u-boot/httpd/vendors/makefsdatac @@ -1,5 +1,7 @@ #!/bin/bash +# Copyright (C) 2016 Piotr Dymacz + # This script generates "fsdata.c" file for uIP 0.9 stack. # It takes one argument - name of vendor directory, # which should contains all www files, at least: @@ -15,132 +17,177 @@ # - uboot.html () # # HTML and CSS files are compressed before placing them -# inside "fsdata.c". +# inside "fsdata.c" if JAVA is installed. # # You SHOULDN'T embed addresses of any external # files in "flashing.html" file, because web server, # after receive POST data, returns this page and stops. -# Vendor specific directory (default: "general") -vendor_dir=${1:-general} +# ================ +# Global variables +# ================ + +# Vendor specific directory +# (default: "general") +VENDOR_DIR=${1:-general} # Temporary files -files_content_tmp="vendors/.files_content" -files_list_tmp="vendors/.files_list" +FILES_CONTENT_TMP="vendors/.files_content" + FILES_LIST_TMP="vendors/.files_list" -# YUI Compressor path (should be in the same dir) -yui_compressor=`ls -t vendors/*.jar 2> /dev/null | tail --lines=1` +# YUI Compressor path +# (should be in the same dir) + YUI_PATH=$(ls -t vendors/*.jar 2> /dev/null | tail --lines=1) +HAVE_JAVA=0 # Previous fsdata_file var name -prev_fsdata_struct="NULL" +PREV_FSDATA_STRUCT="NULL" # Files counter -files_counter=0 +FS_CNT=0 -# Change ASCII to bytes, comma separated (e.g. "0x01, 0x02, 0x03...") +# Change ASCII to bytes, comma separated +# (e.g. "0x01, 0x02, 0x03...") function ascii_to_bytes() { - echo -ne $1 | od -A n -t x1 | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g' + echo -ne "$1" |\ + od -A n -t x1 |\ + tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g' >> "$FILES_CONTENT_TMP" } # $1 -> file path function print_data_array() { - local _file_ext="${1##*.}" - local _file_name="${1##*/}" - local _file_name_no_ext="${_file_name%\.*}" - local _file_content="" + local f_ext="${1##*.}" + local f_name="${1##*/}" + local f_name_no_ext="${f_name%\.*}" + local f_content="" # Open variable declaration - `echo -ne "static const char data_"$_file_name_no_ext"_"$_file_ext"[] = {\n" >> "$files_content_tmp"` - `echo -ne "/* HTTP Header */\n" >> "$files_content_tmp"` + echo -ne "static const char data_${f_name_no_ext}_${f_ext}[] = {\n" \ + >> "$FILES_CONTENT_TMP" + echo -ne "/* HTTP Header */\n" >> "$FILES_CONTENT_TMP" # HTTP header (200 OK or 404 Not Found) - if [ "$_file_name_no_ext" == "404" ]; then - `ascii_to_bytes "HTTP/1.0 404 File not found\r\n" >> "$files_content_tmp"` + if [ "$f_name_no_ext" = "404" ]; then + ascii_to_bytes "HTTP/1.0 404 File not found\r\n" else - `ascii_to_bytes "HTTP/1.0 200 OK\r\n" >> "$files_content_tmp"` + ascii_to_bytes "HTTP/1.0 200 OK\r\n" fi # Server type - `echo "," >> "$files_content_tmp"` - `ascii_to_bytes "Server: uIP/0.9\r\n" >> "$files_content_tmp"` - `echo "," >> "$files_content_tmp"` + echo "," >> "$FILES_CONTENT_TMP" + ascii_to_bytes "Server: uIP/0.9\r\n" + echo "," >> "$FILES_CONTENT_TMP" # Content - if [ "$_file_ext" == "css" ]; then - if [ -e "$yui_compressor" ]; then - _file_content=`java -jar "$yui_compressor" --charset utf-8 "$1" | od -A n -t x1 | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g'` + case $f_ext in + css) + if [ $HAVE_JAVA -eq 1 ] &&\ + [ -e $YUI_PATH ]; then + f_content=$(java -jar "$YUI_PATH" --charset utf-8 "$1" |\ + od -A n -t x1 | tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g') else - _file_content=`cat "$1" | tr -d '\r\n\t' | od -A n -t x1 | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g'` + f_content=$(cat "$1" | tr -d '\r\n\t' |\ + od -A n -t x1 | tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g') fi - `ascii_to_bytes "Content-type: text/css; charset=UTF-8\r\n\r\n" >> "$files_content_tmp"` - elif [ "$_file_ext" == "png" ]; then - _file_content=`od -A n -t x1 < "$1" | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g'` - `ascii_to_bytes "Content-Type: image/png\r\n\r\n" >> "$files_content_tmp"` - elif [ "$_file_ext" == "jpg" -o "$_file_ext" == "jpeg" ]; then - _file_content=`od -A n -t x1 < "$1" | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g'` - `ascii_to_bytes "Content-Type: image/jpeg\r\n\r\n" >> "$files_content_tmp"` - elif [ "$_file_ext" == "gif" ]; then - _file_content=`od -A n -t x1 < "$1" | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g'` - `ascii_to_bytes "Content-Type: image/gif\r\n\r\n" >> "$files_content_tmp"` - else - _file_content=`cat "$1" | tr -d '\t\r\n' | od -A n -t x1 | tr -d '\r\n' | sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g'` - `ascii_to_bytes "Content-type: text/html; charset=UTF-8\r\n\r\n" >> "$files_content_tmp"` - fi - `echo "," >> "$files_content_tmp"` + ascii_to_bytes "Content-type: text/css; charset=UTF-8\r\n\r\n" + ;; + png) + f_content=$(od -A n -t x1 < "$1" | tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g') + ascii_to_bytes "Content-Type: image/png\r\n\r\n" + ;; + jpg|jpeg) + f_content=$(od -A n -t x1 < "$1" | tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g') + ascii_to_bytes "Content-Type: image/jpeg\r\n\r\n" + ;; + gif) + f_content=$(od -A n -t x1 < "$1" | tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g') + ascii_to_bytes "Content-Type: image/gif\r\n\r\n" + ;; + htm|html) + f_content=$(cat "$1" | tr -d '\t\r\n' |\ + od -A n -t x1 | tr -d '\r\n' |\ + sed 's/ /0x/;s/ /, 0x/g;s/.\{102\}/&\n/g') + ascii_to_bytes "Content-type: text/html; charset=UTF-8\r\n\r\n" + ;; + *) + echo "ERROR! Unsupported file type: '${f_name}'!" + exit 1 + esac + + echo "," >> "$FILES_CONTENT_TMP" # File content - `echo -ne "/* Page/File content */\n" >> "$files_content_tmp"` - `echo -ne "$_file_content" >> "$files_content_tmp"` + echo -ne "/* Page/File content */\n" >> "$FILES_CONTENT_TMP" + echo -ne "${f_content}" >> "$FILES_CONTENT_TMP" # And close declaration - `echo -ne ", 0 };\n\n" >> "$files_content_tmp"` + echo -ne ", 0 };\n\n" >> "$FILES_CONTENT_TMP" } # $1 -> file path function print_data_struct() { - local _file_ext="${1##*.}" - local _file_name="${1##*/}" - local _file_name_no_ext="${_file_name%\.*}" - - `echo -ne "const struct fsdata_file file_"$_file_name_no_ext"_"$_file_ext"[] = {{\n" >> "$files_list_tmp"` - `echo -ne "\t"$prev_fsdata_struct",\n" >> "$files_list_tmp"` - `echo -ne "\t\"/$_file_name_no_ext.$_file_ext\",\n" >> "$files_list_tmp"` - `echo -ne "\tdata_"$_file_name_no_ext"_"$_file_ext",\n" >> "$files_list_tmp"` - `echo -ne "\t(int)sizeof(data_"$_file_name_no_ext"_"$_file_ext") - 1\n" >> "$files_list_tmp"` - `echo -ne "}};\n\n" >> "$files_list_tmp"` - - prev_fsdata_struct="file_"$_file_name_no_ext"_"$_file_ext"" + local f_ext="${1##*.}" + local f_name="${1##*/}" + local f_name_no_ext="${f_name%\.*}" + + echo -ne "const struct fsdata_file file_${f_name_no_ext}_${f_ext}[] = {{\n" >> "$FILES_LIST_TMP" + echo -ne "\t${PREV_FSDATA_STRUCT},\n" >> "$FILES_LIST_TMP" + echo -ne "\t\"/${f_name_no_ext}.${f_ext}\",\n" >> "$FILES_LIST_TMP" + echo -ne "\tdata_${f_name_no_ext}_${f_ext},\n" >> "$FILES_LIST_TMP" + echo -ne "\t(int)sizeof(data_"$f_name_no_ext"_"$f_ext") - 1\n" >> "$FILES_LIST_TMP" + echo -ne "}};\n\n" >> "$FILES_LIST_TMP" + + PREV_FSDATA_STRUCT="file_${f_name_no_ext}_${f_ext}" } -# === Main loop === +main() { + if [ -d vendors/$VENDOR_DIR ]; then + # Do we hava java? + which java > /dev/null 2>&1 + if [ $? -eq 0 ]; then + HAVE_JAVA=1 + fi -if [ -d vendors/"$vendor_dir" ]; then # If vendor dir exists - # Remove old fsdata.c - if [ -a "fsdata.c" ]; then - `rm "fsdata.c"` - fi + # Remove old fsdata.c + if [ -e fsdata.c ]; then + rm -f fsdata.c 2> /dev/null + fi - `touch "$files_content_tmp" "$files_list_tmp"` + # Temporary files + touch "$FILES_CONTENT_TMP" \ + "$FILES_LIST_TMP" - # Loop through all files in vendor dir - for file in vendors/"$vendor_dir"/*; do # For all found files - print_data_array $file - print_data_struct $file - files_counter=$((files_counter+1)) - done + # Loop through all files in vendor dir + for file in vendors/${VENDOR_DIR}/*; do + print_data_array "$file" + print_data_struct "$file" + FS_CNT=$((FS_CNT + 1)) + done - # Add required defines - `echo "#define FS_ROOT "$prev_fsdata_struct"" >> "$files_list_tmp"` - `echo "#define FS_NUMFILES "$files_counter"" >> "$files_list_tmp"` + # Add required defines + echo -e "#define FS_ROOT\t\t${PREV_FSDATA_STRUCT}" >> "$FILES_LIST_TMP" + echo -e "#define FS_NUMFILES\t${FS_CNT}" >> "$FILES_LIST_TMP" - # Generate new fsdata.c - `touch "fsdata.c"` + # Generate new fsdata.c + cat "$FILES_CONTENT_TMP" > fsdata.c + cat "$FILES_LIST_TMP" >> fsdata.c - `cat "$files_content_tmp" > "fsdata.c"` - `cat "$files_list_tmp" >> "fsdata.c"` + rm -f "$FILES_CONTENT_TMP" \ + "$FILES_LIST_TMP" 2> /dev/null + else + echo "ERROR! Vendor specific directory (u-boot/httpd/vendors/${VENDOR_DIR}) doesn't exist!" + exit 1 + fi +} - `rm "$files_content_tmp" "$files_list_tmp"` -else - echo "Error! Vendor specific directory (vendors/"$vendor_dir") doesn't exist!" -fi +# ===================== +# Execution begins here +# ===================== +main