Skip to content

Commit

Permalink
Merge pull request #42 from jinjorge/shellcheck_changes
Browse files Browse the repository at this point in the history
Update install script with shellcheck recommended changes
  • Loading branch information
warpling authored Apr 9, 2019
2 parents ea8b1d9 + 56e0fe3 commit 9f1a74f
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions scripts/macmoji
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

cd "$(dirname "$(realpath "$0")")";

DATE=`date +%s`
USER=`id -un`
USER_DICTIONARY_FOLDER=`ls -td -- ~/Library/Dictionaries/CoreDataUbiquitySupport/"${USER}"~*/UserDictionary/* | head -n 1`
DATE=$(date +%s)
USER=$(id -un)
USER_DICTIONARY_FOLDER=$(ls -td -- ~/Library/Dictionaries/CoreDataUbiquitySupport/"${USER}"~*/UserDictionary/* | head -n 1)
USER_DICTIONARY="${USER_DICTIONARY_FOLDER}/store/UserDictionary.db"
GLOBAL_PREFERENCES="/Users/${USER}/Library/Preferences/.GlobalPreferences.plist"
PLIST_BUDDY="/usr/libexec/PlistBuddy"
SQLITE="/usr/bin/sqlite3"
EMOJI_SUBSTITUTIONS="../emoji substitutions.plist"
COUNT=`${PLIST_BUDDY} -c 'Print' "${EMOJI_SUBSTITUTIONS}" | grep 'Dict' | wc -l`
COUNT=$(${PLIST_BUDDY} -c 'Print' "${EMOJI_SUBSTITUTIONS}" | grep -c 'Dict')

contains () {
local e
local i=0
declare -a arr=("${@:3}")
for e in "${arr[@]}"; do
if [[ "$e" == "$1" ]]; then
if [[ "${arr[$(($i + 1))]}" == "$2" ]]; then
if [[ "${arr[$i + 1]}" == "$2" ]]; then
return 0
fi
fi
Expand All @@ -32,20 +32,20 @@ contains () {
install() {
echo "Installing Macmoji text replacements..."

PK_LAST=`${SQLITE} "${USER_DICTIONARY}" "SELECT Z_PK FROM ZUSERDICTIONARYENTRY ORDER BY Z_PK DESC LIMIT 1;"`
PK_LAST=$(${SQLITE} "${USER_DICTIONARY}" "SELECT Z_PK FROM ZUSERDICTIONARYENTRY ORDER BY Z_PK DESC LIMIT 1;")

pk=$(($PK_LAST))
cnt=`echo -e "${COUNT}" | tr -d '[[:space:]]'`
pk=$PK_LAST
cnt=$(echo -e "${COUNT}" | tr -d '[[:space:]]')
plist=''
sql=''

while [ $cnt -gt 0 ]; do
let cnt-=1
let pk+=1
replace=`${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}"`
with=`${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}"`
while [ "${cnt}" -gt 0 ]; do
((cnt-=1))
((pk+=1))
replace=$(${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}")
with=$(${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}")
plist+="{on=1;replace=\"${replace}\";with=\"${with}\";},"
sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES($((${pk})),1,1,0,0,0,0,${DATE},NULL,NULL,NULL,NULL,NULL,\"${with}\",\"${replace}\",NULL);"
sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES(${pk},1,1,0,0,0,0,${DATE},NULL,NULL,NULL,NULL,NULL,\"${with}\",\"${replace}\",NULL);"
done

$SQLITE "${USER_DICTIONARY}" "${sql}"
Expand All @@ -56,28 +56,28 @@ uninstall() {
echo "Uninstalling Macmoji text replacements..."

seen=()
cnt=`echo -e "${COUNT}" | tr -d '[[:space:]]'`
cnt=$(echo -e "${COUNT}" | tr -d '[[:space:]]')

while [ $cnt -gt 0 ]; do
let cnt-=1
let pk+=1
replace=`${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}"`
with=`${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}"`
while [ "${cnt}" -gt 0 ]; do
((cnt-=1))
((pk+=1))
replace=$(${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}")
with=$(${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}")
arr=("$replace" "$with")
seen+=("${arr[@]}")
sql+="DELETE FROM 'ZUSERDICTIONARYENTRY' WHERE ZPHRASE = \"${with}\" AND ZSHORTCUT = \"${replace}\";"
done
$SQLITE "${USER_DICTIONARY}" "${sql}"

EXISTING_COUNT=`${PLIST_BUDDY} -c 'Print :NSUserDictionaryReplacementItems' "${GLOBAL_PREFERENCES}" | grep 'Dict' | wc -l`
existing_cnt=`echo -e "${EXISTING_COUNT}" | tr -d '[[:space:]]'`
EXISTING_COUNT=$(${PLIST_BUDDY} -c 'Print :NSUserDictionaryReplacementItems' "${GLOBAL_PREFERENCES}" | grep -c 'Dict')
existing_cnt=$(echo -e "${EXISTING_COUNT}" | tr -d '[[:space:]]')

while [ $existing_cnt -gt 0 ]; do
let existing_cnt-=1
replace=`${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$(($existing_cnt)):replace" "${GLOBAL_PREFERENCES}"`
with=`${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$(($existing_cnt)):with" "${GLOBAL_PREFERENCES}"`
if contains $replace $with "${seen[@]}"; then
${PLIST_BUDDY} -c "Delete :NSUserDictionaryReplacementItems:$(($existing_cnt))" "${GLOBAL_PREFERENCES}"
while [ "$existing_cnt" -gt 0 ]; do
((existing_cnt-=1))
replace=$(${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$existing_cnt:replace" "${GLOBAL_PREFERENCES}")
with=$(${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$existing_cnt:with" "${GLOBAL_PREFERENCES}")
if contains "$replace" "$with" "${seen[@]}"; then
${PLIST_BUDDY} -c "Delete :NSUserDictionaryReplacementItems:$existing_cnt" "${GLOBAL_PREFERENCES}"
fi
done
}
Expand Down

0 comments on commit 9f1a74f

Please sign in to comment.