Advertisement
rs232

TTB 3.02

Dec 30th, 2021
1,449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.05 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # v1.00 Shibby 2013
  4. # v1.01 Fix memory leak + cosmetics - pedro 2019 - 2021
  5. # v2.05 [Command line operation, custom URL, local storage] + GUI integration added - rs232 2019
  6. # v3.02 Changed default URLs and added URL redundancy/randomisation  - rs232 06/2021
  7. #
  8.  
  9. PID=$$
  10. LOCK="/tmp/ttb.lock"
  11. DIR="/www/ext"
  12. LOGS="logger -s -t TTB[$PID]"
  13. SUBDIR="TomatoThemeBase"
  14. URL_ORIG="http://ttb.mooo.com http://ttb.ath.cx http://ttb.ddnsfree.com"
  15. URI=""
  16.  
  17. ttbexit() {
  18.     rm $LOCK >/dev/null 2>&1
  19.     exit $1
  20. }
  21.  
  22. ttbshuf() {
  23.     awk 'BEGIN {srand(); OFMT="%.17f"} {print rand(), $0}' "$@" | sort -k1,3n | cut -d ' ' -f2-;
  24. }
  25.  
  26. ttbdownload() {
  27.     ls -l /www/ext/ | grep "\->" | cut -d" " -f 27 | while read file; do
  28.         rm $DIR/$file  >/dev/null 2>&1
  29.     done
  30.     rm $DIR2/*  >/dev/null 2>&1
  31.     echo $(nvram get ttb_url | tr ' ' '\n' | ttbshuf ) | tr ' ' '\n' | sed 's/\/$//' |
  32.     while read l; do
  33.         wget -U "Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/88.0" -T 10 -t 1 $l/$TTB.zip -O $DIR2/$TTB.zip >/dev/null 2>&1 && echo $l && break
  34.     done
  35.     unzip -p $DIR2/$TTB.zip >/dev/null 2>&1 && {
  36.         cru d ttbDL
  37.     } || {
  38.         cru a ttbDL "*/5 * * * * /usr/sbin/ttb"
  39.         $LOGS "ERROR - Cannot download Online theme. Will try again soon..."
  40.         ttbexit 1
  41.     }
  42. }
  43.  
  44. ttbextract() {
  45.     rm $DIR/*.css $DIR/*.zip $DIR/*.png $DIR/*.gif $DIR/*.jpg  >/dev/null 2>&1
  46.     unzip -o $DIR2/$TTB.zip -d $DIR2/ >/dev/null 2>&1
  47.     ls -1 $DIR2 | grep -v .zip | grep -v .list |
  48.     (
  49.     while read line; do
  50.         ln -s $DIR2/$line $DIR/$line
  51.     done
  52.     )
  53.     $LOGS "INFO - The theme [ $TTB ] has been successfully applied"
  54. }
  55.  
  56. # check if another script in action
  57. [ -f $LOCK ] && {
  58.     $LOGS "WARNING - Another process in action - exiting"
  59.     exit 1
  60. }
  61.  
  62. # check if set to online
  63. [ ! "$(nvram get web_css)" == "online" ] && {
  64.     $LOGS "WARNING - The NVRAM variable [ web_css ] was not set to [ online ] - exiting"
  65.     exit 1
  66. }
  67.  
  68. [ -z "$(nvram get ttb_url)" ] && {
  69.     URL="$URL_ORIG"
  70.     nvram set ttb_url="$URL_ORIG"
  71.     nvram commit
  72. } || {
  73.     [ "$(nvram get ttb_url)" == "http://www.tomatothemebase.eu/wp-content/uploads" ] && nvram set ttb_url="$URL_ORIG"
  74.     URL=$(echo $(nvram get ttb_url) | sed 's/\/$//')
  75. }
  76.  
  77. if [ -n "$2" ]; then
  78.     DIR2=$(echo $2 | sed 's/\/$//')
  79.     DIR2=$DIR2/$SUBDIR
  80.     nvram set ttb_loc="$2"
  81.     nvram commit
  82. elif [ ! -n "$(nvram get ttb_loc)" ]; then
  83.     $LOGS "INFO - The NVRAM variable ttb_loc not set. I don't know where to save the theme will keep it in RAM"
  84.     DIR2="/tmp/$SUBDIR"
  85. else
  86.     DIR2=$(echo $(nvram get ttb_loc) | sed 's/\/$//')
  87.     DIR2=$DIR2/$SUBDIR
  88. fi
  89.  
  90. [ -d $DIR2 ] || mkdir $DIR2
  91. touch $LOCK
  92.  
  93. [ -n "$1" ] && {
  94.     if [ $1 == "help" ]; then
  95.         echo -e "\nNOTE: This is to be used for troubleshooting, always prefer the tomato GUI whenever possible.\n\n    TTB 3.02 - usage\n\nttb <no parameter>      - execute TTB based on NVRAM settings\n help            - Shows this info\n list            - Display list of available themes, this uses a themes.txt from the defined URL.\n  <theme name>        - Download, sets the theme name and sets NVRAM variables\n  <theme name> <folder>   - Download, sets the theme name, stores locally and sets NVRAM variables\n\n"
  96.         ttbexit 0
  97.     elif [ $1 == "list" ]; then
  98.         echo $(nvram get ttb_url | tr ' ' '\n' | ttbshuf ) | tr ' ' '\n' | sed 's/\/$//' |
  99.         while read l; do
  100.             ( wget -U "Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/88.0" -T 6 -t 1 -q -O - $l/themes.txt | more ) && break
  101.         done
  102.         ttbexit 0
  103.     else
  104.         TTB=$1
  105.         nvram set ttb_css="$1"
  106.         nvram commit
  107.     fi
  108. } || {
  109.     TTB=$(nvram get ttb_css)
  110.     [ -z $TTB ] && {
  111.         $LOGS "ERROR - Using TTB but the ttb_css nvram variable meant to contain the theme name is empty - exiting"
  112.         ttbexit 1
  113.     }
  114. }
  115.  
  116. [ -f $DIR2/$TTB.css ] && {
  117.     $LOGS "INFO - Installing theme [ $TTB ] from the local storage [ $DIR2 ]"
  118.     ttbextract $DIR2
  119. } || {
  120.     URI=$(ttbdownload $DIR2)
  121.     [[ $(echo $URL_ORIG | grep $URI >/dev/null 2>&1 | wc -l) -ne 1 ]] && {
  122.         SOURCE="user defined url [ $URI ]"
  123.     } || {
  124.         SOURCE="internally defined site [ $URI ]"
  125.     }
  126.     $LOGS "INFO - Downloaded theme [ $TTB ] from $SOURCE and stored it locally in [ $DIR2 ]"
  127.     ttbextract $DIR2
  128. }
  129.  
  130. ttbexit 0
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement