#!/bin/sh
#
# Switch to a different version of Zeus Traffic Manager

# Ensure ZEUSHOME is setup to point to our installation
if [ -z "$ZEUSHOME" ]
then
    # Have a good guess, we live in $ZEUSHOME/zxtm/bin
    OLD_PWD=`pwd`
    cd `dirname $0`/..
    cd ..
    ZEUSHOME=`pwd`
    export ZEUSHOME
    cd $OLD_PWD
fi

if [ ! -d "$ZEUSHOME/zxtm" ]
then
   echo "rollback-helper FATAL: ZEUSHOME not set"
   exit 1
fi

# Try to get networking commands into our PATH
PATH=/sbin:/usr/etc/sbin:/usr/etc:/usr/sbin:$PATH

# Restart ourselves in perl
exec $ZEUSHOME/perl/miniperl -x $0 ${1+"$@"}

#!perl -w
#line 31

=head1 NAME

rollback-helper - change to a different installed version of Zeus Traffic Manager

=head1 SYNOPSIS

B<rollback-helper>

=head1 DESCRIPTION

B<rollback-helper> is called by the rollback script to assist with roll back and
roll forward operations. Unlike the rollback script, it runs in the full environment
of the higher numbered zxtm version. So it can use more library dependencies.

This script is not intended to be run directly.

=head1 COPYRIGHT

Copyright (C) 2014-2015 Riverbed Technology Limited. All rights reserved.

=cut

# setup our libraries
BEGIN {
   unshift @INC, ( "$ENV{ZEUSHOME}/perl",
                   "$ENV{ZEUSHOME}/admin/lib/perl",
                   "$ENV{ZEUSHOME}/zxtmadmin/lib/perl",
                   "$ENV{ZEUSHOME}/zxtm/lib/perl",
                 );
}

use Zeus::ZInstall::ZInstall;
use strict;

sub post_roll_forward($$);
sub pre_roll_back($$);

chdir( $ENV{ZEUSHOME} ) or die "Can't cd to $ENV{ZEUSHOME}: $!\n";

# ----------------------------------------------------------
# Process arguments
my $to_version;
my $from_version;
my $hook;

foreach ( @ARGV ) {
   if( /^-h|-\?|--help$/ ) {
      print "Usage: rollback-helper [options]\n";
      print "Options:\n";
      print "    --help                  this message\n";
      print "    --from=VERSION          changing from this specific version\n";
      print "    --to=VERSION            changing to this specific version\n";
      print "    --hook=[pre|post]       hook state, pre or post version change\n";
      print "\n";
      print " *** Don't run this directly, run the 'rollback' script instead ***\n";
      exit 0;

   } elsif ( /^--(.*?)=(.*)$/ || /^--(.*)$/ ) {
      my( $key, $val ) = ( $1, $2 );

      # Check valid options and set variables
      if( $key eq 'to' ) { $to_version = $val; }
      elsif( $key eq 'from' ) { $from_version = $val; }
      elsif( $key eq 'hook' ) { $hook = $val; }
      else {
         print STDERR "Unknown option: $_\n";
         exit 1;
      }
   } else {
      print STDERR "Unknown parameter: $_\n";
      exit 1;
   }
}

if( !defined( $hook ) ) {
   print STDERR "Missing --hook argument\n";
   exit 1;
}

my $direction = Zeus::ZInstall::ZInstall::compare_versions( $to_version, $from_version );
if( $direction > 0 && $hook eq 'post' ) {
   post_roll_forward( $to_version, $from_version );
} elsif( $direction < 0 && $hook eq 'pre' ) {
   pre_roll_back( $to_version, $from_version );
}

exit 0;

sub post_roll_forward( $$ )
{
   my( $to_version, $from_version ) = @_;
   if( Zeus::ZInstall::ZInstall::compare_versions( $from_version, "9.7a1" ) < 0 ) {
      # Need to hide the AFM startup files from pre-9.7
      foreach my $file( qw( S15afm S16updater ) ) {
         if( -e "rc.d/$file" || -l "rc.d/$file" ) {
            rename( "rc.d/$file", "rc.d/.$file" );
         }
      }

      # Need to upgrade the boot scripts so that zxtm stops before networking
      Zeus::ZInstall::Common::upgrade_bootable();
   }
}

sub pre_roll_back( $$ )
{
   my( $to_version, $from_version ) = @_;
   if( Zeus::ZInstall::ZInstall::compare_versions( $to_version, "9.7a1" ) < 0 ) {
      # Might need to migrate back to a previous AFM version, so need to
      # re-establish the start scripts.
      foreach my $file( qw( S15afm S16updater ) ) {
         if( -e "rc.d/.$file" || -l "rc.d/.$file" ) {
            rename( "rc.d/.$file", "rc.d/$file" );
         }
      }

      # Need to downgrade the boot scripts so that the old configure script
      # can recognise them
      Zeus::ZInstall::Common::downgrade_bootable();
   }
}

