#!/bin/bash MYVERSION=0.20070906 # works with cygwin and gnu/linux # If you improve this script, please read the comments at the # end of this file. Thanks. # usage/help message usage_message() { cat >&2 <&2 exit $E_OSTYPE } #parse the arguments while getopts "ahV" ARGS 2>/dev/null; do case $ARGS in \?) printf "$ERRFMT" "cannot parse given parameters" >&2 usage_message exit $E_ARGS ;; a) STRIPALL=yes; ;; h) usage_message; exit $E_HELP; ;; V) printf "$NFOFMT" "version $MYVERSION" >&2; exit $E_HELP; ;; esac done # move on to non-option parameters shift $((OPTIND - 1)) # see if a profilename was given [[ $# -gt 1 ]] && { printf "$ERRFMT" "cannot parse given parameters" >&2 exit $E_ARGS } (($#)) && PROFNAME=$1 || PROFNAME=$DEFPROFNAME # use the profilename to find its directory by examining profiles.ini # then set $BMFILE to the file we want to work on # (note: the [[:cntrl:]] characters are there in case we are dealing with # a CR/LF-formatted profiles.ini) PROFDESC=$(grep -B 1 -A 2 "^Name=$PROFNAME[[:cntrl:]]\?$" "$FXSETDIR/profiles.ini") [[ -z $PROFDESC ]] && { printf "$ERRFMT" \ "cannot find profile '$PROFNAME'" \ "to verify the name, run firefox -ProfileManager" >&2 usage_message exit $E_PROF } [[ $(echo "$PROFDESC" | wc -l) -ne 4 ]] && { printf "$ERRFMT" \ "cannot determine directory for profile '$PROFNAME'" \ "please debug me now" >&2 exit $E_PROF } RELFLAG=$(echo "$PROFDESC" | sed '/^IsRelative=\([01]\)/!d; s~^IsRelative=\([01]\)[[:cntrl:]]\?$~\1~') [[ $RELFLAG -eq 1 ]] && PROFDIR=$FXSETDIR/$(echo "$PROFDESC" | sed '/^Path=/!d; s~^Path=~~') || PROFDIR=$(echo "$PROFDESC" | sed '/^Path=/!d; s~^Path=~~') [[ ! -d $PROFDIR ]] && { printf "$ERRFMT" \ "cannot determine directory for profile '$PROFNAME'" \ "please debug me now" >&2 exit $E_PROF } # if under cygwin, change path to POSIXness if [[ $OSTYPE == *cygwin* ]]; then PROFDIR=$(cygpath "$PROFDIR") || { printf "$ERRFMT" "error using cygpath under cygwin" \ "please make sure cygpath is installed" >&2 exit $E_CYG } fi [[ -h $PROFDIR/lock ]] || [[ -e $PROFDIR/lock ]] || [[ -e $PROFDIR/parent.lock ]] && { printf "$ERRFMT" \ "profile '$PROFNAME' is locked" \ "try again after killing any firefoxen" >&2 exit $E_PROF } [[ -f $PROFDIR/bookmarks.html ]] && BMFILE=$PROFDIR/bookmarks.html || { printf "$ERRFMT" \ "cannot find the bookmarks for profile '$PROFNAME'" \ "please debug me now" >&2 exit $E_PROF } SIZE1=$(stat --printf=%s "$BMFILE") || { printf "$ERRFMT" \ "cannot stat the bookmarks for profile '$PROFNAME'" \ "please debug me now" >&2 exit $E_PROF } # if we are cleaning all the icons, we can do it now and exit [[ $STRIPALL == yes ]] && { CLEANEDMARKS=$(sed s~[[:space:]]ICON=\"[^\"]*\"~~g "$BMFILE") echo "$CLEANEDMARKS" > "$BMFILE" SIZE2=$(stat --printf=%s "$BMFILE") [[ $OSTYPE == *cygwin* ]] && BMFILE=$(cygpath -m "$BMFILE") printf "$NFOFMT" \ "all icons have been stripped from the file" \ "$BMFILE" \ "$((SIZE1-SIZE2)) bytes were removed, a reduction of $(((SIZE1-SIZE2)*100/SIZE1))%" exit 0 } # find the line number where the PTF starts PTFSTARTLINE=$(sed '/
which closes it has the same indentation level. IDTLVL=$(sed -n "$PTFSTARTLINE{s~^\([[:space:]]*\)<.*~\1~; p; q}" "$BMFILE") PTFENDLINE=$(sed -n "$((PTFSTARTLINE+1))~1!d; \~^$IDTLVL.*~{=; q}" "$BMFILE") # ************************************************************************* # ------------------------------------------------------------------------- # note: this block of commands is what I first used to find the end of the # toolbar folder. It tracks the openings and closings of
tags # instead of just looking at the indentation level of the
's. # It works, and it *might* be safer than the indentation level method, # but it calls sed an awful lot and takes some time. If you want to # use it, just uncomment it and comment out the commands above # between the *********** comments. # ------------------------------------------------------------------------- # # move CURLINE to the line on which the first
opens in the PTF # CURLINE=$(($(sed -n "$PTFSTARTLINE~1!d; \~~{=;q}" "$BMFILE"))) # OPENDLS=1 # # # step through the file, tracking the number of open
's # # when none are open, we've found the end of the PTF # while [[ $OPENDLS -ne 0 ]]; do # CURLINE=$((CURLINE+1)) # sed -n "$CURLINE!d; \~
~q10; \~
~q20" "$BMFILE" # STATE=$? # [[ $STATE -eq 10 ]] && OPENDLS=$((OPENDLS+1)) # [[ $STATE -eq 20 ]] && OPENDLS=$((OPENDLS-1)) # done # PTFENDLINE=$CURLINE #--------------------------------------------------------------------------- # clean the file and exit CLEANEDFILE=$(sed "1,~$((PTFSTARTLINE-1))s~[[:space:]]ICON=\"[^\"]*\"~~g; $((PTFENDLINE+1))~1s~[[:space:]]ICON=\"[^\"]*\"~~g" "$BMFILE") echo "$CLEANEDFILE" > "$BMFILE" SIZE2=$(stat --printf=%s "$BMFILE") [[ $OSTYPE == *cygwin* ]] && BMFILE=$(cygpath -m "$BMFILE") printf "$NFOFMT" \ "all non-toolbar icons have been stripped from the file" \ "$BMFILE" \ "$((SIZE1-SIZE2)) bytes were removed, a reduction of $(((SIZE1-SIZE2)*100/SIZE1))%" exit 0 ################################################################## ## ## ## This script is provided as-is, with no warranty. Use it ## ## however you want. No rights are reserved. ## ## ## ## If you improve the script, please mail your changes to ## ## boxcars at gmx dot net. ## ## ## ##################################################################