#!/bin/bash

### Copyright notice at the bottom of this file ###

# $Id: asm2install,v 1.8 2012/01/11 21:00:43 adamm Exp adamm $
#
# $Source: /home/adamm/src/asm2install/RCS/asm2install,v $


export PROG=$(basename $0)


main() {
    # Special handling for --help, --man
    if [ "x$1" == "x--man" ] || [ "x$1" == "x--help" ] ; then
	manpage
	exit 0
    fi

    dir=$HOME/bin
    mode=0700

    while getopts "d:l:m:" opt 2> /dev/null ; do
	case "$opt" in
	\?)    usage 1>&2 ; exit 1    ;;
	d)     dir=$OPTARG            ;;
	l)     links=$OPTARG          ;;
	m)     mode=$OPTARG           ;;
	esac
    done

    shift $(($OPTIND - 1))
    if [ $# -ne 1 ] ; then
	usage 1>&2
	exit 1
    else
	name=$1
    fi

    rm -f $dir/$name
    if [ "$links" ] ; then
	for link in $(echo "$links" | tr ',' ' ') ; do
	    rm -f $dir/$link
	done
    fi

    cp $name $dir/$name

    if [ "$links" ] ; then
	for link in $(echo "$links" | tr ',' ' ') ; do
	    ln $dir/$name $dir/$link
	done
    fi

    chmod $mode $dir/$name
} # main


manpage() {
    cat - <<__EOF
NAME
    asm2install -- install a program

SYNOPSIS
    asm2install [ -d dir ] [ -l link[,...] ] [ -m mode ] name

DESCRIPTION
    asm2install installs a program in a directory, sets the permissions,
    and if directed, creates links to the program (in the same directory
    where the program is installed).

OPTIONS
    -f dir
	the directory in which to install the program; default is
	$HOME/bin

    -l link[,...]
	a comma-separated list of (hard) link names; links will be
	created in the same directory as the program

    -m mode
	an octal permission value (as used by chmod(1))

    --help
    --man
	show this man page

SEE ALSO
    chmod(1), install(1)

AUTHOR
    Adam Moskowitz <adamm@menlo.com>
__EOF
} # manpage


usage() {
    printf "Usage: %s [ %s ] [ %s ] [ %s ] name\n" \
	$PROG \
	"-d dir" "-l link[,...]" "-m mode"
} # usage


main "$@"


###
### Copyright, 2012, Adam Moskowitz. All rights reserved.
###
### Any redistribution of this software must retain the above copyright
### notice, this list of conditions, and the following disclaimer.
###
### Without specific prior written permission from the copyright holder,
### you may not charge a fee for the redistribution of this software.
###
### All other redistribution and use is hereby permitted.
###
### This software is provided "as is" and without any express or implied
### warranties, including, without limitation, the implied warranties of
### merchantability and fitness for a particular purpose.
###
