autowolf

automated librewolf setup
git clone https://git.awy.one/autowolf.git
Log | Files | Refs | README

configure_librewolf.sh (7100B)


      1 #!/usr/bin/env bash
      2 
      3 set -Eeo pipefail
      4 
      5 [ "${UID}" = "0" ] && echo "Root not allowed" && exit
      6 
      7 G='\e[1;92m' R='\e[1;91m' B='\e[1;94m'
      8 P='\e[1;95m' Y='\e[1;93m' N='\033[0m'
      9 C='\e[1;96m' W='\e[1;97m'
     10 
     11 URL_USER_JS="https://raw.githubusercontent.com/arkenfox/user.js/master/user.js"
     12 URL_UPDATER_SH="https://raw.githubusercontent.com/arkenfox/user.js/master/updater.sh"
     13 URL_USER_OVERRIDES_JS="https://git.awy.one/raw/?repository=autowolf.git&file=user-overrides.js"
     14 URL_USERCHROME_CSS="https://git.awy.one/raw/?repository=autowolf.git&file=userChrome.css"
     15 URL_USERCONTENT_CSS="https://git.awy.one/raw/?repository=autowolf.git&file=userContent.css"
     16 URL_UBLOCK_BACKUP="https://git.awy.one/raw/?repository=autowolf.git&file=ublock_backup.txt"
     17 
     18 FILES_DIR="${HOME}/files"
     19 USERNAME="$(id -nu "1000")"
     20 
     21 loginf() {
     22         sleep "0.3"
     23 
     24         case "${1}" in
     25                 g) COL="${G}" MSG="DONE!" ;;
     26                 r) COL="${R}" MSG="WARNING!" ;;
     27                 b) COL="${B}" MSG="STARTING." ;;
     28                 c) COL="${B}" MSG="RUNNING." ;;
     29         esac
     30 
     31         TSK="${W}(${C}${TSKNO}${P}/${C}${ALLTSK}${W})"
     32         RAWMSG="${2}"
     33 
     34         DATE="$(date "+%Y-%m-%d ${C}/${P} %H:%M:%S")"
     35 
     36         LOG="${C}[${P}${DATE}${C}] ${Y}>>>${COL}${MSG}${Y}<<< ${TSK} - ${COL}${RAWMSG}${N}"
     37 
     38         [ "${1}" = "c" ] && echo -e "\n\n${LOG}" || echo -e "${LOG}"
     39 }
     40 
     41 handle_err() {
     42         stat="${?}"
     43         cmd="${BASH_COMMAND}"
     44         line="${LINENO}"
     45         loginf r "Line ${B}${line}${R}: cmd ${B}'${cmd}'${R} exited with ${B}\"${stat}\""
     46 }
     47 
     48 declare -A associate_files
     49 
     50 associate_f() {
     51         key="${1}"
     52         url="${2}"
     53         base_path="${3}"
     54 
     55         final_path="${base_path}/${key}"
     56 
     57         associate_files["${key}"]="${url} ${FILES_DIR}/${key} ${final_path}"
     58 }
     59 
     60 update_associations() {
     61         associate_f "user.js" "${URL_USER_JS}" "${LIBREW_PROF_DIR}"
     62         associate_f "updater.sh" "${URL_UPDATER_SH}" "${LIBREW_PROF_DIR}"
     63         associate_f "user-overrides.js" "${URL_USER_OVERRIDES_JS}" "${LIBREW_PROF_DIR}"
     64         associate_f "userChrome.css" "${URL_USERCHROME_CSS}" "${LIBREW_CHROME_DIR}"
     65         associate_f "userContent.css" "${URL_USERCONTENT_CSS}" "${LIBREW_CHROME_DIR}"
     66         associate_f "ublock_backup.txt" "${URL_UBLOCK_BACKUP}" "${HOME}"
     67 }
     68 
     69 update_associations
     70 
     71 move_file() {
     72         local key="${1}"
     73         #local custom_destination="${2:-}"
     74         local download_path final_destination
     75         IFS=' ' read -r _ download_path final_destination <<< "${associate_files[${key}]}"
     76 
     77         mv -fv "${download_path}" "${final_destination}"
     78 }
     79 
     80 progs() {
     81         pct="$((100 * ${2} / ${1}))"
     82         fll="$((65 * pct / 100))"
     83         bar=""
     84         for _ in $(seq "1" "${fll}"); do bar="${bar}${G}#${N}"; done
     85         for _ in $(seq "$((fll + 1))" "65"); do bar="${bar}${R}-${N}"; done
     86         printf "${bar}${P} ${pct}%%${N}\r"
     87 }
     88 
     89 download_file() {
     90         local source="${1}"
     91         local dest="${2}"
     92 
     93         [ -f "${dest}" ] && {
     94                 loginf b "File ${dest} already exists, skipping download."
     95                 return
     96         }
     97 
     98         curl -fSLk "${source}" -o "${dest}" > "/dev/null" 2>&1
     99 }
    100 
    101 retrieve_files() {
    102         rm -rfv "${FILES_DIR}" "${HOME}/ublock_backup.txt"
    103         mkdir -p "${FILES_DIR}"
    104         local total="$((${#associate_files[@]}))"
    105         local current="0"
    106 
    107         for key in "${!associate_files[@]}"; do
    108                 current="$((current + 1))"
    109                 progs "${total}" "${current}"
    110 
    111                 IFS=' ' read -r source dest _ <<< "${associate_files[${key}]}"
    112                 download_file "${source}" "${dest}"
    113         done
    114 
    115         echo ""
    116 }
    117 
    118 create_profile() {
    119         rm -rfv "${HOME}/.librewolf"
    120         librewolf --headless > "/dev/null" 2>&1 &
    121         sleep "3"
    122 
    123         killall "librewolf"
    124 }
    125 
    126 initiate_vars() {
    127         LIBREW_CONFIG_DIR="${HOME}/.librewolf"
    128 
    129         LIBREW_PROF_NAME="$(sed -n "/Default=.*\(esr\|release\)$/ { s/Default=//p; q }" \
    130                 "${LIBREW_CONFIG_DIR}/profiles.ini")"
    131 
    132         LIBREW_PROF_DIR="${LIBREW_CONFIG_DIR}/${LIBREW_PROF_NAME}"
    133         LIBREW_CHROME_DIR="${LIBREW_PROF_DIR}/chrome"
    134 
    135         mkdir -pv "${LIBREW_CHROME_DIR}"
    136 
    137         update_associations
    138 }
    139 
    140 place_files() {
    141         move_file "user.js"
    142         move_file "user-overrides.js"
    143         move_file "updater.sh"
    144         move_file "userChrome.css"
    145         move_file "userContent.css"
    146 }
    147 
    148 modify_orides() {
    149         sed -i "s|/home/.*/downloads|/home/${USERNAME}/downloads|
    150 		s/Count.*/Count\", $(nproc);/" \
    151                 "${LIBREW_PROF_DIR}/user-overrides.js"
    152 }
    153 
    154 run_arkenfox() {
    155         chmod +x "${LIBREW_PROF_DIR}/updater.sh"
    156 
    157         "${LIBREW_PROF_DIR}/updater.sh" -s -u
    158 }
    159 
    160 install_extensions() {
    161         EXT_DIR="${LIBREW_PROF_DIR}/extensions"
    162         mkdir -pv "${EXT_DIR}"
    163 
    164 	ADDON_NAMES=("ublock-origin" "violentmonkey" "vimium-ff")
    165 
    166         for ADDON_NAME in "${ADDON_NAMES[@]}"; do
    167                 ADDON_URL="$(curl -fSk "https://addons.mozilla.org/en-US/firefox/addon/${ADDON_NAME}/" |
    168                         grep -o 'https://addons.mozilla.org/firefox/downloads/file/[^"]*')"
    169 
    170                 curl -fSLk "${ADDON_URL}" -o "extension.xpi"
    171 
    172                 EXT_ID="$(unzip -p "extension.xpi" "manifest.json" | grep "\"id\"")"
    173                 EXT_ID="${EXT_ID%\"*}"
    174                 EXT_ID="${EXT_ID##*\"}"
    175 
    176                 mv -fv "extension.xpi" "${EXT_DIR}/${EXT_ID}.xpi"
    177         done
    178 }
    179 
    180 place_ublock_backup() {
    181         move_file "ublock_backup.txt"
    182         rm -rfv "${HOME}/files"
    183 }
    184 
    185 main() {
    186         declare -A tsks
    187         tsks["retrieve_files"]="Retrieve the files.
    188 		        Files retrieved."
    189 
    190         tsks["create_profile"]="Create a profile.
    191 		           A profile created."
    192 
    193         tsks["initiate_vars"]="Initiate new variables.
    194 		             New variabeles initiated."
    195 
    196         tsks["place_files"]="Place the necessary files.
    197 			The necessary files placed."
    198 
    199         tsks["modify_orides"]="Modify overrides.
    200 			Overrides modified."
    201 
    202         tsks["run_arkenfox"]="Run the ArkenFox script.
    203                                  The Arkenfox script successful."
    204 
    205         tsks["install_extensions"]="Install browser extensions.
    206                                  Browser extensions installed."
    207 
    208         tsks["place_ublock_backup"]="Place uBlock Backup.
    209                                 uBlock Backup placed."
    210 
    211         tsk_ord=("retrieve_files" "create_profile" "initiate_vars" "place_files" "modify_orides"
    212                 "run_arkenfox" "install_extensions" "place_ublock_backup")
    213 
    214         ALLTSK="${#tsks[@]}"
    215         TSKNO="1"
    216 
    217         trap 'handle_err' ERR RETURN
    218 
    219         for funct in "${tsk_ord[@]}"; do
    220                 descript="${tsks[${funct}]}"
    221                 descript="${descript%%$'\n'*}"
    222 
    223                 msgdone="$(echo "${tsks[${funct}]}" | tail -n "1" | sed 's/^[[:space:]]*//g')"
    224 
    225                 loginf b "${descript}"
    226 
    227                 "${funct}"
    228                 loginf g "${msgdone}"
    229 
    230                 [[ "${TSKNO}" -eq "${ALLTSK}" ]] && {
    231                         loginf g "All tsks completed."
    232                         exit
    233                 }
    234 
    235                 ((TSKNO++))
    236         done
    237 }
    238 
    239 main