ui_print("CleanGB Build 18");
show_progress(1.000000, 0);

package_extract_file("tools/fat.format", "/tmp/fat.format");
set_perm(0, 0, 0755, "/tmp/fat.format");
set_progress(0.050000);


#EXTRACT BACKUP SCRIPT
package_extract_file("tools/busybox", "/tmp/busybox");
package_extract_file("tools/tar-backup", "/tmp/tar-backup");

set_perm(0, 0, 0755, "/tmp/tar-backup");
set_perm(0, 0, 0755, "/tmp/busybox");


#CREATE BACKUP
set_progress (0.350);
ui_print("Backing up data - this may take a while...");
run_program("/sbin/mount","/sdcard");
run_program("/sbin/mount","/data");
run_program("/tmp/tar-backup","backup");

set_progress(0.35000);

unmount("/system");
unmount("/data");
unmount("/cache");

ui_print("Formatting system");
run_program("/sbin/dd", "if=/dev/zero", "of=/dev/block/stl9", "bs=4096");
run_program("/sbin/dd", "if=/dev/zero", "of=/dev/block/stl10", "bs=4096");
run_program("/sbin/dd", "if=/dev/zero", "of=/dev/block/stl11", "bs=4096");
run_program("/tmp/fat.format", "-F", "32", "-S", "4096", "-s", "1", "/dev/block/stl9");
run_program("/tmp/fat.format", "-F", "32", "-S", "4096", "-s", "1", "/dev/block/stl10");
run_program("/tmp/fat.format", "-F", "16", "-S", "4096", "-s", "1", "/dev/block/stl11");

set_progress(0.45000);
ui_print("Mounting file systems");
run_program("/sbin/mount", "-t", "rfs", "-o", "check=no", "/dev/block/stl9", "/system");
run_program("/sbin/mount", "-t", "rfs", "-o", "nosuid,nodev,check=no", "/dev/block/stl10", "/data");
run_program("/sbin/mount", "-t", "rfs", "-o", "nosuid,nodev,check=no", "/dev/block/stl11", "/cache");

ui_print("Extracting System...");
package_extract_dir("system", "/system");
delete("/system/etc/init.d/file");
set_progress(0.60000);


ui_print("Setting symlinks...");
symlink("dumpstate","/system/bin/dumpcrash","/system/bin/dumpmesg");
symlink("toolbox","/system/bin/cat","/system/bin/chmod","/system/bin/chown","/system/bin/cmp","/system/bin/date","/system/bin/dd",
        "/system/bin/df","/system/bin/dmesg","/system/bin/getevent","/system/bin/getprop","/system/bin/hd","/system/bin/id",
        "/system/bin/ifconfig","/system/bin/iftop","/system/bin/insmod","/system/bin/ioctl","/system/bin/ionice","/system/bin/kill",
        "/system/bin/ln","/system/bin/log","/system/bin/ls","/system/bin/lsmod","/system/bin/lsof","/system/bin/mkdir",
        "/system/bin/mv","/system/bin/nandread","/system/bin/netstat","/system/bin/newfs_msdos","/system/bin/notify",
        "/system/bin/printenv", "/system/bin/ps","/system/bin/reboot","/system/bin/renice","/system/bin/rm","/system/bin/rmdir",
        "/system/bin/rmmod","/system/bin/route","/system/bin/schedtop","/system/bin/sendevent","/system/bin/setconsole",
        "/system/bin/setprop","/system/bin/sleep","/system/bin/smd","/system/bin/start","/system/bin/stop","/system/bin/sync",
        "/system/bin/top","/system/bin/umount","/system/bin/uptime","/system/bin/vmstat","/system/bin/watchprops","/system/bin/wipe");
symlink("/system/xbin/busybox","/system/bin/busybox");
symlink("busybox","/system/xbin/acpid","/system/xbin/adjtimex","/system/xbin/arp","/system/xbin/arping",
		"/system/xbin/awk","/system/xbin/[","/system/xbin/[[","/system/xbin/base64","/system/xbin/basename",
		"/system/xbin/beep","/system/xbin/blkid","/system/xbin/cal","/system/xbin/cat","/system/xbin/catv",
		"/system/xbin/chattr","/system/xbin/chgrp","/system/xbin/chmod",
		"/system/xbin/chown","/system/xbin/chroot","/system/xbin/chrt","/system/xbin/cksum","/system/xbin/clear",
		"/system/xbin/cmp","/system/xbin/comm","/system/xbin/cp","/system/xbin/cut","/system/xbin/date",
		"/system/xbin/dd","/system/xbin/depmod","/system/xbin/devmem","/system/xbin/df","/system/xbin/diff",
		"/system/xbin/dirname","/system/xbin/dmesg","/system/xbin/dnsd","/system/xbin/dnsdomainname",
		"/system/xbin/dos2unix","/system/xbin/du","/system/xbin/dumpkmap","/system/xbin/echo","/system/xbin/egrep",
		"/system/xbin/env","/system/xbin/ether-wake","/system/xbin/expand","/system/xbin/expr","/system/xbin/fakeidentd",
		"/system/xbin/fgrep","/system/xbin/find","/system/xbin/findfs","/system/xbin/flock","/system/xbin/fold",
		"/system/xbin/free","/system/xbin/fsck","/system/xbin/fsync","/system/xbin/ftpget","/system/xbin/ftpput",
		"/system/xbin/fuser","/system/xbin/getopt","/system/xbin/grep","/system/xbin/gunzip","/system/xbin/gzip",
		"/system/xbin/hd","/system/xbin/head","/system/xbin/hexdump","/system/xbin/hostid","/system/xbin/hostname",
		"/system/xbin/httpd","/system/xbin/hwclock","/system/xbin/id","/system/xbin/ifconfig","/system/xbin/ifenslave",
		"/system/xbin/inetd","/system/xbin/insmod","/system/xbin/install","/system/xbin/ionice","/system/xbin/iostat",
		"/system/xbin/ip","/system/xbin/ipaddr","/system/xbin/ipcalc","/system/xbin/iplink","/system/xbin/iproute",
		"/system/xbin/iprule","/system/xbin/iptunnel","/system/xbin/kill","/system/xbin/killall","/system/xbin/killall5",
		"/system/xbin/length","/system/xbin/less","/system/xbin/link.txt","/system/xbin/ln","/system/xbin/logname",
		"/system/xbin/losetup","/system/xbin/ls","/system/xbin/lsattr","/system/xbin/lsmod","/system/xbin/lsusb",
		"/system/xbin/md5sum","/system/xbin/mkdir","/system/xbin/mke2fs","/system/xbin/mkfifo","/system/xbin/mkfs.ext2",
		"/system/xbin/mknod","/system/xbin/modinfo","/system/xbin/modprobe","/system/xbin/more","/system/xbin/mount",
		"/system/xbin/mountpoint","/system/xbin/mpstat","/system/xbin/mv","/system/xbin/nc","/system/xbin/netstat",
		"/system/xbin/nice","/system/xbin/nmeter","/system/xbin/nohup","/system/xbin/nslookup","/system/xbin/ntpd",
		"/system/xbin/od","/system/xbin/patch","/system/xbin/pgrep","/system/xbin/pidof","/system/xbin/ping",
		"/system/xbin/ping6","/system/xbin/pkill","/system/xbin/pmap","/system/xbin/powertop","/system/xbin/printenv",
		"/system/xbin/printf","/system/xbin/ps","/system/xbin/pscan","/system/xbin/pwd","/system/xbin/rdate",
		"/system/xbin/rdev","/system/xbin/readlink","/system/xbin/realpath","/system/xbin/renice","/system/xbin/reset",
		"/system/xbin/resize","/system/xbin/rev","/system/xbin/rm","/system/xbin/rmdir","/system/xbin/rmmod",
		"/system/xbin/route","/system/xbin/runlevel","/system/xbin/script","/system/xbin/scriptreplay","/system/xbin/sed",
		"/system/xbin/seq","/system/xbin/setkeycodes","/system/xbin/setlogcons","/system/xbin/setsid","/system/xbin/sha1sum",
		"/system/xbin/sha256sum","/system/xbin/sha512sum","/system/xbin/showkey","/system/xbin/sleep","/system/xbin/smemcap",
		"/system/xbin/sort","/system/xbin/split","/system/xbin/stat","/system/xbin/strings","/system/xbin/stty",
		"/system/xbin/sum","/system/xbin/sync","/system/xbin/sysctl","/system/xbin/tac","/system/xbin/tail",
		"/system/xbin/tar","/system/xbin/tcpsvd","/system/xbin/tee","/system/xbin/telnet","/system/xbin/telnetd",
		"/system/xbin/test","/system/xbin/tftp","/system/xbin/tftpd","/system/xbin/time","/system/xbin/timeout",
		"/system/xbin/top","/system/xbin/touch","/system/xbin/tr","/system/xbin/traceroute","/system/xbin/traceroute6",
		"/system/xbin/tty","/system/xbin/ttysize","/system/xbin/tunctl","/system/xbin/udpsvd","/system/xbin/umount",
		"/system/xbin/uname","/system/xbin/uncompress","/system/xbin/unexpand","/system/xbin/uniq","/system/xbin/unix2dos",
		"/system/xbin/unzip","/system/xbin/uptime","/system/xbin/usleep","/system/xbin/uudecode","/system/xbin/uuencode",
		"/system/xbin/vconfig","/system/xbin/vi","/system/xbin/watch","/system/xbin/wc","/system/xbin/wget",
		"/system/xbin/who","/system/xbin/whoami","/system/xbin/zcat");
ui_print("Setting permissions...");
set_perm_recursive(0, 0, 0755, 0644, "/system/app");
set_perm_recursive(0, 0, 0777, 0777, "/data");
set_perm_recursive(0, 2000, 0755, 0755, "/system/bin");
set_perm_recursive(0, 2000, 0755, 0755, "/vendor/bin");
set_perm(0, 3003, 02755, "/system/bin/netcfg");
set_perm(0, 3004, 02755, "/system/bin/ping");
set_perm(0, 0, 0644, "/system/etc/resolv.conf");
set_perm(1002, 1002, 0440, "/system/etc/dbus.conf");
set_perm(1014, 2000, 0550, "/system/etc/dhcpcd/dhcpcd-run-hooks");
set_perm(0, 2000, 0550, "/system/etc/init.goldfish.sh");
set_perm_recursive(0, 0, 0755, 0555, "/system/etc/ppp");
set_perm_recursive(0, 2000, 0755, 0755, "/system/xbin");
set_perm_recursive(0, 0, 0755, 0644, "/system/etc/security/drm");
set_perm(0, 0, 04755, "/system/xbin/ipctool");
set_perm_recursive(0, 2000, 0755, 0755, "/system/vendor/bin");



ui_print("Setting SYSTEM:bin/su permissions to 6755...");
set_perm(0, 0, 6755, "/system/bin/su");


ui_print("Creating SYSTEM:xbin/su symbolic link...");
symlink("/system/bin/su", "/system/xbin/su");
set_progress(0.61000);

ui_print("Restoring data - this may take awhile...");
run_program("/sbin/mount","/data");
run_program("/tmp/tar-backup","restore");
unmount("/data");


ui_print("Finishing Flash");
package_extract_dir("kernel_update", "/tmp");
set_perm(0, 0, 0755, "/tmp/bmlwrite");
run_program("/tmp/bmlwrite", "/tmp/zImage", "/dev/block/bml7");
ui_print("Install complete, enjoy!");
set_progress(1.000000);
