remote /.bash_functions

spath="~/bin"

# Untar an archive
function untar {
    if [[ $# != 1 ]]; then
        echo "Usage: untar "
        echo "    This will untar tar, tar.gz or tar.bz2 archives"
        echo "    and will change the CWD to the extracted folder"
        return 1
    fi

    archive=$1
    if [[ ${archive/.tar.gz//} != $archive ]]; then
        tar -xvzf $archive

        dir=`tar -tf $archive | head -1`
        if [[ -d $dir ]]; then
            cd $dir
        fi

        return $?

    elif [[ ${archive/.tar.bz2//} != $archive ]]; then
        tar -xvjf $archive
        return $?

    elif [[ ${archive/.tar//} != $archive ]]; then
        tar -xvf $archive
        return $?
    fi
}

# Runs configure make and make install with dumb error handling
function install_source {
    if [[ $# != 1 ]]; then
        echo "Using $PWD as source location."
    elif [[ $# == 2 ]]; then
        cd $2
    fi

    if [[ ! -x ./configure ]]; then
        echo "Could not find configure script in $PWD!"
        return 1
    fi

    ./configure
    if [[ $? != 0 ]]; then
        echo -e "\nA problem occured with the automated configuration.\n"
        return 1
    fi

    make
    if [[ $? != 0 ]]; then
        echo -e "\nA problem happened while making this program.\n"
        return 1
    fi

    make install
    if [[ $? != 0 ]]; then
        echo -e "\nA problem happened while installing the program.\n"
        return 1
    fi
}

# Uses the two previous functions (untar & install_source)
function install_archive {
    if [[ $# != 1 ]]; then
        echo "Usage: install_archive "
        echo "    This will untar and install a program from source"
        return 1
    fi

    archive=$1
    
    untar $archive
    install_source

    return $?
}

# install a file in $spath
function sinst { 
    chmod +x $1
    mv ${1} $spath
}