#!/bin/sh
#
# $Id: pcb-print,v 1.2 2005/08/05 22:31:27 danmc Exp $
#
#                             COPYRIGHT
# 
#   PCB, interactive printed circuit board design
#   Copyright (C) 2005 David Baird
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
# 
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 

# get some environment variables with defaults
#

# the pcb executible script
PCB=${PCB:-pcb}

usage() {
cat << EOF
Usage: $0 [Options] --input filename.pcb
  -h, --help                      Print this help message

  -v, --verbose                   Verbose operation

  --input filename.pcb            File to read PCB data from

  File generation:
  --prefix prefix                 prefix (defaults to basename of filename)
  --output-format (eps|gbr)       Generate EPS or Gerber output (default: eps)
  --use-dos-filenames             Use DOS 8.3 filenames

  Image transformation:
  --scale scaling-factor          Factor to resize an image (default: 1.0)
  --mirror                        Produce a mirrored/reversed image
  --rotate
  --color
  --invert
  --outline

  Add extra data to images:
  --add-alignment
  --add-drill-helper

Example:
  $0 --mirror --prefix output_mirrored --input mypcbfile.pcb
EOF
}

# Set default values:
input_file="bad.input"
input_file_set=0
#
prefix="bad.prefix"
prefix_set=0
device=1
scale=1.0
#
mirror=0
rotate=0
color=0
invert=0
outline=0
#
add_alignment=0
add_drill_helper=0
use_dos_filenames=0

# 
verbose=no

while test $# -ne 0 ; do
	case "$1" in
		-h|--help)
			usage
			exit 0
			;;

		-v|--verbose)
			verbose=yes
			;;

		--input)
			input_file="$2"
			input_file_set=1
			shift
			;;

		--prefix)
			prefix="$2"
			prefix_set=1
			shift
			;;

		--output-format)
			case "$2" in
			eps) device=1 ;;
			gbr) device=2 ;;
			*)
				echo "$0: Unrecognized output format: $1 $2"
				exit 1
				;;
			esac
			shift
			;;

		--scale)
			scale="$2"
			shift 
			;;

		--mirror) mirror=1 ;;

		--rotate) rotate=1 ;;

		--color) color=1 ;;

		--invert) invert=1 ;;

		--outline) outline=1 ;;

		--add-alignment) add_alignment=1 ;;

		--add-drill-helper) add_drill_helper=1 ;;

		--use-dos-filenames) use_dos_filenames=1 ;;

		-*)
			echo "$0: invalid option: $1"
			usage
			exit 1
			;;

		*)
			break
			;;

	esac
	shift
done

if test $# -ne 0 ; then
	echo "$0: Invalid argument"
	usage
	exit 1
fi

if test $input_file_set -eq 0 ; then
	echo "$0: Missing --input filename.pcb"
	usage
	exit 1
fi

if test ${prefix_set} -eq 0 ; then
	prefix=`basename "${input_file}" .pcb`
fi

cmd="Print($prefix, $device, $scale, $mirror, $rotate, $color, $invert, $outline, $add_alignment, $add_drill_helper, $use_dos_filenames) Quit"

if test "${verbose}" = "yes" ; then
	echo "${PCB} -auto-place -action-string \"${cmd}\" ${input_file}"
fi

${PCB} -auto-place -action-string "${cmd}" ${input_file}

