Skip to content

Commit

Permalink
install.sh: Improve the installation script
Browse files Browse the repository at this point in the history
- Simplified the basic installation code to improve the readability.
- Added options such as --dest, --name, --color, --size, and --help.
- A lot of codes/ideas/descriptions are based on @n3oxmind work!
- This is still incomplete and a few bugs remain.

TODO/BUGS:

- Needs to update 'change_color.sh' accordingly.
- Can't use 'standard' instead of 'normal' as an argument of --size option... Why?
- When the wrong argument(s) is typed to --color/--size, incorrect error message
  will be shown. (e.g. `./install.sh --color lighttt`) Even if we rewrite "*" to *,
  the script does not start. :(
  • Loading branch information
nana-4 committed Jan 11, 2018
1 parent 6fe2f07 commit 960249d
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 232 deletions.
19 changes: 0 additions & 19 deletions TODO.md
Expand Up @@ -4,25 +4,6 @@

- Use `make` or `meson` for building? (`help wanted`)

- Improve `install.sh` script? (`help wanted`)

Currently, `install.sh` allows such arguments (thanks to @actionless):

```sh
# This will install only normal color variant of compact theme into ~/.themes dir as MyTheme,
# ie ~/.themes/MyTheme-compact
COLOR_VARIANTS="," SIZE_VARIANTS="-compact" THEME_DIR_BASE=~/.themes/MyTheme ./install.sh
```

My alternative idea is (more like shell option):

```sh
# Note that this is just a draft.
./install.sh --color - --size compact --dir ~/.themes --name MyTheme
# and/or shorter
./install.sh -c - -s compact -d ~/.themes -n MyTheme
```

## Supports

- Firefox theme ([#78](../../issues/78))
Expand Down

2 comments on commit 960249d

@n3oxmind
Copy link
Contributor

@n3oxmind n3oxmind commented on 960249d Jan 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand from your script that your trying to parse the arguments manually without getopt or similar tools.
and since you're using a nested case, arguments handling will get complex. I already fixed your code regarding the parsing. see the FIX:

#!/bin/bash
#set -ueo pipefail
#set -x

DEST_DIR=/usr/share/themes
THEME_NAME=Materia
COLOR_VARIANTS=('' '-dark' '-light')
SIZE_VARIANTS=('' '-compact')

GTK_VERSIONS=('3.18' '3.20' '3.22')
GS_VERSIONS=('3.18' '3.20' '3.22' '3.24' '3.26')
LATEST_GS_VERSION=${GS_VERSIONS[@]: -1}

Set a proper gnome-shell theme version

if [[ $(which gnome-shell 2> /dev/null) ]]; then
CURRENT_GS_VERSION=$(gnome-shell --version | cut -d ' ' -f 3 | cut -d . -f -2)
for version in "${GS_VERSIONS[@]}"; do
if (( $(echo "$CURRENT_GS_VERSION <= $version" | bc) )); then
GS_VERSION=${version}
break
elif (( $(echo "$CURRENT_GS_VERSION > $LATEST_GS_VERSION" | bc) )); then
GS_VERSION=${LATEST_GS_VERSION}
break
fi
done
else
GS_VERSION=${LATEST_GS_VERSION}
fi

usage() {
printf "%s\n" "Usage: $0 [OPTIONS...]"
printf "\n%s\n" "OPTIONS:"
printf " %-25s%s\n" "-d, --dest DIR" "Specify theme destination directory (Default: ${DEST_DIR})"
printf " %-25s%s\n" "-n, --name NAME" "Specify theme name (Default: ${THEME_NAME})"
printf " %-25s%s\n" "-c, --color VARIANTS..." "Specify theme color variants [standard|dark|light] (Default: All variants)"
printf " %-25s%s\n" "-s, --size VARIANT" "Specify theme size variant [standard|compact] (Default: All variants)"
printf " %-25s%s\n" "-h, --help" "Show this help"
printf "\n%s\n" "INSTALLATION EXAMPLES:"
printf "%s\n" "Install all theme variants into ~/.themes"
printf " %s\n" "$0 --dest ~/.themes"
printf "%s\n" "Install standard theme variant only"
printf " %s\n" "$0 --color standard --size standard"
printf "%s\n" "Install specific theme variants with different name into ~/.themes"
printf " %s\n" "$0 --dest ~/.themes --name MyTheme --color light dark --size compact"
}

install() {
local dest=${1}
local name=${2}
local color=${3}
local size=${4}

[[ ${color} == '-dark' ]] && local ELSE_DARK=${color}
[[ ${color} == '-light' ]] && local ELSE_LIGHT=${color}

local THEME_DIR=${dest}/${name}${color}${size}

[[ -d ${THEME_DIR} ]] && rm -rf ${THEME_DIR}

echo "Installing '${THEME_DIR}'..."

mkdir -p ${THEME_DIR}
cp -ur COPYING ${THEME_DIR}
cp -ur src/index${color}${size}.theme ${THEME_DIR}/index.theme

mkdir -p ${THEME_DIR}/chrome
cp -ur "src/chrome/Materia${color} Theme.crx" ${THEME_DIR}/chrome #/${name}${color} Theme.crx
cp -ur "src/chrome/Materia${ELSE_DARK} Scrollbars.crx" ${THEME_DIR}/chrome #/${name}${ELSE_DARK} Scrollbars.crx

mkdir -p ${THEME_DIR}/gnome-shell
cp -ur src/gnome-shell/${GS_VERSION}/*.svg ${THEME_DIR}/gnome-shell
cp -urL src/gnome-shell/${GS_VERSION}/{extensions,noise-texture.png,pad-osd.css} ${THEME_DIR}/gnome-shell
cp -urL src/gnome-shell/${GS_VERSION}/assets${ELSE_DARK} ${THEME_DIR}/gnome-shell/assets
cp -ur src/gnome-shell/${GS_VERSION}/gnome-shell${color}${size}.css ${THEME_DIR}/gnome-shell/gnome-shell.css
glib-compile-resources
--sourcedir=${THEME_DIR}/gnome-shell
--target=${THEME_DIR}/gnome-shell/gnome-shell-theme.gresource
src/gnome-shell/${GS_VERSION}/gnome-shell-theme.gresource.xml

mkdir -p ${THEME_DIR}/gtk-2.0
cp -ur src/gtk-2.0/{apps.rc,hacks.rc,main.rc} ${THEME_DIR}/gtk-2.0
cp -ur src/gtk-2.0/assets${ELSE_DARK} ${THEME_DIR}/gtk-2.0/assets
cp -ur src/gtk-2.0/gtkrc${color} ${THEME_DIR}/gtk-2.0/gtkrc

mkdir -p ${THEME_DIR}/gtk-common
cp -ur src/gtk-3.0/gtk-common/assets ${THEME_DIR}/gtk-common

for version in "${GTK_VERSIONS[@]}"; do
if [[ ${version} == '3.18' ]]; then
mkdir -p ${THEME_DIR}/gtk-3.0
cp -ur src/gtk-3.0/${version}/assets ${THEME_DIR}/gtk-3.0
cp -ur src/gtk-3.0/${version}/gtk${color}.css ${THEME_DIR}/gtk-3.0/gtk.css
[[ ${color} != '-dark' ]] &&
cp -ur src/gtk-3.0/${version}/gtk-dark.css ${THEME_DIR}/gtk-3.0/gtk-dark.css
else
mkdir -p ${THEME_DIR}/gtk-${version}
cp -ur src/gtk-3.0/${version}/assets ${THEME_DIR}/gtk-${version}
cp -ur src/gtk-3.0/${version}/gtk${color}${size}.css ${THEME_DIR}/gtk-${version}/gtk.css
[[ ${color} != '-dark' ]] &&
cp -ur src/gtk-3.0/${version}/gtk-dark${size}.css ${THEME_DIR}/gtk-${version}/gtk-dark.css
fi
done

mkdir -p ${THEME_DIR}/metacity-1
cp -ur src/metacity-1/assets ${THEME_DIR}/metacity-1
cp -ur src/metacity-1/metacity-theme-2${ELSE_LIGHT}.xml ${THEME_DIR}/metacity-1/metacity-theme-2.xml
cp -ur src/metacity-1/metacity-theme-3${ELSE_LIGHT}.xml ${THEME_DIR}/metacity-1/metacity-theme-3.xml

mkdir -p ${THEME_DIR}/unity
cp -ur src/unity/{.svg,.png,dash-widgets.json} ${THEME_DIR}/unity
cp -ur src/unity/assets${ELSE_LIGHT} ${THEME_DIR}/unity/assets

mkdir -p ${THEME_DIR}/xfwm4
cp -ur src/xfwm4/{*.svg,themerc} ${THEME_DIR}/xfwm4
cp -ur src/xfwm4/assets${ELSE_LIGHT} ${THEME_DIR}/xfwm4/assets
}

while [[ $# -gt 0 ]]; do
case "${1}" in
-d|--dest)
dest="${2}"
if [[ ! -d "${dest}" ]]; then
echo "ERROR: Destination directory does not exist."
exit 1
fi
shift 2
;;
-n|--name)
name="${2}"
shift 2
;;
-c|--color)
shift
for variant in "${@}"; do
case "${variant}" in
standard)
colors+=("${COLOR_VARIANTS[0]}")
shift
;;
dark)
colors+=("${COLOR_VARIANTS[1]}")
shift
;;
light)
colors+=("${COLOR_VARIANTS[2]}")
shift
;;
-|--)
break
;;
) # FIXME: Why doesn't it work without quotes?
echo "ERROR: Unrecognized color variant '$1'."
echo "Try '$0 --help' for more information."
exit 1
;;
esac
done
;;
-s|--size)
shift
for variant in "${@}"; do
case "${variant}" in
# FIXME: Why cannot it use the "standard" string as an argument?
# standard)
standard)
sizes+=("${SIZE_VARIANTS[0]}")
shift
;;
compact)
sizes+=("${SIZE_VARIANTS[1]}")
shift
;;
-
|--*)
break
;;
*) # FIXME: Why doesn't it work without quotes?
echo "ERROR: Unrecognized size variant '$1'."
echo "Try '$0 --help' for more information."
exit 1
;;
esac
done
;;
-h|--help)
usage
exit 0
;;
*)
echo "ERROR: Unrecognized installation option '$1'."
echo "Try '$0 --help' for more information."
exit 1
;;
esac
done

if [[ ! -w "${dest:-${DEST_DIR}}" ]]; then
echo "Please run as root."
exit 1
fi

for color in "${colors[@]:-${COLOR_VARIANTS[@]}}"; do
for size in "${sizes[@]:-${SIZE_VARIANTS[@]}}"; do
install "${dest:-${DEST_DIR}}" "${name:-${THEME_NAME}}" "${color}" "${size}"
done
done

echo
echo Done.

@nana-4
Copy link
Owner Author

@nana-4 nana-4 commented on 960249d Jan 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@n3oxmind
Wow, it seems everything go well!!
Thank you very much for your advice!

Please sign in to comment.