#!/bin/sh # amtm is free to use under the GNU General Public License version 3 (GPL-3.0) # https://opensource.org/licenses/GPL-3.0 # Proudly coded by thelonelycoder # Copyright (c) 2016-2066 thelonelycoder - All Rights Reserved # https://www.snbforums.com/members/thelonelycoder.25480/ # https://diversion.ch/amtm.html add=/jffs/addons/amtm amtmURL=https://fwupdate.asuswrt-merlin.net/amtm_fw amtmRev=7 a_m(){ [ -z "$am" ] && am=$1 || am="$am\\n$1";} c_nl(){ [ -n "$(tail -c2 "$1")" ] && echo >> "$1";} c_url(){ /usr/sbin/curl -fsNL --connect-timeout 10 --retry 3 --max-time 12 "$@";} d_t_u(){ dos2unix < $1 | cmp -s - $1;[ "$?" = 1 ] && dos2unix $1;} p_r_l(){ echo "${R}_____________________________________________${NC}";} p_e_l(){ p_r_l;echo;} r_w_e(){ [ "$(sed '/^[[:space:]]*$/d; /#!\/bin\/sh/d' "$1" | wc -c)" = 0 ] && rm "$1";} theme_standard(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=20;} theme_green(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=20;} theme_blue(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=21;} theme_blue_on_white(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=21;} theme_solarized(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=32;} theme_high_contrast(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=20;} theme_reduced(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=21;} theme_reduced_w(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=23;} theme_reduced_cw(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=23;} theme_reduced_b(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=21;} theme_reduced_cb(){ R='';R_BG='';E_BG='';GN='';GN_BG='';B='';GY='';NC='';COR=23;} theme_basic(){ R='';R_BG=;E_BG=;GN=;GN_BG=;B=;GY='';NC='';COR=15;} f_b_url(){ a_m " ! using ${R}fallback server${NC} diversion.ch";amtmURL=https://diversion.ch/amtm_fw;dfc=1;g_m "$@";} c_t(){ divconf=/opt/share/diversion/.conf/diversion.conf if [ -f "${add}"/.amtm_theme ]; then . "${add}"/.amtm_theme else [ -s "$divconf" ] && theme="$(grep "THEME=" "$divconf" | sed -e 's/THEME=//')" fi [ "$theme" ] && theme_$theme || theme_amtm new } g_m(){ [ "$1" = amtm.mod ] && set -- "$1" "$2" "${add}/a_fw" [ "$3" ] || set -- "$1" "$2" "${add}" if [ "$2" = new ]; then [ ! -f /tmp/amtm-dl ] && a_m "\\n Getting from $(echo $amtmURL | awk -F[/:] '{print $4}')" touch /tmp/amtm-dl c_url "$amtmURL/$1" -o "$3/${1}.new" if [ -s "$3/${1}.new" ]; then if grep -wq '^#bof' "$3/${1}.new" && grep -wq '^#eof' "$3/${1}.new"; then mv -f "$3/${1}.new" "$3/$1" a_m " - Module ${GN}$1${NC} ${rdl}downloaded" sfp=1;dlok=1;dfc= else rm -f "$3/${1}.new" a_m " ! Module ${R}$1${NC} is not an amtm file" dlok=0 [ -z "$dfc" ] && f_b_url "$@" fi elif [ -f "$3/$1" ]; then rm -f "$3/${1}.new" a_m " ! Module ${R}$1${NC} ${rdl}download failed, using existing file" dlok=0 [ -z "$dfc" ] && f_b_url "$@" else rm -f "$3/${1}.new" a_m " ! Module ${R}$1${NC} ${rdl}download failed" dlok=0 [ -z "$dfc" ] && f_b_url "$@" fi fi if [ "$2" = include ]; then if [ -f "$3/$1" ]; then . "$3/$1" dlok=1 else g_m "$1" new "$3" [ -f "$3/$1" ] && . "$3/$1" || dlok=0 fi fi } theme_amtm(){ p_e_l if [ -z "$1" ]; then printf " All colors in use are shown.\\n Your current theme is: ${R_BG} $theme ${NC}\\n\\n" else printf " Select a theme that works best in your\\n SSH client. All colors in use are shown.\\n\\n" fi themes='standard green blue blue_on_white solarized high_contrast reduced reduced_w reduced_cw reduced_b reduced_cb' i=1 for theme in $themes; do ncorr=' ' case $theme in blue) corr1=-1;corr2=-1;; blue_on_white) corr2=-1;; solarized) corr3=-2;; reduced) corr2=-1;; reduced_w) corr2=-3;; reduced_cw) corr2=-3;; reduced_b) corr2=+5;ncorr=;; reduced_cb) corr1=-2;corr2=+3;ncorr=;; esac theme_$theme printf "%-$((COR+2$corr1))s %-$((COR+4$corr3))s %-$((COR-6))s\\n" "${R_BG}$ncorr$i. $theme" "${NC}${GN_BG} $theme" "${NC}${B} ${theme:0:10}${NC}" printf " %-$((COR-1))s %-$((COR+4$corr2))s %-s\\n" "${E_BG} $theme" "${NC}${GN} $theme" "${NC}${GY} ${theme:0:10}${NC}" p_e_l i=$((i+1)) unset corr3 corr2 corr1 ncorr done theme_basic printf "${R_BG}12. basic ${NC}${GN_BG} basic${NC}\\n" p_r_l ton=12;noad= if [ -f "$divconf" ]; then printf "\\n13. Let Diversion set theme ($(grep "THEME=" "$divconf" | sed -e 's/THEME=//'))\\n" p_r_l ton=13;noad=13 fi printf "\\n The basic and reduced themes use no or fewer\\n colors, service states may not be visible.\\n" theme_standard while true; do if [ -z "$1" ]; then printf "\\n Set new amtm theme: [1-$ton e=Exit] ";read -r continue else printf "\\n Select amtm theme: [1-$ton] ";read -r continue fi case "$continue" in 1) theme=standard;break;; 2) theme=green;break;; 3) theme=blue;break;; 4) theme=blue_on_white;break;; 5) theme=solarized;break;; 6) theme=high_contrast;break;; 7) theme=reduced;break;; 8) theme=reduced_w;break;; 9) theme=reduced_cw;break;; 10) theme=reduced_b;break;; 11) theme=reduced_cb;break;; 12) theme=basic;break;; $noad) [ -f "${add}"/.amtm_theme ] && rm "${add}"/.amtm_theme theme= show_amtm " amtm now uses the Diversion theme" break;; [Ee]) show_amtm menu;; *) printf "\\n input is not an option\\n";; esac done echo "theme=$theme" >"${add}"/.amtm_theme [ "$1" ] || show_amtm " changed theme to $theme" } init_amtm(){ [ -d "${add}" ] && fmd= || fmd=1 mkdir -p "${add}/a_fw" if [ ! -d "${add}" ]; then printf "\\n amtm failed to create the directory\\n ${add}\\n Please investigate. Aborting amtm now.\\n\\n";exit 1 fi if [ "$(nvram get ntp_ready)" = 0 ]; then printf "\\n NTP not ready, check that the router time\\n is synced.\\n Aborting amtm now.\\n\\n";exit 1 fi mv /jffs/amtm-* "${add}" 2> /dev/null;mv /jffs/.amtm_* "${add}" 2> /dev/null c_t if [ -f "/jffs/configs/profile.add" ]; then sed -i '/alias amtm=/d' /jffs/configs/profile.add >/dev/null r_w_e /jffs/configs/profile.add fi if [ -f /jffs/scripts/amtm ] || [ -f /opt/bin/amtm ]; then rm -f /jffs/scripts/amtm /opt/bin/amtm a_m " amtm migrated to integrated firmware version" else a_m " Initializing amtm for first run" fi [ "$fmd" ] && a_m " - Created ${GN}${add}${NC} directory" if [ "$(nvram get jffs2_scripts)" != 1 ]; then a_m " - JFFS custom scripts and configs enabled" nvram set jffs2_scripts=1 nvram commit fi g_m amtm.mod new if [ ! -f "${add}"/a_fw/amtm.mod ]; then rm -f /tmp/amtm-dl p_e_l printf "$am\\n\\n" printf " All download attempts ${R}failed${NC},\\n exiting amtm now.\\n\\n Check that both domains properly resolve\\n on this router:\\n - ${GN}fwupdate.asuswrt-merlin.net${NC}\\n - ${GN}$(echo $amtmURL | awk -F[/:] '{print $4}')$1${NC}\\n\\n" exit 1 fi exec "$0" "$am" } run_amtm(){ if [ ! -f "${add}"/a_fw/amtm.mod ] || [ -f /jffs/scripts/amtm ]; then init_amtm elif [ -z "$1" ]; then [ "$am" ] && show_amtm "$am" || show_amtm menu elif [ "$1" = tpu ]; then su=1;suUpd=0;updErr=;tpu=1 > /tmp/amtm-tpu-check show_amtm >/dev/null 2>&1 elif [ "$1" = updcheck ]; then su=1;suUpd=0;updErr=;tpu=1;updcheck=1 echo "Available script updates:" >/tmp/amtm-tpu-check update_firmware update_amtm show_amtm else show_amtm "$1" fi } [ -f "${add}"/a_fw/amtm.mod ] && . "${add}"/a_fw/amtm.mod run_amtm "$@"