#!/usr/bin/perl

use strict;
use warnings;
use Expect;

my $exp = Expect->new();

$exp->spawn("fdisk", ('/dev/sda'));

my $count1 = 0;
my $count2 = 0;

$exp->expect(10,
  [
    qr/m for help/ => sub {
      my $exp = shift;

      if ($count1 == 0) {
        $exp->send("d\n");
      }
      elsif ($count1 == 1) {
        $exp->send("n\n");
      }
      elsif ($count1 == 2) {
        $exp->send("w\n");
      }

      $count1++;
      exp_continue;
    },
  ],
  [
    qr/Partition number/ => sub {
      my $exp = shift;

      if ($count2++ == 0) {
        $exp->send("4\n");
      }

      exp_continue;
    },
  ],
  [
    qr/primary partition/ => sub {
      my $exp = shift;
      $exp->send("p\n");
      exp_continue;
    },
  ],
  [
    qr/First sector/ => sub {
      my $exp = shift;
      $exp->send("\n");
      exp_continue;
    },
  ],
  [
    qr/Last sector/ => sub {
      my $exp = shift;
      $exp->send("\n");
      exp_continue;
    },
  ],
);

