autofox

browser auto-configuration
git clone https://git.awy.one/autofox
Log | Files | Refs | README

configure_firefox.sh (6113B) - View raw


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env bash

set -Eeo pipefail

[ "${UID}" = "0" ] && echo "Root not allowed" && exit

G='\e[1;92m' R='\e[1;91m' B='\e[1;94m'
P='\e[1;95m' Y='\e[1;93m' N='\033[0m'
C='\e[1;96m' W='\e[1;97m'

URL_USER_JS="https://raw.githubusercontent.com/arkenfox/user.js/master/user.js"
URL_UPDATER_SH="https://raw.githubusercontent.com/arkenfox/user.js/master/updater.sh"
URL_USER_OVERRIDES_JS="https://git.awy.one/autofox.git/plain/user-overrides.js"
URL_USERCHROME_CSS="https://git.awy.one/autofox.git/plain/userChrome.css"
URL_USERCONTENT_CSS="https://git.awy.one/autofox.git/plain/userContent.css"
URL_UBLOCK_BACKUP="https://git.awy.one/autofox.git/plain/ublock_backup.txt"

FILES_DIR="${HOME}/files"
USERNAME="$(id -nu "1000")"

loginf() {
  sleep "0.3"

  case "${1}" in
  g) COL="${G}" MSG="DONE!" ;;
  r) COL="${R}" MSG="WARNING!" ;;
  b) COL="${B}" MSG="STARTING." ;;
  c) COL="${B}" MSG="RUNNING." ;;
  esac

  TSK="${W}(${C}${TSKNO}${P}/${C}${ALLTSK}${W})"
  RAWMSG="${2}"

  DATE="$(date "+%Y-%m-%d ${C}/${P} %H:%M:%S")"

  LOG="${C}[${P}${DATE}${C}] ${Y}>>>${COL}${MSG}${Y}<<< ${TSK} - ${COL}${RAWMSG}${N}"

  [ "${1}" = "c" ] && echo -e "\n\n${LOG}" || echo -e "${LOG}"
}

handle_err() {
  stat="${?}"
  cmd="${BASH_COMMAND}"
  line="${LINENO}"
  loginf r "Line ${B}${line}${R}: cmd ${B}'${cmd}'${R} exited with ${B}\"${stat}\""
}

declare -A associate_files

associate_f() {
  key="${1}"
  url="${2}"
  base_path="${3}"

  final_path="${base_path}/${key}"

  associate_files["${key}"]="${url} ${FILES_DIR}/${key} ${final_path}"
}

update_associations() {
  associate_f "user.js" "${URL_USER_JS}" "${LIBREW_PROF_DIR}"
  associate_f "updater.sh" "${URL_UPDATER_SH}" "${LIBREW_PROF_DIR}"
  associate_f "user-overrides.js" "${URL_USER_OVERRIDES_JS}" "${LIBREW_PROF_DIR}"
  associate_f "userChrome.css" "${URL_USERCHROME_CSS}" "${LIBREW_CHROME_DIR}"
  associate_f "userContent.css" "${URL_USERCONTENT_CSS}" "${LIBREW_CHROME_DIR}"
  associate_f "ublock_backup.txt" "${URL_UBLOCK_BACKUP}" "${HOME}"
}

update_associations

move_file() {
  local key="${1}"
  #local custom_destination="${2:-}"
  local download_path final_destination
  IFS=' ' read -r _ download_path final_destination <<<"${associate_files[${key}]}"

  mv -fv "${download_path}" "${final_destination}"
}

progs() {
  pct="$((100 * ${2} / ${1}))"
  fll="$((65 * pct / 100))"
  bar=""
  for _ in $(seq "1" "${fll}"); do bar="${bar}${G}#${N}"; done
  for _ in $(seq "$((fll + 1))" "65"); do bar="${bar}${R}-${N}"; done
  printf "${bar}${P} ${pct}%%${N}\r"
}

download_file() {
  local source="${1}"
  local dest="${2}"

  [ -f "${dest}" ] && {
    loginf b "File ${dest} already exists, skipping download."
    return
  }

  curl -fSLk "${source}" -o "${dest}" >"/dev/null" 2>&1
}

retrieve_files() {
  rm -rfv "${FILES_DIR}" "${HOME}/ublock_backup.txt"
  mkdir -p "${FILES_DIR}"
  local total="$((${#associate_files[@]}))"
  local current="0"

  for key in "${!associate_files[@]}"; do
    current="$((current + 1))"
    progs "${total}" "${current}"

    IFS=' ' read -r source dest _ <<<"${associate_files[${key}]}"
    download_file "${source}" "${dest}"
  done

  echo ""
}

create_profile() {
  rm -rfv "${HOME}/.mozilla"
  firefox --headless >"/dev/null" 2>&1 &
  sleep "3"

  killall "firefox"
}

initiate_vars() {
  LIBREW_CONFIG_DIR="${HOME}/.mozilla/firefox"

  LIBREW_PROF_NAME="$(sed -n "/Default=.*\(esr\|release\)$/ { s/Default=//p; q }" \
    "${LIBREW_CONFIG_DIR}/profiles.ini")"

  LIBREW_PROF_DIR="${LIBREW_CONFIG_DIR}/${LIBREW_PROF_NAME}"
  LIBREW_CHROME_DIR="${LIBREW_PROF_DIR}/chrome"

  mkdir -pv "${LIBREW_CHROME_DIR}"

  update_associations
}

place_files() {
  move_file "user.js"
  move_file "user-overrides.js"
  move_file "updater.sh"
  move_file "userChrome.css"
  move_file "userContent.css"
}

modify_orides() {
  sed -i "s|/home/.*/downloads|/home/${USERNAME}/downloads|
		s/Count.*/Count\", $(nproc);/" \
    "${LIBREW_PROF_DIR}/user-overrides.js"
}

run_arkenfox() {
  chmod +x "${LIBREW_PROF_DIR}/updater.sh"

  "${LIBREW_PROF_DIR}/updater.sh" -s -u
}

install_extensions() {
  EXT_DIR="${LIBREW_PROF_DIR}/extensions"
  mkdir -pv "${EXT_DIR}"

  ADDON_NAMES=("ublock-origin" "violentmonkey")

  for ADDON_NAME in "${ADDON_NAMES[@]}"; do
    ADDON_URL="$(curl -fSk "https://addons.mozilla.org/en-US/firefox/addon/${ADDON_NAME}/" |
      grep -o 'https://addons.mozilla.org/firefox/downloads/file/[^"]*')"

    curl -fSLk "${ADDON_URL}" -o "extension.xpi"

    EXT_ID="$(unzip -p "extension.xpi" "manifest.json" | grep "\"id\"")"
    EXT_ID="${EXT_ID%\"*}"
    EXT_ID="${EXT_ID##*\"}"

    mv -fv "extension.xpi" "${EXT_DIR}/${EXT_ID}.xpi"
  done
}

place_ublock_backup() {
  move_file "ublock_backup.txt"
  rm -rfv "${HOME}/files"
}

main() {
  declare -A tsks
  tsks["retrieve_files"]="Retrieve the files.
		        Files retrieved."

  tsks["create_profile"]="Create a profile.
		           A profile created."

  tsks["initiate_vars"]="Initiate new variables.
		             New variabeles initiated."

  tsks["place_files"]="Place the necessary files.
			The necessary files placed."

  tsks["modify_orides"]="Modify overrides.
			Overrides modified."

  tsks["run_arkenfox"]="Run the ArkenFox script.
                                 The Arkenfox script successful."

  tsks["install_extensions"]="Install browser extensions.
                                 Browser extensions installed."

  tsks["place_ublock_backup"]="Place uBlock Backup.
                                uBlock Backup placed."

  tsk_ord=("retrieve_files" "create_profile" "initiate_vars" "place_files" "modify_orides"
    "run_arkenfox" "install_extensions" "place_ublock_backup")

  ALLTSK="${#tsks[@]}"
  TSKNO="1"

  trap 'handle_err' ERR RETURN

  for funct in "${tsk_ord[@]}"; do
    descript="${tsks[${funct}]}"
    descript="${descript%%$'\n'*}"

    msgdone="$(echo "${tsks[${funct}]}" | tail -n "1" | sed 's/^[[:space:]]*//g')"

    loginf b "${descript}"

    "${funct}"
    loginf g "${msgdone}"

    [[ "${TSKNO}" -eq "${ALLTSK}" ]] && {
      loginf g "All tsks completed."
      exit
    }

    ((TSKNO++))
  done
}

main