#!/bin/sh
# Setup functions and settings used in subdirectories.
#
# Their setup.sh script sources this file.

# Copyright (C) 2009-2018  Simon Ruderich

# csh gives the error "Unknown colorls variable `su'." when used with newer
# options supported by Zsh or GNU ls.
unset LS_COLORS


# Check if the given program is installed. `type` is portable, `which` is not.
installed() {
    type "$1" >/dev/null 2>&1
}
# Get the path of the given program. Thanks to Gilles on [1] (read on
# 2013-03-10) for the PATH-walking idea. `which` is not portable and `type`
# has no well-formed output format.
#
# [1]: http://unix.stackexchange.com/questions/4988/how-do-i-test-to-see-if-an-application-exists-in-path/4991
installed_path() {
    test -z "$1" && return 1

    # Keep IFS change local.
    (
        IFS=:
        # Walk PATH.
        for directory in $PATH; do
            if test -x "$directory/$1"; then
                printf '%s\n' "$directory/$1"
                return 0
            fi
        done

        return 1
    )
}

# usage: cmd_i <cmd> ... <file>
#
# run <cmd> with all arguments (including the last file) and write the result
# to the temporary file <file>.tmp and then rename that file to <file>.
# this cant be done in-place (e.g. cmd <file >file) because it truncates the file.
cmd_i() {
    # get last argument.
    last=
    for x; do
        last="$x"
    done

    "$@" >"$last".tmp
    mv "$last".tmp "$last"
}

# usage: sed_i ... <file>
#
# sed -i is not portable due to different implementations. see cmd_i.
sed_i() {
    cmd_i sed "$@"
}
grep_i() {
    cmd_i grep "$@"
}

# usage: perl_line_filter <cmd> ...
#
# run the perl command cmd on each line before printing it.
perl_line_filter() {
    cmd="$1"
    shift

    # Can't use -pe because it uses <> which treats the arguments as files.
    perl -e "use strict; use warnings; while (<STDIN>) { $cmd; print; }" "$@"
}

# usage: simple_cpp <FIRST> <SECOND> .. -- <replacement-for-first> ...
#
# replaces each FIRST (on word boundaries) with <replacement-for-first>
# like a simple cpp replacement.
simple_cpp() {
    cmd='my $i = 0;'

    for x; do
        shift

        if test x"$x" = x--; then
            break
        fi

        cmd="$cmd s/\\b$x\\b/\$ARGV[\$i]/g; \$i++;"
    done

    perl_line_filter "$cmd" -- "$@"
}

# print the current OS. the following OS are supported at the moment:
#
# - Debian (debian)
# - Gentoo (gentoo)
# - Solaris/OpenSolaris (sun)
# - FreeBSD (freebsd)
#
# if an unsupported OS is used an error is printed.
os() {
    if test -f /etc/debian_version; then
        echo debian
    elif test -f /etc/gentoo-release; then
        echo gentoo
    elif test x"$(uname)" = xSunOS; then
        echo sun
    elif test x"$(uname)" = xFreeBSD; then
        echo freebsd
    else
        echo 'unsupported OS!' >&2
        return 1
    fi
}

# creates a symbolic link for file $1 in dirname of $2 with name of basename
# $2.
#
# `./link.sh example ~/.examplerc` creates a symbolic link to example
# (wherever it is located) in ~/ named .examplerc.
link() {
    local pwd base source target >/dev/null 2>&1 || true

    # Get all necessary paths.
    pwd=$(pwd)
    base=$(printf '%s' "$2" | sed "s|~|$HOME|") # expand ~, some sh don't do it
    base=$(dirname "$base")
    source=$(printf '%s' "$pwd/$1" | sed "s|$base/||")
    target=$(basename "$2")

    # Go to the directory where the link is going to be created.
    cd "$base" || return 1

    # Abort if the target file exists and is no symbolic link. Prevents
    # overwriting real files.
    if test -e "$target" && test ! -h "$target"; then
        printf 'link(): target "%s" exists already and is no symbolic link!\n' \
               "$target" >&2
        exit 1
    fi

    # Make sure the source exists.
    if test ! -e "$source"; then
        printf 'link(): source "%s" does not exist!\n' "$source" >&2
        exit 1
    fi

    # Create the new symbolic link; remove the old one if necessary.
    printf 'link(): linking "%s" to "%s"\n' "$source" "$target"
    rm -f "$target"
    ln -s "$source" "$target"

    # Go back to the directory where we were before.
    cd "$pwd" || return 1
}

# generate a file from a source file using a given command.
# a warning not to edit it is automatically added to the created file.
#
# usage: generate <file> <extension> <cmd..>
#
# if an empty extension is provided, the file is modified in-place
# (through a temporary file).
generate() {
    local file      >/dev/null 2>&1 || true
    local file_tmp  >/dev/null 2>&1 || true
    local extension >/dev/null 2>&1 || true

    # get command and target file.
    file="$1"
    extension="$2"
    shift
    shift

    if test -z "$extension"; then
        file_tmp="$file.tmp"
    else
        # we only need this message if we generate a new file.
        printf '%s: generating from "%s" (%s)\n' \
            "$file" "$file$extension" "$1"

        {
            echo '###################################'
            echo '# WARNING! DO NOT EDIT THIS FILE! #'
            echo '###################################'
            echo
            printf '# It was generated from "%s" on %s.\n' \
                "$file$extension" "$(date)"
            echo
        } > "$file"

        file_tmp="$file"
    fi

    # generate $file from $file$extension using the given command.
    "$@" <"$file$extension" >>"$file_tmp"

    if test -z "$extension"; then
        mv "$file_tmp" "$file"
    fi
}

