scripts / flightgear / mount.terrafs /
e727c60 a year ago
1 contributor
87 lines | 2.044kb
#!/bin/zsh

terrafs_d=${1:-$HOME/.fgfs/terrafs.d}

osm2city_dirs=(
        Buildings
        Details
        Pylons
        Roads
        Trees
    )

ws20_dirs=(
        Airports
        Models
        Objects
        Terrain
    )

for k in ${(koM)parameters}; do
    if [[ $k =~ .+_dirs && ${(M)parameters[$k]} == array ]]; then
        sceneries+=(${k%_dirs})
        typeset -A ${k%_dirs}_urls
    fi
done

if test -r $0.conf; then
    source $0.conf
else
    echo pas de $0.conf trouvé
    exit 1
fi

for scenery in ${sceneries[@]}; do
    if ! eval "test \${#${scenery}_urls[@]} -gt 0"; then
        echo $scenery erreur
        exit 1
    fi
done

get_url () {
    eval "local hosts=(\${(k)${1}_urls[@]})"
    echo ${hosts[$(( $RANDOM % ${#hosts[@]} + 1 ))]}
}

remove_url () {
    eval "unset \"${1}_urls[$2]\""
}

get_largest () {
    eval "local _1=\${#${1}_urls[@]}"
    eval "local _2=\${#${2}_urls[@]}"
    if test $_1 -gt $_2; then
        echo largest=$1
        echo smallest=$2
    else
        echo largest=$2
        echo smallest=$1
    fi
}

mount_dirs () {
    local dir=
    eval "local oserver=https://\${${1}_url}/\${${1}_urls[\${${1}_url}]}"
    eval "local dirs=(\${${1}_dirs[@]})"
    for dir in ${dirs[@]}; do
        mkdir -p $HOME/.fgfs/terrafs.d/$dir
        printf "- %-10s " $dir && $HOME/.fgfs/terrafs -oserver=$oserver/$dir $terrafs_d/$dir && echo "ok" || echo "ERROR"
    done
}

if test $(grep -Ec "^terrafs +$terrafs_d" /proc/mounts) -ne $(( ${#ws20_dirs[@]} + ${#osm2city_dirs[@]} )); then
    eval $(get_largest ${sceneries[@]})
    eval "${largest}_url=$(get_url $largest)"
    eval "remove_url $smallest \$${largest}_url"
    eval "${smallest}_url=$(get_url $smallest)"

    for m in ${sceneries[@]}; do
        eval "printf 'montage %s depuis %s\n' ${(U)m} \$${m}_url"
        mount_dirs $m
    done
else
    echo "démontage $terrafs_d"
    for dir in ${osm2city_dirs[@]} ${ws20_dirs[@]}; do
        printf "- %-10s " $dir && fusermount -u $terrafs_d/$dir && echo "ok" || echo "ERROR"
    done
fi