#!/bin/bash
NUM=0
QUEUE=""
MAX_NPROC=2 # default
REPLACE_CMD=0 # no replacement by default

USAGE="A simple wrapper for generating jpg pictures out of svg-files in parallel.
Usage: `basename $0` [-h] [-i] [-j nb_jobs] command arg_list
    -h      Shows this help
    -c      give the path to the cfg file
    -i      give the file where the web-adresses are
    -k      chek if the output file already exist
    -s      set the sleeptime while there are too many simultanious jobs
    -j      Set number of simultanious jobs. Default is 2
    -o      directory where the files should be moved to after creation.
            Default ist PICCREATE_OUT
 Examples:
    `basename $0` -i file -c config 
    `basename $0` -j 3 -i file -c config"
 
function cfg_parser {
    INI_FILE=$1
    SECTION=$2
    eval `sed -n -e "/^\[$SECTION\]/,/^\s*\[/{/^[^;].*\=.*/p;}" < $INI_FILE`
}
function get_filepath {
    # probiere alle möglichen Kombinationen wo das file stehen könnte je
    # nachdem ob es mit oder ohne Endung, mit oder ohne den Komplettpfad
    # gegeben wurde
    FILENAME=$1
    ENDING=$2
    DEFAULT_DIR=$3
    if [ -s $FILENAME ] ; then
        echo $FILENAME
    elif [ -s $FILENAME.$ENDING ] ; then
        echo "$FILENAME.$ENDING"
    elif [ -s $DEFAULT_DIR/$FILENAME ] ; then
        echo "$DEFAULT_DIR/$FILENAME"
    elif [ -s $DEFAULT_DIR/$FILENAME.$ENDING ] ; then
        echo "$DEFAULT_DIR/$FILENAME.$ENDING"
    fi
    return
}

# Funktion die auf globalen Parametern beruht.
# überprüft ob alle notwendigen Angaben vorhanden sind
function check_params {
        if [ -z "$INPUT_FILENAME" ]; then
            echo "please give input file with -i."
            exit 1
        fi
        if [ -z "$CFG_FILENAME" ]; then
            echo "please give config file with -c."
            exit 1
        fi

        INPUT_FILE=`get_filepath $INPUT_FILENAME 'csv' $PICCREATE_WORK`
        if [ ! -s $INPUT_FILE ] ; then
            echo "-> input filename does not exist"
            print "given was: -$INPUT_FILENAME-"
            exit 1
        fi
        CFG_PATH=`get_filepath $CFG_FILENAME 'cfg' $PICCREATE_CFG`
        if [ ! -s $CFG_PATH ]; then
            echo "-> config file with this name does not exist."
            print "given was: -$CFG_FILENAME-"
            exit 1
        fi
		echo $INPUT_FILENAME
        cfg_parser $CFG_PATH "$FILE_FORMAT" 
        echo 'using width '$width
        echo 'using height '$height
        if [ -z "$width" ]; then
            echo "please give width=xxx in section [$FILE_FORMAT] in config $CFG_FILENAME."
            exit 1
        fi
        if [ -z "$height" ]; then
            echo "please give height=xxx in section [$FILE_FORMAT] in config $CFG_FILENAME."
            exit 1
        fi
		FORMAT="$format"
}


# parse command line
if [ $# -eq 0 ]; then #  must be at least one arg
    echo "$USAGE" >&2
    exit 1
fi

CHECKFILE=1
SLEEPTIME=0.5
BACKGROUND=''
FILE_FORMAT='webscan'
OUTDIR=$PICCREATE_OUT
while getopts "j:i:kc:s:o:hc:f" OPT; do # "j:" waits for an argument "h" doesnt
    case $OPT in
    h)  echo "$USAGE"
        exit 0 ;;
    j)  MAX_NPROC=$OPTARG ;;
    k)  CHECKFILE=0 ;;
    c)  CFG_FILENAME=$OPTARG ;;
    o)  OUTDIR=$OPTARG ;;
    s)  SLEEPTIME=$OPTARG ;;
    i)  INPUT_FILENAME=$OPTARG ;;
	f)  FORMAT=$OPTARG ;;
    *) echo "$USAGE" >&2; exit 1;   # Default.
    esac
done

# Main program
echo Using $MAX_NPROC parallel threads
check_params

# Starte die Abspeichern der ganzen Webseite als ganzes
echo $INPUT_FILE
for i in `cat $INPUT_FILENAME`
do
    CUSTOMER_ID=`echo "$i" | sed -e 's/;.*//g'`
	CUSTOMER_URL=`echo "$i" |sed -e 's/.*;//g'`

    FILE_PATH=$OUTDIR/$CUSTOMER_ID.$FORMAT
    echo $FILE_PATH
    wait
    if [ -z $CUSTOMER_URL ]
        then
            echo 'i need a url!!'
            echo $CUSTOMER_ID 'did not have an url, ist was set to www.syntops.de instead'>> logfile.txt
			CUSTOMER_URL='www.syntops.de'
            sleep 
    fi
    if [ -s $FILE_PATH ] && [ $CHECKFILE -eq 1 ] 
	    then
		    echo $FILE_PATH exists
        else 
            CMDLINE="$CUTYCAPT/./CutyCapt --url=http://$CUSTOMER_URL --max-wait=20000 --min-width=800 --min-height=600 --out=$FILE_PATH"
            $CMDLINE
			wait
    fi
done

wait # wait for all processes to finish before exit

echo "...all Webpages have been saved!"
echo ""
echo "Converting the pictures..."
echo " "


# Zuschnitt der seite auf das benötigte Format
# Das Bild wir hierfür erst gleichmäßig auf die forgegebene Breite Skaliert
# und anschließend die unten herüberstehende Höhe abgeschnitten
echo $outdir
for i in `ls $OUTDIR| grep .$FORMAT `
do
    FILE_PATH_IN=$OUTDIR/$i
    FILE_PATH_OUT=$OUTDIR/'beschnitten'/$i

	if [ -s $FILE_PATH_OUT ] && [ $CHECKFILE -eq 1 ]
        then
            echo $PIC_NAME exists
        else 
            RESIZE="${width}"
			SLICE="${width}x${height}"
            CMDLINE="convert $FILE_PATH_IN -resize $RESIZE -crop $SLICE $FILE_PATH_OUT"
            echo $CMDLINE
            (echo $ID; $CMDLINE) &
 
 
            while [ `ps clx|grep -w 'convert' |wc -l` -ge $MAX_NPROC ]; do
                sleep $SLEEPTIME
            done
    fi
    # auskommentiert da bereits runtergeladene seiten behalten werden sollen
	#rm $FILE_PATH

done
 
wait # wait for all processes to finish before exit

rm $OUTDIR/'beschnitten'/*-[1-9].$FORMAT

	# Löscht den verschnitt aus dem output-ordner



# vi:set wrapmargin=0 ai showmatch ts=4 syntax=sh: 
