#!/bin/bash

#####
##### Copyright (C) 2020-2021 Aloysius Indrayanto
#####
##### This file is part of the eCxx library, see LICENSE file for the license details.
#####


#####
##### Download the CSV file
#####
SRC_URL='https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv'
TMP_CSV='/tmp/zones_data.csv'

wget $SRC_URL -O $TMP_CSV || curl $SRC_URL > $TMP_CSV


#####
##### Parse the CSV file into names and values
##### The SED and TR commands are based on https://github.com/esp8266/Arduino/blob/master/tools/TZupdate.sh
#####
TMP_NSTR='/tmp/zones_nstr.txt'
TMP_VSTR='/tmp/zones_vstr.txt'

sed -e 's/^\([^,]*\),.*/\1/g' -e 's,["],,g' $TMP_CSV | tr '/\-+' '_mp' > $TMP_NSTR
sed -e 's/^[^,]*,//g'                       $TMP_CSV                   > $TMP_VSTR


#####
##### Generate the C++ code for the names and values
#####

##### Using #define and PGM_STR() macros
#cat $TMP_NSTR | awk '{ printf "#define TZ_%-30s\n", $1 }' | tee $TMP_NSTR > /dev/null
#cat $TMP_VSTR | awk '{ printf "PGM_STR(%-46s)\n", $1 }'   | tee $TMP_VSTR > /dev/null

##### Using PGMCharPointer and program string literal
#cat $TMP_NSTR | awk '{ printf "static PGMCharPointer %-30s =\n", $1 }' | tee $TMP_NSTR > /dev/null
#cat $TMP_VSTR | awk '{ printf "%46s_pgm;\n", $1 }'                     | tee $TMP_VSTR > /dev/null

##### Using plain PROGMEM constant character array
#cat $TMP_NSTR | awk '{ printf "%s_P\n", $1 }'                                | tee $TMP_NSTR > /dev/null
#cat $TMP_NSTR | awk '{ printf "static const char PROGMEM %-32s[] =\n", $1 }' | tee $TMP_NSTR > /dev/null
#cat $TMP_VSTR | awk '{ printf "%-46s;\n", $1 }'                              | tee $TMP_VSTR > /dev/null

##### Using constexpr constant pointer to constant string literal
cat $TMP_NSTR | awk '{ printf "%s\n", $1 }'                                         | tee $TMP_NSTR > /dev/null
cat $TMP_NSTR | awk '{ printf "static constexpr const char* const %-32s =\n", $1 }' | tee $TMP_NSTR > /dev/null
cat $TMP_VSTR | awk '{ printf "%-46s;\n", $1 }'                                     | tee $TMP_VSTR > /dev/null


#####
##### Generate the C++ header file
#####
OUT_FILE='TZName.h'

cat 'prefix.h'                                            \
    | sed '/^[[:space:]]*$/{:a;$d;N;/\n[[:space:]]*$/ba}' \
    | sed -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;};/\n$/ba' > $OUT_FILE
echo >> $OUT_FILE
echo >> $OUT_FILE

echo "/*"                                                                >> $OUT_FILE
echo " * These constants are autogenerated from IANA timezone database:" >> $OUT_FILE
echo " *     $SRC_URL"                                                   >> $OUT_FILE
echo " * using the script:"                                              >> $OUT_FILE
echo " *     <eCxx_source_code_root>/tools/tzname/generate.sh"           >> $OUT_FILE
echo " */"                                                               >> $OUT_FILE

paste -d' ' $TMP_NSTR $TMP_VSTR >> $OUT_FILE

echo >> $OUT_FILE
echo >> $OUT_FILE
cat 'postfix.h'                                           \
    | sed '/^[[:space:]]*$/{:a;$d;N;/\n[[:space:]]*$/ba}' \
    | sed -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;};/\n$/ba' >> $OUT_FILE

echo -e "\nHeader file '$OUT_FILE' has been generated.\n"
echo -e "Please move it to '../../include/eCxx'.\n\n"


#####
##### Delete the temporary files
#####
rm -f $TMP_CSV $TMP_NSTR $TMP_VSTR
