#!/bin/bash # $HeadURL$ $LastChangedRevision$ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Configurable stuff # Other globals main() { local MY_ARGS # Defaults for options # Process options miniade_process_options --help-handler=help MY_ARGS "$@" && set -- "${MY_ARGS[@]}" # Process arguments [ $# = 1 ] || miniade_bad_usage type encode > /dev/null 2>&1 || miniade_internal "encode: command not found (hint: is PATH set correctly?)" ENCODE_OUTPUT=$(encode --decode "$1") || miniade_internal "encode: failed (hint: see messages above)" eval set -- "$ENCODE_OUTPUT" [ $# -eq 3 ] || miniade_bad_usage SYMLINK="$1" TARGET="$2" MKDIR_FLAG="$3" miniade_debug 10 "main: SYMLINK=$SYMLINK, TARGET=$TARGET, MKDIR_FLAG=$MKDIR_FLAG" # Sanity checks and derivations [[ $MKDIR_FLAG =~ ^(true|false)$ ]] || miniade_error "$MKDIR_FLAG: invalid value for MKDIR_FLAG" [[ $SYMLINK =~ ^/ ]] || miniade_error "$SYMLINK: not absolute" ! [[ $TARGET =~ ^/ ]] || miniade_error "$SYMLINK: not relative" # Guts # Symlink if [ ! -h $SYMLINK -a ! -e $SYMLINK ]; then ln -s $TARGET $SYMLINK elif { [ ! -h $SYMLINK -a -e $SYMLINK ]; }; then miniade_error "$SYMLINK: exists but is not a symlink (hint: fix this manually)" elif { [ -h $SYMLINK ] && [ $(readlink $SYMLINK) != $TARGET ]; }; then miniade_warning "$SYMLINK: exists and is a symlink but points to $(readlink $SYMLINK) instead of $TARGET; removing ..." rm -f $SYMLINK ln -s $TARGET $SYMLINK fi # Mkdir if $MKDIR_FLAG; then ABSOLUTE_TARGET=$(realpath -m $SYMLINK) if [ ! -h $ABSOLUTE_TARGET -a ! -e $ABSOLUTE_TARGET ]; then mkdir -p $ABSOLUTE_TARGET elif [ -h $ABSOLUTE_TARGET -o \( -e $ABSOLUTE_TARGET -a ! -d $ABSOLUTE_TARGET \) ]; then miniade_error "$ABSOLUTE_TARGET: exists but is not a directory (hint: fix this manually)" fi else # It'd be nice to do this "if link's targer doesn't exist then error" test before we even create the # symlink, but $ABSOLUTE_TARGET depends on the realpath command and I suspect that that won't work # in the above call unless $SYMLINK actually exists. But perhaps it would e..g: # # lagane# realpath /etc/a # where /etc/a does not exist! # /etc/a # lagane# # # Nonetheless, for the time being, we do it this way: undo the just created symlink # and return an error. if [ ! -e $ABSOLUTE_TARGET ]; then rm -f $SYMLINK miniade_error "$ABSOLUTE_TARGET: does not exist and you didn't request me to create it (as a directory); symlink has been removed" fi fi } help() { local PROGNAME miniade_get_progname PROGNAME echo "Usage: $PROGNAME [ ] @@