Skip to content

Instantly share code, notes, and snippets.

@unhammer unhammer/apertium-ca.sh
Last active Aug 29, 2015

Embed
What would you like to do?
source this file; cd to apertium language pair / language dir by saying "ca some pair" or "ca somelang"
#!/bin/bash
# Save this file to e.g. ~/src/apertium-ca.sh, then put "source
# ~/src/apertium-ca.sh" in ~/.bashrc Now you can type "ca nno" to go
# to languages/apertium-nno and "ca kir kaz" to go to
# nursery/apertium-kaz-kir – even though it's in the other order
export APERTIUM_SVNROOT=$HOME/src/apertium
ca () {
declare -a modes dirs
declare -A pairsdirs langsdirs
modes=( "${APERTIUM_SVNROOT}"/{trunk,incubator,nursery,staging,languages}/apertium-*/modes.xml )
dirs=( ${modes[@]%/modes.xml} )
add_langsdir () {
if [[ ${langsdirs[$1]+isset} ]]; then
langsdirs[$1]="$2\n${langsdirs[$1]}"
else
langsdirs[$1]=$2
fi
}
for dir in "${dirs[@]}"; do
pair=${dir##*apertium-}
if [[ ${pair} = *-* ]]; then
from=${pair%%-*}
to=${pair##*-}
pairsdirs[${from}-${to}]=${dir}
pairsdirs[${to}-${from}]=${dir}
add_langsdir ${from} ${dir}
add_langsdir ${to} ${dir}
# interpret nor as both nob and nno:
if [[ ${from} = nor ]]; then
pairsdirs[nno-${to}]=${dir}
add_langsdir nno ${dir}
pairsdirs[nob-${to}]=${dir}
add_langsdir nob ${dir}
elif [[ ${to} = nor ]]; then
pairsdirs[${from}-nno]=${dir}
add_langsdir nno ${dir}
pairsdirs[${from}-nob]=${dir}
add_langsdir nob ${dir}
fi
else
pairsdirs[${pair}]=$dir
add_langsdir ${pair} ${dir}
fi
done
case $# in
1) cd "${pairsdirs[$1]}";;
2)
if [[ $1 = -l ]]; then
if [[ ${langsdirs[$2]+isset} ]]; then
echo -e "${langsdirs[$2]}"
else
echo "No directories found for language code $2"
return 1
fi
else
cd "${pairsdirs[$1-$2]}"
fi
;;
*)
echo "Usage: "
echo "$0 nob nno cd to the directory containing a pair of nob and nno (even though it's named apertium-nno-nob)"
echo "$0 nno cd to the directory containing apertium-nno"
echo "$0 -l nno list all apertium directories with nno in the name"
return 2
;;
esac
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.