# #!/bin/bash ############################################################################### #Name: desktop-files-antix #Version: 1.0 #Author: Robin.antiX #Purpose: Switch between default .desktop files set and special antiX updated and translated version of the file set. #License: gplv3 ############################################################################### TEXTDOMAINDIR=/usr/share/locale TEXTDOMAIN=desktop-files-antix basefolder="/usr/share/antiX-desktop-files/applications" user_basefolder=".local/share/applications" userfolder="$(grep "$(logname)" "/etc/passwd" | cut -d: -f6)" backup_suffix='.antiX.bak' function usage { a21="antix-desktop-files" a22="/usr/share/antiX-desktop-files" a23=".local/share/applications" echo -e $"Usage:"" $(basename "$1") [""Options""]\n" echo -e $"This script switches beween the default .desktop file set and the\n\ antiX .desktop file set provided by the $a21 package.\n\ Once installed on a system the antiX .desktop file set resides in\n\ the $a22 folder, and this script creates\n\ or removes symbolic links to the hidden $a23\n\ subfolder within the user's home folder and lets the users menu get\n\ refreshed. Existing files of the same name present in users folder are\n\ saved to backup copies. The unset options restore these backup copies\n\ while removing the symbolic links created by the set options.""\n" echo -e $"Options"":" echo -e " --set-single "$"or"" -s "$"Switch single user to antiX .desktop files set." echo -e " --unset-single "$"or"" -u "$"Switch single user to default .desktop files set." echo -e " --set-global "$"or"" -sg "$"Switch all system users to antiX .desktop files\n set (needs sudo or root)." echo -e " --unset-global "$"or"" -ug "$"Switch all system users to default .desktop files\n set (needs sudo or root)." echo -e " --help "$"or"" -h "$"Display usage information""\n" exit 0 } # usage function antix_set { local the_path local use_name local new_name the_path="$1" use_name="$2" new_name="${the_path}/${user_basefolder}/$(basename "$i")" for i in "$basefolder/"*; do if [ -e "${new_name}" ]; then if ! [ -h "${new_name}" ]; then mv "${new_name}" "${new_name}${backup_suffix}" if [ "$(whoami)" == "root" ]; then chown "${use_name}:${use_name}" "${new_name}${backup_suffix}"; fi else unlink "${new_name}" fi fi ln -s "$i" "${new_name}" if [ "$(whoami)" == "root" ]; then chown -h "${use_name}:${use_name}" "${new_name}"; fi done } # antix_set function default_set { local the_path local use_name local new_name the_path="$1" use_name="$2" new_name="${the_path}/${user_basefolder}/$(basename "$i")" for i in "$basefolder/"*; do if [ -h "${new_name}" ]; then unlink "${new_name}" fi done for k in "${the_path}/${user_basefolder}/"*"${backup_suffix}"; do [ -f "$k" ] || break # TODO/FIXME: do you really want to break (folder found) or simply continue in the loop (to next item that may be a file? mv "$k" "$(echo "$k" | sed "s@\\(..*\\)${backup_suffix}\$@\\1@")" || exit 1 # ALTERNATIVE: #mv "$k" "${k%${backup_suffix}}" || echo "Failed to copy [${k}] to [${k%${backup_suffix}}]" # Verified that '%' cuts out the last occurence only! #mv "$k" "${k%${backup_suffix}}" || exit 1 # Verified that '%' cuts out the last occurence only! if [ "$(whoami)" == "root" ]; then chown "${use_name}:${use_name}" "$(echo "$k" | sed "s@\\(..*\\)${backup_suffix}\$@\\1@")" fi # ALTERNATIVE: #if [ "$(whoami)" == "root" ]; then # chown "${use_name}:${use_name}" "${k%${backup_suffix}}" #fi done } # default_set function set_single { echo $"Switching to antiX .desktop files set." antix_set "$userfolder" "$(logname)" desktop-menu --write-out-single exit 0 } # set_single function unset_single { echo $"Switching to default .desktop files set." default_set "$userfolder" "$(logname)" desktop-menu --write-out-single exit 0 } # unset_single function set_global { local p echo $"Switching to antiX .desktop files set." checkroot "--set-global" get_users p=0 for m in "${userfolders[@]}"; do antix_set "$m" "${usernames[$p]}" ((p++)) done desktop-menu --write-out-global exit 0 } # set_global function unset_global { local p echo $"Switching to default .desktop files set." checkroot "--unset-global" get_users p=0 for m in "${userfolders[@]}"; do default_set "$m" "${usernames[$p]}" ((p++)) done desktop-menu --write-out-global exit 0 } # unset_global function get_users { k=0 while read -r i; do if [ "$(echo "$i" | cut -d: -f2)" != "*" ] && [ "$(echo "$i" | cut -d: -f1)" != "root" ]; then usernames[$k]="$(echo "$i" | cut -d: -f1)" userfolders[$k]="$(grep "$(echo "$i" | cut -d: -f1)" "/etc/passwd" | cut -d: -f6)" ((k++)) fi done < "/etc/shadow" } # get_users function checkroot { if ! [ "$(whoami)" == "root" ]; then echo $"This option needs sudo or root:"" $1" exit 1 fi } # checkroot # Iterate over the passed arguments: bad_args=() count_it=0 for opt in "$@"; do case "$opt" in --help|-h) usage "$0" ;; --set-single|-s) set_single ;; --unset-single|-u) unset_single ;; --set-global|-sg) set_global ;; --unset-global|-ug) unset_global ;; #*) echo $"Unknown option:"" $opt" && exit 1 ;; # ALTERNATIVE (print the usage when wrong argument(s): *) bad_args[${count_it}]="$opt" && ((count_it++)) ;; esac done # Inform /of bad arguments passed, if any: if [[ "${count_it}" -gt "0" ]]; then echo "${#bad_args[@]} bad option passed [${bad_args[@]}] to the script [$0]" usage "$0" fi