Advertisement
MartineauPASTEBIN

Dictionary_MSG.sh

Sep 16th, 2018
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.39 KB | None | 0 0
  1. #!/bin/sh
  2. VER="v1.01"
  3. #============================================================================== © 2016-2018 Martineau, v01.01
  4. #
  5. # Utility to query/manage Language Dictionaries
  6. #
  7. #     Dictionary_MSG    [ help | -h ]
  8. #                       { text_msg | msg_no [ language] }  |  { map { language } | reset }
  9. #     Dictionary_MSG    Yes
  10. #                       List all messages containing 'Yes' within the current/mapped Dictionary.
  11. #                            124:  Yes
  12. #                            1509: Select [Yes] to enable filter that specify IP or port for control incoming and outgoing packets.
  13. #                            1711: Selecting Yes allows multiple players in the LAN to play Starcraft at the same time.
  14. #                            <snip>
  15. #     Dictionary_MSG    Ja DE
  16. #                       List all messages containing 'Ja' in the German Dictionary
  17. #                            73:   Ich bin 16 Jahre oder älter.
  18. #                            74:   Bitte bestätigen Sie, dass Sie 16 Jahre oder älter sind.
  19. #                            124:  Ja
  20. #                            299:  Ihr Browser unterstützt kein JavaScript. Wahlen Sie einen Browser, der JavaScript unterstützt oder aktivieren Sie JavaScript zur Verwendung mit Ihrem RT-AC68U.
  21. #                            <snip>
  22. #     Dictionary_MSG    1837
  23. #                       List message number '1837' within the current/mapped Dictionary.
  24. #                            1837: IP address, server and hostname have not changed since the last update. If you want to update, please click [Yes].
  25. #     Dictionary_MSG    map JP
  26. #                       Replace the existing Language Dictionary with the Japanese Dictionary
  27. #     Dictionary_MSG    reset
  28. #                       Remove any Language Dictionary mapping
  29.  
  30. # Print between line beginning with'#==' to first blank line inclusive
  31. ShowHelp() {
  32.     awk '/^#==/{f=1} f{print; if (!NF) exit}' $0
  33. }
  34. ANSIColours () {
  35.  
  36.     cRESET="\e[0m";cBLA="\e[30m";cRED="\e[31m";cGRE="\e[32m";cYEL="\e[33m";cBLU="\e[34m";cMAG="\e[35m";cCYA="\e[36m";cGRA="\e[37m"
  37.     cBGRA="\e[90m";cBRED="\e[91m";cBGRE="\e[92m";cBYEL="\e[93m";cBBLU="\e[94m";cBMAG="\e[95m";cBCYA="\e[96m";cBWHT="\e[97m"
  38.     aBOLD="\e[1m";aDIM="\e[2m";aUNDER="\e[4m";aBLINK="\e[5m";aREVERSE="\e[7m"
  39.     cRED_="\e[41m";cGRE_="\e[42m"
  40.  
  41. }
  42. #=========================================================Main====================================================
  43. Main(){}
  44.  
  45. ANSIColours
  46.  
  47. # Help request ?
  48. if [ -z "$1" ] || [ "$1" == "help" ] || [ "$1" == "-h" ];then       # Show help
  49.     echo -e $cBWHT
  50.    ShowHelp
  51.    echo -en $cRESET
  52.    exit 0
  53. fi
  54.  
  55.  
  56. #   BR  CZ  DE  ES  FR  IT  KR  NO  RO  SV  TR  UK
  57. #   CN  DA  EN  FI  HU  JP  MS  PL  RU  TH  TW
  58.  
  59. DICTIONARY=$(nvram get preferred_lang)
  60.  
  61. # Was a dynamic language mapping/unmapping requested?
  62. case "$1" in
  63.     "map")  if [ ! -z "$2" ];then
  64.                 DICTIONARY=$(echo $2 | tr 'a-z' 'A-Z')
  65.                 if [ -f /www/$DICTIONARY.dict ];then
  66.                     # Map the desired Language Dictionary
  67.                     case $DICTIONARY in
  68.                         EN) TXT="English";;
  69.                         BR) TXT="Brazilian";;
  70.                         CN) TXT="Chinese";;
  71.                         CZ) TXT="Czech";;
  72.                         DA) TXT="Danish";;
  73.                         DE) TXT="German";;
  74.                         ES) TXT="Spanish";;
  75.                         FI) TXT="Finnish";;
  76.                         FR) TXT="French";;
  77.                         HU) TXT="Hungarian";;
  78.                         IT) TXT="Italian";;
  79.                         JP) TXT="Japanese";;
  80.                         KR) TXT="Korean";;
  81.                         MS) TXT="Malaysian";;
  82.                         NO) TXT="Norwegian";;
  83.                         PL) TXT="Polish";;
  84.                         RO) TXT="Romanian";;
  85.                         RU) TXT="Russian";;
  86.                         SV) TXT="Swedish";;
  87.                         TH) TXT="Thai";;
  88.                         TR) TXT="Turkish";;
  89.                         TW) TXT="Taiwanese";;
  90.                         UK) TXT="Ukrainian";;
  91.                         *)  TXT="?";;
  92.                     esac
  93.                     echo -e "\n\t"${cBGRE}"Mapping" $TXT "Language Dictionary '/www/$DICTIONARY.dict' -> '/www/$(nvram get preferred_lang).dict'\n" $cBRED
  94.                     umount /www/$(nvram get preferred_lang).dict 2>/dev/null
  95.                     MSG="Setup is complete"
  96.                     MSGNO=$(awk  -v pattern="${MSG}" '$0~pattern {print NR-1}' /www/EN.dict)    # Get position in English
  97.                     MSGNO=$((MSGNO+1))                                  # Display as used in HTML index reference '<#123#>'
  98.                     mount -o bind /www/$DICTIONARY.dict /www/$(nvram get preferred_lang).dict
  99.                     echo -e $cBCYA"\t\t"$(df | grep /www/$(nvram get preferred_lang).dict)
  100.                     echo -e $cBMAG"\t\t"$(cat /www/$DICTIONARY.dict | awk NR==$MSGNO)           # "Setup is complete" ->> translation
  101.                     echo -e "\n\t"${cBGRE}"Restarting httpd....."
  102.                     service restart_httpd
  103.                     echo -e $cRESET
  104.                     exit 0
  105.                 else
  106.                     echo -e "\a\n\t"${cBRED}"***ERROR Language Dictionary '/www/"$DICTIONARY".dict' not found!\n"$cRESET
  107.                     exit 99
  108.                 fi
  109.             else
  110.                 echo -e "\a\n\t"${cBRED}"Missing arg2 Language Dictionary e.g. 'map   FR' to map to French language\n"$cRESET
  111.                 exit 99
  112.             fi
  113.             ;;
  114.             # Remove the mapping
  115.     "reset")    echo -e "\n\t"${cBGRE}"Removing Language Dictionary mapping for '/www/$(nvram get preferred_lang).dict'\n" $cBRED
  116.             if [ ! -z "$(df | grep /www/$(nvram get preferred_lang).dict)" ];then
  117.                 umount /www/$(nvram get preferred_lang).dict
  118.                 #echo -en $cBCYA"\t\t"$(df | grep /www/$(nvram get preferred_lang).dict)
  119.                 MSG="Setup is complete"
  120.                 MSGNO=$(awk  -v pattern="${MSG}" '$0~pattern {print NR-1}' /www/EN.dict)    # Get position in English
  121.                 MSGNO=$((MSGNO+1))                                      # Display as used in HTML index reference '<#123#>'
  122.                 echo -e $cBMAG"\t\t"$(cat /www/$DICTIONARY.dict | awk NR==$MSGNO)           # "Setup is complete" ->> translation
  123.                 echo -e "\n\t"${cBGRE}"Restarting httpd....."
  124.                 service restart_httpd
  125.             else
  126.                 echo -e "\t\t"$cBYEL"*** No current Language Dictionary mapping for '/www/$(nvram get preferred_lang).dict'\n"
  127.             fi
  128.             echo -e $cRESET
  129.             exit 0
  130.             ;;
  131. esac
  132.    
  133. # Dictionay query/enquire for a different language?
  134. if [ ! -z "$2" ];then
  135.     DICTIONARY=$(echo $2 | tr 'a-z' 'A-Z')
  136. fi
  137.  
  138. MSGNO=$1
  139.  
  140. # Specific MSGNO requested.....
  141. if [ ! -z "$1" ] && [ "$(echo "$1" |  grep -oE '^[0-9]+$')" ];then      # Is a number provided
  142.  
  143.     if [ -f /www/$DICTIONARY.dict ];then
  144.         if [ $(wc -l </www/$DICTIONARY.dict) -gt $MSGNO ];then
  145.             MSGNO=$((MSGNO+1))                                          # Display as used in HTML index reference '<#123#>'
  146.             echo -e "\n\t"${cBGRE}$MSGNO":\t"$(cat /www/$DICTIONARY.dict | awk NR==$MSGNO)"\n" $cRESET
  147.         else
  148.             echo -e "\n\t\a"$cBRED"MSGNO="$1" too large\n" $cRESET
  149.         fi
  150.     else
  151.         echo -e "\n\t\a"$cBRED"Language dictionary '"$2".dict' not found...\tBR  CZ  DE  ES  FR  IT  KR  NO  RO  SV  TR  UK CN  DA  EN  FI  HU  JP  MS  PL  RU  TH  TW\n" $cRESET
  152.     fi
  153. else                                                                        # Search for all matching text
  154.  
  155.     #grep -ni "$1" /www/$DICTIONARY.dict
  156.     # Case match?
  157.     if [ "Case" == "Yes" ];then
  158.         awk  -v pattern="${1}" '$0~pattern {print NR-1":\t"$0}' /www/$DICTIONARY.dict
  159.     else
  160.        
  161.         if [ ! -z "$(awk  -v pattern="${1}" 'tolower($0)~tolower(pattern) {print "\t"NR-1":\t"$0}' /www/$DICTIONARY.dict)" ];then
  162.             echo -en "\n"$cMAG"\tMatching dictionary ('"$DICTIONARY.dict"') results for search string"${cBYEL}" '"$1"'\n\n" $cBCYA
  163.             awk  -v pattern="${1}" 'tolower($0)~tolower(pattern) {print "\t"NR-1":\t"$0}' /www/$DICTIONARY.dict
  164.         else
  165.             echo -e "\n\t\a"$cBRED"***No dictionay ('"$DICTIONARY.dict"') match for search string"${cBYEL}" '"$1"'" $cRESET
  166.         fi
  167.        
  168.     fi
  169.     echo -e $cRESET
  170. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement