#pragma module DSBD "V1.0-000"

/*
// COPYRIGHT (c) 1992, 1997 BY
// DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
// ALL RIGHTS RESERVED.
//
// THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
// ONLY  IN  ACCORDANCE  OF  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
// INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR  ANY  OTHER
// COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
// OTHER PERSON.  NO TITLE TO AND  OWNERSHIP OF THE  SOFTWARE IS  HEREBY
// TRANSFERRED.
//
// THE INFORMATION IN THIS SOFTWARE IS  SUBJECT TO CHANGE WITHOUT NOTICE
// AND  SHOULD  NOT  BE  CONSTRUED  AS A COMMITMENT BY DIGITAL EQUIPMENT
// CORPORATION.
//
// DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE  OR  RELIABILITY OF ITS
// SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
*/

/*
//++
//  Facility:
//
//	Examples
//
//  Version: V1.0
//
//  Abstract:
//
//	Example of working with library time and symbol services, and
//	one part of an example of a BACKUP procedure scheduler.
//      DSBD (Days Since Base Date) sets a DCL symbol to the number
//      of days since the system base date.  Used by the operator
//      backup procedure to select the correct tape set.
//
//  Author:
//	Steve Hoffman
//
//  Creation Date:  20-Feb-1989
//
//  Modification History:
//	30-Aug-1989	Hoffman
//	Needed a second bias: account for the day of the week of the
//	VMS system base date -- it was a Wednesday.
//	4-Jun-1997 Hoffman
//	Removed vestiges of VAX C syntax; now compiles under the DEC C
//	compiler without need for the /STANDARD=VAXC qualifier.
//--
*/
#include <descrip.h>
#include <lib$routines.h>
#include <libclidef.h>
#include <ssdef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stsdef.h>
#include <string.h>

#define WEDNESDAY		4   /* 17-Nov-1858 was a Wednesday */

/*
//  Number of tape sets kept and the weeks-since-base-date bias
*/
#define MAXTAPESET		2   /* number of sets */
#define WEEKLYBIAS		1   /* week 1 uses tapeset B */


main()
    {
    int RetStat;
    int DaysSinceBaseDate;
    char DaysBuffer[9];
    char TapeSetBuffer[2];
    struct dsc$descriptor DaysBufferDesc;
    struct dsc$descriptor TapeSetDesc;
    $DESCRIPTOR( DaysSymbDesc, "DSBD$DAYSSINCEBASEDATE" );
    $DESCRIPTOR( TapeSetSymbDesc, "DSBD$BACKUPTAPESET" );

    DaysBufferDesc.dsc$b_dtype  = DSC$K_DTYPE_T;
    DaysBufferDesc.dsc$b_class  = DSC$K_CLASS_S;
    DaysBufferDesc.dsc$a_pointer = DaysBuffer;

    TapeSetDesc.dsc$b_dtype  = DSC$K_DTYPE_T;
    TapeSetDesc.dsc$b_class  = DSC$K_CLASS_S;
    TapeSetDesc.dsc$a_pointer = TapeSetBuffer;

    RetStat = lib$day( &DaysSinceBaseDate );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
	return RetStat;

    /*
    // convert the days since base-date to a string and convert
    // the days since base-date into the weekly tape set code.
    // Take the tapeset weekly bias and the fact that the base
    // date was not a Sunday but a Wednesday into account.
    */
    sprintf( DaysBuffer, "%8.8d", DaysSinceBaseDate );
    DaysBufferDesc.dsc$w_length = strlen( DaysBuffer );
    sprintf( TapeSetBuffer, "%c",
	(int) 'A' +
	(((
	(DaysSinceBaseDate - WEDNESDAY) / 7) + WEEKLYBIAS)
	% MAXTAPESET
	) );
    TapeSetDesc.dsc$w_length = strlen( TapeSetBuffer );

    /*
    //	Set a couple of DCL symbols for the BACKUP command procedure.
    */
    RetStat = lib$set_symbol( &DaysSymbDesc,
	&DaysBufferDesc, &LIB$K_CLI_GLOBAL_SYM );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
	return RetStat;
    RetStat = lib$set_symbol( &TapeSetSymbDesc,
	&TapeSetDesc, &LIB$K_CLI_GLOBAL_SYM );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
	return RetStat;

    return SS$_NORMAL;
    }
