#!/usr/bin/perl

#
# Creates several smaller patches out of the Psion Linux kernel
# for feeding back to the kernel maintainers and to produce
# patches suitable for bitkeeper.
#
# Version 20021006
#

use strict;

# Place the new patches into the parent directory
my $new_patches_dir = "..";

my @psion_toplevel = (

);

my @psion_toplevel_additional = (
"./.config.patch", 
"./Makefile.patch", 
"./fs/fat/dir.c.patch", 
);

my @psion_arch = (
"./arch/arm/Makefile.patch", 
"./arch/arm/config.in.patch", 
"./arch/arm/def-configs/psion_5mx_ericsson_mc218_16mb.patch", 
"./arch/arm/kernel/Makefile.patch", 
"./arch/arm/kernel/debug-armv.S.patch", 
"./arch/arm/kernel/entry-armv.S.patch", 
"./arch/arm/kernel/irq.c.patch", 
"./arch/arm/kernel/psionw_pm.c.patch", 
"./arch/arm/lib/Makefile.patch", 
"./arch/arm/lib/psionwfiqs.S.patch", 
"./arch/arm/mach-psionw/Makefile.patch", 
"./arch/arm/mach-psionw/irq.c.patch", 
"./arch/arm/mach-psionw/mm.c.patch", 
"./arch/arm/mach-psionw/psionw-arch.c.patch", 
"./arch/arm/mach-psionw/psionw-hardware.c.patch", 
"./arch/arm/mach-psionw/psionw-leds.c.patch", 
"./arch/arm/mach-psionw/psionw-power.c.patch", 
"./arch/arm/mach-psionw/psionw-time.c.patch", 
"./arch/arm/mach-psionw/serial-debug.c.patch", 
"./arch/arm/mm/init.c.patch", 
);

my @psion_arch_additional = (
"./arch/arm/def-configs/psion_revo_8mb.patch", 
"./arch/arm/def-configs/psion_revo_plus_diamond_mako_16mb.patch", 
);

my @psion_arch_controversial = (
"./arch/arm/kernel/head-armv.S.patch", 
"./arch/arm/mm/proc-arm720.S.patch", 
);

my @psion_drivers = (
"./drivers/char/Config.in.patch", 
"./drivers/char/Makefile.patch", 
"./drivers/char/keyboard_psion.c.patch", 
"./drivers/char/keyboard_psion.h.patch", 
"./drivers/char/keymap_psion.map.patch", 
"./drivers/char/psionw-rtc.c.patch", 
"./drivers/char/psionw_procfs.c.patch", 
);

my @psion_driver_serial = (
"./drivers/serial/Config.in.patch", 
"./drivers/serial/Makefile.patch", 
"./drivers/serial/serial_core.c.patch", 
"./drivers/serial/serial_psionw.c.patch", 
"./drivers/serial/serial_psionw.h.patch", 
);

my @psion_driver_fb = (
"./drivers/video/Config.in.patch", 
"./drivers/video/Makefile.patch", 
"./drivers/video/fbmem.c.patch", 
"./drivers/video/psionwfb.c.patch", 
"./drivers/video/psionwfb.h.patch", 
);

my @psion_driver_pcmcia = (
"./drivers/pcmcia/Config.in.patch", 
"./drivers/pcmcia/Makefile.patch", 
"./drivers/pcmcia/psion_etna.c.patch", 
"./drivers/pcmcia/psion_etna.h.patch", 
);

my @psion_driver_ssi = (
"./drivers/ssi/Config.in.patch", 
"./drivers/ssi/Makefile.patch", 
"./drivers/ssi/adc7843.c.patch", 
"./drivers/ssi/ps5mx_ssi.c.patch", 
"./drivers/ssi/ssi_bus.h.patch", 
"./drivers/ssi/ssi_dev.h.patch", 
);

my @psion_driver_sound = (
"./drivers/sound/Config.in.patch", 
"./drivers/sound/Makefile.patch", 
"./drivers/sound/psionw_sound.c.patch", 
);

#
# See also the changes to drivers/char/Makefile for various keymaps
#
my @psion_drivers_additional = (
"./drivers/char/console.c.patch", 
"./drivers/char/keyboard.c.patch", 
"./drivers/char/keymap_psion_de.map.patch", 
"./drivers/char/keymap_psion_us.map.patch", 
"./drivers/char/keymap_psion_fr.map.patch", 
"./drivers/video/fbcon.c.patch", 
);

my @psion_include = (
"./include/asm-arm/arch-psionw/dma.h.patch", 
"./include/asm-arm/arch-psionw/hardware.h.patch", 
"./include/asm-arm/arch-psionw/ide.h.patch", 
"./include/asm-arm/arch-psionw/io.h.patch", 
"./include/asm-arm/arch-psionw/irq.h.patch", 
"./include/asm-arm/arch-psionw/irqs.h.patch", 
"./include/asm-arm/arch-psionw/keyboard.h.patch", 
"./include/asm-arm/arch-psionw/memory.h.patch", 
"./include/asm-arm/arch-psionw/param.h.patch", 
"./include/asm-arm/arch-psionw/psionw-power.h.patch", 
"./include/asm-arm/arch-psionw/system.h.patch", 
"./include/asm-arm/arch-psionw/time.h.patch", 
"./include/asm-arm/arch-psionw/timex.h.patch", 
"./include/asm-arm/arch-psionw/uncompress.h.patch", 
"./include/asm-arm/arch-psionw/vmalloc.h.patch", 
"./include/asm-arm/hardware/psionw.h.patch", 
"./include/linux/miscdevice.h.patch", 
"./include/linux/serial_core.h.patch", 
);

my @psion_include_additional = (
"./include/linux/keyboard.h.patch", 
);

###
# Main program
###

#
# Top level files
#
produce_patchset($new_patches_dir."/psion_toplevel.patch", 
                 "Psion patch for toplevel files", 
                 @psion_toplevel);

produce_patchset($new_patches_dir."/psion_toplevel_additional.patch", 
                 "Psion patch for toplevel additional files", 
                 @psion_toplevel_additional);

#
# Arch
#
produce_patchset($new_patches_dir."/psion_arch.patch", 
                 "Psion patch for linux/arch/arm files", 
                 @psion_arch);

produce_patchset($new_patches_dir."/psion_arch_additional.patch", 
                 "Psion patch for linux/arch/arm additional files", 
                 @psion_arch_additional);

produce_patchset($new_patches_dir."/psion_arch_controversial.patch", 
                 "Psion patch for linux/arch/arm controversial files", 
                 @psion_arch_controversial);

#
# Device drivers
#
produce_patchset($new_patches_dir."/psion_drivers.patch", 
                 "Psion patch for linux/drivers/ files", 
                 @psion_drivers);

# Serial
produce_patchset($new_patches_dir."/psion_driver_serial.patch", 
                 "Psion serial driver patch for linux/drivers/serial/ files", 
                 @psion_driver_serial);

# Framebuffer
produce_patchset($new_patches_dir."/psion_driver_fb.patch", 
                 "Psion framebuffer driver patch for linux/drivers/video/ files", 
                 @psion_driver_fb);

# PCMCIA
produce_patchset($new_patches_dir."/psion_driver_pcmcia.patch", 
                 "Psion PCMCIA driver patch for linux/drivers/pcmcia/ files", 
                 @psion_driver_pcmcia);

# SSI (touchpad & voltage)
produce_patchset($new_patches_dir."/psion_driver_ssi.patch", 
                 "Psion SSI driver (touchpad & voltage) patch for linux/drivers/ssi/ files", 
                 @psion_driver_ssi);

# Sound
produce_patchset($new_patches_dir."/psion_driver_sound.patch", 
                 "Psion sound driver patch for linux/drivers/sound/ files", 
                 @psion_driver_sound);


# Additional drivers
produce_patchset($new_patches_dir."/psion_drivers_additional.patch", 
                 "Psion patch for linux/drivers/ additional files", 
                 @psion_drivers_additional);

#
# Include
#
produce_patchset($new_patches_dir."/psion_include.patch", 
                 "Psion patch for linux/include/ files", 
                 @psion_include);

produce_patchset($new_patches_dir."/psion_include_additional.patch", 
                 "Psion patch for linux/include/ additional files", 
                 @psion_include_additional);


###
# Subroutines
###

sub produce_patchset() {
    my($file, $description, @patch_files) = @_;

    if (scalar(@patch_files < 1)) {
        printf("No files for patch %s...\n", $file);
        return;
    }

    open OUT, ">$file"
      or die "Could not open file for writing: $!\n";
    print OUT $description."\n\n";

    print OUT "This patch contains updates to the following files:\n";

    my $patch_file;
    my $target_file;
    foreach $patch_file (@patch_files) {
        printf("File: %s\n", $patch_file);
        ($target_file, my $junk) = split(".patch", $patch_file);
        print OUT $target_file."\n";
    }
    print OUT "\n\n";

    foreach $patch_file (@patch_files) {
        printf("Adding patch %s...\n", $patch_file);
        open IN, "<$patch_file"
          or die "Could not open file for reading: $!\n";
        while (<IN>) {
            print OUT;
        }
        close IN;
    }

    close OUT;
}
